From 9f269b1aa68d90c65accd0de71aa906998f19cb2 Mon Sep 17 00:00:00 2001
From: XANTRONIX Industrial <xan@xantronix.com>
Date: Sun, 6 Apr 2025 01:32:55 -0400
Subject: [PATCH] Initial commit of bin/xmet-spc-ingest

---
 bin/xmet-spc-ingest | 49 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100755 bin/xmet-spc-ingest

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()