|
1 |
| -from banker.analyzer.analyze import analyze_transactions, deduce_month_year |
2 |
| -from banker.data.category import Category, PaymentType |
3 | 1 | import argparse
|
| 2 | +import os.path |
| 3 | + |
| 4 | +from importlib_resources import files |
| 5 | + |
| 6 | +from banker.analyzer.analyze import analyze_transactions, deduce_month_year |
| 7 | +from banker.data.category import Category |
4 | 8 |
|
| 9 | +from banker.data.transaction import Transaction |
5 | 10 | from banker.formatter.month_year_formatter import format_month_year
|
6 | 11 | from banker.parser.html_transactions_parser import HtmlTransactionsParser
|
7 | 12 | from banker.formatter.html_transactions_formatter import HtmlTransactionsFormatter
|
| 13 | +from banker.parser.interfaces.categories_parser import ICategoriesParser |
| 14 | +from banker.parser.interfaces.transactions_parser import ITransactionsParser |
| 15 | +from banker.parser.json_categories_parser import JsonCategoriesParser |
8 | 16 | from banker.writer.excel_categories_writer import ExcelCategoriesWriter
|
9 | 17 |
|
10 | 18 |
|
| 19 | +def get_supported_categories(categories_parser: ICategoriesParser, categories_filepath: str) -> list[Category]: |
| 20 | + with open(categories_filepath, "r") as file: |
| 21 | + return categories_parser.parse_categories(file.read()) |
| 22 | + |
| 23 | + |
| 24 | +def get_transactions(transactions_parser: ITransactionsParser, transactions_filepath: str) -> list[Transaction]: |
| 25 | + with open(transactions_filepath, "r") as transactions_file: |
| 26 | + return transactions_parser.parse_transactions(transactions_file.read()) |
| 27 | + |
| 28 | + |
| 29 | +def save_to_file(filepath: str, content: str): |
| 30 | + with open(filepath, "w") as file: |
| 31 | + file.write(content) |
| 32 | + |
| 33 | + |
11 | 34 | def main():
|
12 |
| - supported_categories = [ |
13 |
| - Category(name="Kaufland", payment_type=PaymentType.Household, matching_regexes=[r"KAUFLAND PL"])] |
14 | 35 | transactions_parser = HtmlTransactionsParser()
|
| 36 | + categories_parser = JsonCategoriesParser() |
15 | 37 | transactions_formatter = HtmlTransactionsFormatter()
|
16 | 38 | categories_writer = ExcelCategoriesWriter()
|
17 | 39 |
|
18 | 40 | parser = argparse.ArgumentParser()
|
19 | 41 | parser.add_argument("html_file")
|
| 42 | + parser.add_argument("--categories_file", default=files('banker.resources').joinpath('categories.json')) |
| 43 | + parser.add_argument("--output_directory", default=files('banker.resources').joinpath('output')) |
20 | 44 | args = parser.parse_args()
|
21 | 45 |
|
22 |
| - with open(args.html_file, "rb") as input_file: |
23 |
| - all_transactions = transactions_parser.parse_transactions(input_file.read().decode('utf-8')) |
24 |
| - analyze_result = analyze_transactions(all_transactions, supported_categories) |
25 |
| - formatted_transactions = transactions_formatter.format_transactions(analyze_result.unmatched_transactions) |
26 |
| - with open("unmatched_transactions.html", "w") as transactions_file: |
27 |
| - transactions_file.write(formatted_transactions) |
28 |
| - month_year = deduce_month_year(all_transactions) |
29 |
| - categories_writer.write_categories(analyze_result.matched_categories, "autogen_budget.xlsx", |
30 |
| - format_month_year(month_year)) |
| 46 | + os.makedirs(args.output_directory, exist_ok=True) |
| 47 | + output_unmatched_transactions_filepath = os.path.join(args.output_directory, "unmatched_transactions.html") |
| 48 | + output_matched_categories_filepath = os.path.join(args.output_directory, "autogen_budget.xlsx") |
| 49 | + |
| 50 | + all_transactions = get_transactions(transactions_parser, args.html_file) |
| 51 | + month_year = deduce_month_year(all_transactions) |
| 52 | + supported_categories = get_supported_categories(categories_parser, args.categories_file) |
| 53 | + analyze_result = analyze_transactions(all_transactions, supported_categories) |
| 54 | + formatted_transactions = transactions_formatter.format_transactions(analyze_result.unmatched_transactions) |
| 55 | + |
| 56 | + save_to_file(output_unmatched_transactions_filepath, formatted_transactions) |
| 57 | + categories_writer.write_categories(analyze_result.matched_categories, output_matched_categories_filepath, |
| 58 | + format_month_year(month_year)) |
0 commit comments