Add --quiet, --dry-run flags to xmet-afos-ingest
This commit is contained in:
parent
6f553c040b
commit
8acce6d76a
1 changed files with 20 additions and 4 deletions
|
@ -1,6 +1,6 @@
|
||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
import sys
|
import argparse
|
||||||
|
|
||||||
from xmet.db import Database
|
from xmet.db import Database
|
||||||
from xmet.afos import AFOSMessageParser
|
from xmet.afos import AFOSMessageParser
|
||||||
|
@ -27,12 +27,24 @@ def each_chunk(fh, sep: str):
|
||||||
else:
|
else:
|
||||||
yield part.strip()
|
yield part.strip()
|
||||||
|
|
||||||
db = Database.connect(sys.argv[1])
|
parser = argparse.ArgumentParser(
|
||||||
|
description = 'Ingest National Weather Service text bulletin products'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument('--quiet', action='store_true', help='Suppress output')
|
||||||
|
parser.add_argument('--dry-run', action='store_true', help='Do not actually ingest products')
|
||||||
|
|
||||||
|
parser.add_argument('db', help='XMET SQLite3 database')
|
||||||
|
parser.add_argument('afos-text-file', help='AFOS text bulletin product file')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
db = Database.connect(args.db)
|
||||||
db.execute('begin transaction')
|
db.execute('begin transaction')
|
||||||
|
|
||||||
parser = AFOSMessageParser()
|
parser = AFOSMessageParser()
|
||||||
|
|
||||||
for path in sys.argv[2:]:
|
for path in getattr(args, 'afos-text-file'):
|
||||||
with open(path, 'r') as fh:
|
with open(path, 'r') as fh:
|
||||||
for data in each_chunk(fh, '\x01'):
|
for data in each_chunk(fh, '\x01'):
|
||||||
if len(data) == 0:
|
if len(data) == 0:
|
||||||
|
@ -41,6 +53,10 @@ for path in sys.argv[2:]:
|
||||||
try:
|
try:
|
||||||
message = parser.parse(data)
|
message = parser.parse(data)
|
||||||
|
|
||||||
|
if not args.quiet:
|
||||||
|
print(f"Ingesting AFOS file {path}")
|
||||||
|
|
||||||
|
if not args.dry_run:
|
||||||
db.add(message)
|
db.add(message)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Add table
Reference in a new issue