diff --git a/bin/xmet-spc-ingest b/bin/xmet-spc-ingest new file mode 100755 index 0000000..7710c3b --- /dev/null +++ b/bin/xmet-spc-ingest @@ -0,0 +1,49 @@ +#! /usr/bin/env python3 + +import argparse + +from xmet.config import Config +from xmet.db import Database +from xmet.spc import SPCOutlookParser + +argparser = argparse.ArgumentParser(description='Ingest SPC outlook text products') +argparser.add_argument('--dry-run', action='store_true', help='Do not actually ingest products') +argparser.add_argument('--verbose', action='store_true', help='Report each action taken') +argparser.add_argument('--ignore-errors', action='store_true', help='Continue after failing to ingest a file') +argparser.add_argument('path', nargs='+') + +args = argparser.parse_args() + +config = Config.load() +db = Database.from_config(config) + +db.execute('begin transaction') + +for path in args.path: + parser = SPCOutlookParser() + + with open(path, 'r') as fh: + try: + outlook = parser.parse(fh.read()) + + db.add(outlook) + + for probability in outlook.probabilities: + probability.outlook_id = outlook.id + db.add(probability) + + for category in outlook.categories: + category.outlook_id = outlook.id + + db.add(category) + + if args.verbose: + print(f"Ingested {path}") + except Exception as e: + if args.ignore_errors: + if args.verbose: + print(f"Failed to ingest {path}: {e}") + else: + raise + +db.commit()