Add basic main
This commit is contained in:
82
src/main.py
82
src/main.py
@@ -0,0 +1,82 @@
|
||||
import asyncio
|
||||
import argparse
|
||||
|
||||
import tomllib
|
||||
|
||||
from base.rules import DummyRuleInterface, JSONRuleInterface, RuleSet
|
||||
from converters.wise_converter import WiseConverter
|
||||
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'converter',
|
||||
help="Which converter to use. Available converters: 'wise'"
|
||||
)
|
||||
parser.add_argument(
|
||||
'--config',
|
||||
dest='config',
|
||||
help="Ingest configuration file. Should be a toml file with a section for the selected converter."
|
||||
)
|
||||
parser.add_argument(
|
||||
'--ingest',
|
||||
dest='ingest',
|
||||
help="File to ingest (must be in the correct format for the selected converter)"
|
||||
)
|
||||
parser.add_argument(
|
||||
'-o', '--output',
|
||||
dest='output',
|
||||
default=None,
|
||||
help="Path to output file to append processed transactions. If not given will use stdout."
|
||||
)
|
||||
parser.add_argument(
|
||||
'--rules',
|
||||
dest='rules',
|
||||
default=None,
|
||||
help="Path to JSON file with parsing rules. If not given, will use in-memory ruleset, with no saving."
|
||||
)
|
||||
|
||||
|
||||
|
||||
async def main():
|
||||
args = parser.parse_args()
|
||||
converter = args.converter
|
||||
ingest_file = args.ingest
|
||||
out_path = args.output
|
||||
rule_path = args.rules
|
||||
config_path = args.config
|
||||
|
||||
# Create converter config
|
||||
# TODO: Will need to move to tomlkit or other write-capable library
|
||||
with open(config_path, "rb") as f:
|
||||
root_config = tomllib.load(f)
|
||||
|
||||
# TODO: converter factory
|
||||
converter_cls = WiseConverter
|
||||
converter_config = converter_cls.config_type.from_dict(root_config[converter_cls.config_field])
|
||||
converter = converter_cls(converter_config)
|
||||
|
||||
# TODO: rule interface factory
|
||||
# Also dummy plug when no rules given
|
||||
if rule_path:
|
||||
rule_interface = JSONRuleInterface(converter_cls.qual_name(), path=rule_path)
|
||||
else:
|
||||
rule_interface = DummyRuleInterface(converter_cls.qual_name())
|
||||
ruleset = await RuleSet.load_from(rule_interface)
|
||||
|
||||
print("Finished configuration. Loading records")
|
||||
records = converter.ingest_file(ingest_file)
|
||||
print(f"Loaded {len(records)} records.")
|
||||
partials = [converter.convert(record, ruleset) for record in records]
|
||||
print(f"Converted {len(partials)} partial transactions.")
|
||||
for partial in partials:
|
||||
print(repr(partial))
|
||||
for record, partial in zip(records, partials):
|
||||
print('\n'.join(record.display_table()))
|
||||
print(repr(partial))
|
||||
print('-'*10)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user