diff --git a/__init__.py b/__init__.py index 7e4a91a..0d0a6da 100644 --- a/__init__.py +++ b/__init__.py @@ -1,26 +1,39 @@ import argparse -from table_handler.serializer import TableSerializer +import os from logger import logger +from table_handler.serializer import TableSerializer import instructions.countings_formater as countings_formater import instructions.ring_formater as ring_formater -def main(): +def process_file(input_file, output_file, formatter, row_instructions): + logger.info(f"Processing: {input_file}") + serializer = TableSerializer( + input_file, + formatter.EXPECTED_INPUT_TABLE_SCHEMA, + formatter.EXPECTED_OUTPUT_TABLE_SCHEMA + ) + + serializer.run( + formatter.COL_INSTRUCTIONS, + row_instructions + ) + + serializer.write_output(output_file) + + +def main(): parser = argparse.ArgumentParser(description="Serialize Excel tables.") - parser.add_argument( - "-i", "--input", - required=True, - help="Input Excel file" - ) + # Single file mode (optional fallback) + parser.add_argument("-i", "--input", help="Input Excel file") + parser.add_argument("-o", "--output", help="Output Excel file") - parser.add_argument( - "-o", "--output", - required=True, - help="Output Excel file" - ) + # Directory mode + parser.add_argument("--input-dir", help="Input directory") + parser.add_argument("--output-dir", help="Output directory") parser.add_argument( "-t", "--type", @@ -31,7 +44,7 @@ def main(): args = parser.parse_args() - # Select formatter based on type + # Select formatter if args.type == "ring": formatter = ring_formater row_instructions = ring_formater.ROW_INSTRUCTIONS @@ -39,23 +52,36 @@ def main(): formatter = countings_formater row_instructions = countings_formater.ROW_INSTRUCTION - logger.info("Reading file, building serializer...") - serializer = TableSerializer( - args.input, - formatter.EXPECTED_INPUT_TABLE_SCHEMA, - formatter.EXPECTED_OUTPUT_TABLE_SCHEMA - ) + # directory + if args.input_dir and args.output_dir: + logger.info("Running in directory mode") - logger.info("Starting transformation") - serializer.run( - formatter.COL_INSTRUCTIONS, - row_instructions - ) + for root, _, files in os.walk(args.input_dir): + for file in files: + if not file.endswith((".xlsx", ".xls")): + continue - logger.info("Writing transformations") - serializer.write_output(args.output) + input_path = os.path.join(root, file) + + # Create corresponding output path + relative_path = os.path.relpath(root, args.input_dir) + output_root = os.path.join(args.output_dir, relative_path) + os.makedirs(output_root, exist_ok=True) + + output_path = os.path.join(output_root, file) + + process_file(input_path, output_path, formatter, row_instructions) + + # single file + elif args.input and args.output: + logger.info("Running in single file mode") + process_file(args.input, args.output, formatter, row_instructions) + + else: + parser.error("You must provide either input/output files or input-dir/output-dir") logger.info("Done") + if __name__ == "__main__": main() \ No newline at end of file diff --git a/instructions/mappings.py b/instructions/mappings.py index 4124da3..051d245 100644 --- a/instructions/mappings.py +++ b/instructions/mappings.py @@ -6,6 +6,7 @@ SPICIES = { "bbar", "Barbastella", "barbastellus", + "Bbararar", "B.b.", "B.b", "BB", @@ -37,6 +38,7 @@ SPICIES = { "P.pip.", "P.pip", "PPIP", + "P.p." "ppip", "Pipistrellus pipistrellus", ], @@ -155,6 +157,7 @@ SPICIES = { "P.nat.", "P.nat", "PNAT", + "Pnatatat", "pnat", "Pipistrellus nathusii", "P.n.", @@ -177,6 +180,7 @@ SPICIES = { "unbekannt", "unb.", "Unbestimmt", + "Mspecbech" ], "Malc": [ "M.alc.", @@ -227,6 +231,7 @@ SPICIES = { "MSPEC", "mspec", "Myotis", + "Mspec.Mspec", "M.spe.", "M.spe", "MSPE", @@ -251,6 +256,7 @@ SPICIES = { "MMYO", "mmyo", "Myotis Myotis", + "Mspec.bech" "M.m.", "M.m", "MM", diff --git a/instructions/ring_formater.py b/instructions/ring_formater.py index 0255715..6508c6a 100644 --- a/instructions/ring_formater.py +++ b/instructions/ring_formater.py @@ -77,7 +77,4 @@ ROW_INSTRUCTIONS = [ "target": "Wiederfunde", "value": "TRUE", } - #{ - # "command": "ROW_COPY_REMAINING" - #} ] \ No newline at end of file