41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
"""Export local workbook details or import a private quote snapshot into SQLite."""
|
|
|
|
import argparse
|
|
from contextlib import closing
|
|
import json
|
|
from pathlib import Path
|
|
import sqlite3
|
|
import sys
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from logistics_chat.quote_data import import_snapshot
|
|
from logistics_chat.quote_workbook import export_snapshot
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
source = parser.add_mutually_exclusive_group(required=True)
|
|
source.add_argument('--xlsx', type=Path)
|
|
source.add_argument('--snapshot', type=Path)
|
|
parser.add_argument('--output', type=Path, help='New private snapshot output for --xlsx')
|
|
parser.add_argument('--db', type=Path)
|
|
parser.add_argument('--activate', action='store_true', help='Use this version as general US rates without expiry')
|
|
args = parser.parse_args()
|
|
if args.xlsx:
|
|
if not args.output or args.db or args.output.exists():
|
|
parser.error('--xlsx requires a new --output path, without --db')
|
|
snapshot = export_snapshot(args.xlsx)
|
|
with args.output.open('x', encoding='utf-8') as target:
|
|
json.dump(snapshot, target, ensure_ascii=False, separators=(',', ':'))
|
|
print(json.dumps({key:len(snapshot[key]) for key in ('rates','warehouses','postcodes','documents')}))
|
|
return
|
|
if not args.db or not args.db.is_file() or not args.snapshot.is_file() or args.snapshot.stat().st_size > 20*1024*1024:
|
|
parser.error('Import requires an existing database and a private snapshot smaller than 20 MB')
|
|
snapshot = json.loads(args.snapshot.read_text(encoding='utf-8-sig'))
|
|
with closing(sqlite3.connect(args.db)) as connection, connection:
|
|
print(json.dumps(import_snapshot(connection, snapshot, activate=args.activate)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|