"""Create a synthetic demo workspace; optionally import masked CSV references. Run with CDA_DATA_DIR set to the service's absolute data directory. """ import argparse import json import sys from pathlib import Path ROOT=Path(__file__).resolve().parents[1] sys.path.insert(0,str(ROOT/'tools-service')) from toolsvc.db import database from toolsvc.objects import store from toolsvc.runs import runs def main(): parser=argparse.ArgumentParser() parser.add_argument('--masked-dir',type=Path) args=parser.parse_args() existing={w['name']:w for w in store.list_workspaces()} name='合成样本演示(可复现)' if name in existing: workspace=existing[name] else: workspace=store.create_workspace(name,'使用合成样本演示原始数据处理、统计问答和行级溯源;不代表真实贸易数据。') source=store.register_upload(workspace['id'],'合成演示_small.csv',(ROOT/'samples/small.csv').read_bytes()) run=runs.create(workspace['id'],source['id'],options={'demo':True}) import time while run['status'] in {'queued','running'}: time.sleep(.3);run=runs.get(run['id']) if run['status']!='succeeded':raise RuntimeError(run.get('error')) imported=[] if args.masked_dir: name='真实统计参考(已遮掩)' if name not in existing: masked=store.create_workspace(name,'2026年11月11日结果:16张已遮掩统计表。金额数量中的区间不是精确值,不可取中点或对重复层级求和。此处没有原始申报明细。') for path in sorted(args.masked_dir.glob('*.csv')): item=store.register_upload(masked['id'],'【已遮掩】'+path.name,path.read_bytes(),kind='stat',producer='import_masked_reference') imported.append({'name':item['name'],'rows':item['rows']}) print(json.dumps({'synthetic_workspace':workspace['id'],'masked_imports':imported},ensure_ascii=False,indent=2)) if __name__=='__main__': main()