def main(): logger.info(f"Parsing statement files.") parser = None try: parser = globals()[parsed_args.source].Parser(parsed_args.input_dir) statements = parser.parse() except KeyError: logger.error(f"Unsupported source system: [{parsed_args.source}].") raise SystemExit(1) logger.info(f"Generating [statements.csv] file.") export_statements(os.path.join(parsed_args.output_dir, "statements.csv"), statements) if not statements: logger.error( f"Not activities found. Please, check your statement files.") raise SystemExit(1) logger.info(f"Populating exchange rates.") populate_exchange_rates(statements, parsed_args.use_bnb) logger.info(f"Calculating dividends information.") dividends = calculate_dividends(statements) logger.info(f"Generating [app8-part4-1.csv] file.") export_app8_part4_1( os.path.join(parsed_args.output_dir, "app8-part4-1.csv"), dividends) sales = None purchases = None unsupported_activity_types = parser.get_unsupported_activity_types( statements) if len(unsupported_activity_types) == 0: logger.info(f"Calculating sales information.") sales, purchases = calculate_win_loss(statements) logger.info(f"Generating [app5-table2.csv] file.") export_app5_table2( os.path.join(parsed_args.output_dir, "app5-table2.csv"), sales) logger.info(f"Generating [dec50_2020_data.xml] file.") aggregated_data = export_to_xml( os.path.join(parsed_args.output_dir, "dec50_2020_data.xml"), dividends, sales, purchases) if aggregated_data is not None: logger.info(f"Generating [app8-part1.csv] file.") export_app8_part1( os.path.join(parsed_args.output_dir, "app8-part1.csv"), aggregated_data) win_loss = sum(item["profit"] + item["loss"] for item in sales) logger.info(f"Profit/Loss: {win_loss} lev.") if unsupported_activity_types: logger.warning( f"Statements contain unsupported activity types: {unsupported_activity_types}. Only dividends related data was calculated." )
def main(): logger.info("Collecting statement files.") statement_files = list_statement_files(parsed_args.input_dir) if not statement_files: logger.error(f"No statement files found.") raise SystemExit(1) logger.info( f"Collected statement files for processing: {statement_files}.") logger.info(f"Parsing statement files.") statements = parse_statements(statement_files) logger.info(f"Generating [statements.csv] file.") export_statements(os.path.join(parsed_args.output_dir, "statements.csv"), statements) if not statements: logger.error( f"Not activities found. Please, check your statement files.") raise SystemExit(1) unsupported_activity_types = get_unsupported_activity_types(statements) if unsupported_activity_types: logger.error( f"Statements contain unsupported activity types: {unsupported_activity_types}. Please, check documentation." ) raise SystemExit(1) logger.info(f"Populating exchange rates.") populate_exchange_rates(statements, parsed_args.use_bnb) logger.info(f"Generating [app8-part1.csv] file.") export_app8_part1(os.path.join(parsed_args.output_dir, "app8-part1.csv"), statements) logger.info(f"Calculating sales information.") sales = calculate_win_loss(statements) logger.info(f"Generating [app5-table2.csv] file.") export_app5_table2(os.path.join(parsed_args.output_dir, "app5-table2.csv"), sales) logger.info(f"Calculating dividends information.") dividends = calculate_dividends(statements) logger.info(f"Generating [app8-part4-1.csv] file.") export_app8_part4_1( os.path.join(parsed_args.output_dir, "app8-part4-1.csv"), dividends) logger.info(f"Generating [dec50_2020_data.xml] file.") export_to_xml(os.path.join(parsed_args.output_dir, "dec50_2020_data.xml"), statements, sales, dividends) win_loss = sum(item["profit"] + item["loss"] for item in sales) logger.info(f"Profit/Loss: {win_loss} lev.")
def process(input_dir, output_dir, parser_names, use_bnb, in_currency=False): logger.debug(f"Supported parsers: [{supported_parsers}]") parser_names = list(dict.fromkeys(parser_names)) logger.info(f"Parsing statement files with parsers: {parser_names}.") statements = {} for parser_name in parser_names: parser_input_dir = input_dir if len(parser_names) > 1: parser_input_dir = os.path.join(parser_input_dir, parser_name) statements[parser_name] = supported_parsers[parser_name]( parser_input_dir).parse() if not statements[parser_name]: logger.error( f"Not activities found with parser[{parser_name}]. Please, check the statement files." ) raise SystemExit(1) logger.info(f"Generating statements file.") for_each_parser( export_statements, statements, filename="statements.csv", output_dir=output_dir, ) logger.info(f"Populating exchange rates.") for_each_parser(populate_exchange_rates, statements, use_bnb=use_bnb) logger.info(f"Calculating dividends information.") dividends = for_each_parser(calculate_dividends, statements) merged_dividends = merge_dict_of_dicts(dividends) dividend_taxes = calculate_dividends_tax(merged_dividends) logger.info(f"Generating [app8-part4-1.csv] file.") export_app8_part4_1(os.path.join(output_dir, "app8-part4-1.csv"), dividend_taxes) parsers_calculations = None merged_sales = None remaining_purchases = None unsupported_activity_types = get_unsupported_activity_types( supported_parsers, statements) if len(unsupported_activity_types) == 0: logger.info(f"Calculating sales information.") parsers_calculations = for_each_parser(calculate_sales, statements) logger.info(f"Generating [app5-table2.csv] file.") sales = { parser_name: parser_calculations[0] for parser_name, parser_calculations in parsers_calculations.items() } purchases = { parser_name: parser_calculations[1] for parser_name, parser_calculations in parsers_calculations.items() } merged_sales = merge_dict_of_lists(sales) merged_purchases = merge_dict_of_dicts(purchases) remaining_purchases = calculate_remaining_purchases(merged_purchases) export_app5_table2(os.path.join(output_dir, "app5-table2.csv"), merged_sales) logger.info(f"Generating [dec50_2020_data.xml] file.") export_to_xml( os.path.join(output_dir, "dec50_2020_data.xml"), dividend_taxes, merged_sales if merged_sales is not None else None, remaining_purchases if remaining_purchases is not None else None, ) if remaining_purchases is not None: logger.info(f"Generating [app8-part1.csv] file.") export_app8_part1(os.path.join(output_dir, "app8-part1.csv"), remaining_purchases) if merged_sales is not None: win_loss, win_loss_in_currency = calculate_win_loss(merged_sales) if in_currency: logger.info(f"Profit/Loss: {win_loss_in_currency} USD.") logger.info(f"Profit/Loss: {win_loss} lev.") if len(unsupported_activity_types) > 0: logger.warning( f"Statements contain unsupported activity types: {unsupported_activity_types}. Only dividends related data was calculated." )