Initial commit of bin/xmet-spc-ingest
This commit is contained in:
parent
4e2bf52ad2
commit
9f269b1aa6
1 changed files with 49 additions and 0 deletions
49
bin/xmet-spc-ingest
Executable file
49
bin/xmet-spc-ingest
Executable file
|
@ -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()
|
Loading…
Add table
Reference in a new issue