2025-04-06 01:32:55 -04:00
|
|
|
#! /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())
|
|
|
|
|
2025-04-06 16:50:07 -04:00
|
|
|
if not args.dry_run:
|
|
|
|
db.add(outlook)
|
2025-04-06 01:32:55 -04:00
|
|
|
|
2025-04-06 16:50:07 -04:00
|
|
|
for probability in outlook.probabilities:
|
|
|
|
probability.outlook_id = outlook.id
|
|
|
|
db.add(probability)
|
2025-04-06 01:32:55 -04:00
|
|
|
|
2025-04-06 16:50:07 -04:00
|
|
|
for category in outlook.categories:
|
|
|
|
category.outlook_id = outlook.id
|
2025-04-06 01:32:55 -04:00
|
|
|
|
2025-04-06 16:50:07 -04:00
|
|
|
db.add(category)
|
2025-04-06 01:32:55 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2025-04-06 16:50:07 -04:00
|
|
|
if not args.dry_run:
|
|
|
|
db.commit()
|