(
filenames: list[pathlib.Path],
backup_suffix: str = ".backup",
rename_account: list[tuple[str, str]] | None = None,
rename_currency: list[tuple[str, str]] | None = None,
stdin_mode: bool = False,
backup: bool = False,
logger: logging.Logger | None = None,
)
| 117 | |
| 118 | |
| 119 | def format_beancount( |
| 120 | filenames: list[pathlib.Path], |
| 121 | backup_suffix: str = ".backup", |
| 122 | rename_account: list[tuple[str, str]] | None = None, |
| 123 | rename_currency: list[tuple[str, str]] | None = None, |
| 124 | stdin_mode: bool = False, |
| 125 | backup: bool = False, |
| 126 | logger: logging.Logger | None = None, |
| 127 | ): |
| 128 | logger = logger or logging.getLogger(__name__) |
| 129 | tree_transformers: list[typing.Callable] = [] |
| 130 | if rename_account: |
| 131 | for from_val, to_val in rename_account: |
| 132 | logger.info("Renaming account from %s to %s", from_val, to_val) |
| 133 | tree_transformers.append( |
| 134 | functools.partial(rename_account_transform, dict(rename_account)) |
| 135 | ) |
| 136 | if rename_currency: |
| 137 | for from_val, to_val in rename_currency: |
| 138 | logger.info("Renaming currency from %s to %s", from_val, to_val) |
| 139 | tree_transformers.append( |
| 140 | functools.partial(rename_commodity_transform, dict(rename_currency)) |
| 141 | ) |
| 142 | |
| 143 | # TODO: support follow include statements |
| 144 | parser = make_parser() |
| 145 | if stdin_mode: |
| 146 | logger.info("Processing in stdin mode") |
| 147 | input_content = sys.stdin.read() |
| 148 | tree = parser.parse(input_content) |
| 149 | formatter = Formatter() |
| 150 | formatter.format(tree, sys.stdout) |
| 151 | else: |
| 152 | if filenames: |
| 153 | iterator = file_tree_iterator( |
| 154 | parser=parser, |
| 155 | filenames=filenames, |
| 156 | ) |
| 157 | else: |
| 158 | logger.info("No files provided, traverse starting from main.bean") |
| 159 | iterator = traverse( |
| 160 | parser=parser, |
| 161 | bean_file=pathlib.Path("main.bean"), |
| 162 | root_dir=pathlib.Path.cwd(), |
| 163 | ) |
| 164 | for filepath, tree in iterator: |
| 165 | logger.info("Processing file %s", filepath) |
| 166 | if tree_transformers: |
| 167 | logger.info( |
| 168 | "Running %s transforms against file %s ...", |
| 169 | len(tree_transformers), |
| 170 | filepath, |
| 171 | ) |
| 172 | tree = walk_tree( |
| 173 | tree, |
| 174 | functools.partial(combine_transforms, tree_transformers), |
| 175 | ) |
| 176 | with tempfile.NamedTemporaryFile(mode="wt+", suffix=".bean") as output_file: |
no test coverage detected