24 lines
1.2 KiB
Python
24 lines
1.2 KiB
Python
"""db2tsv.py table.db2 table.dbd out.tsv -> catalogue TSV (index<TAB>Name=value...) using the layout matching the file hash."""
|
|
import re,struct,sys
|
|
sys.path.insert(0,'/home/praxis/.claude/jobs/8f9a0552/tmp'); import wdc5
|
|
db2,dbd,out=sys.argv[1:4]
|
|
h='%08X'%struct.unpack_from('<I',open(db2,'rb').read(),156)[0]
|
|
lines=open(dbd).read().split('\n'); i=next(k for k,l in enumerate(lines) if l.startswith('LAYOUT') and h in l)
|
|
cols=[]
|
|
for l in lines[i+1:]:
|
|
if not l.strip(): break
|
|
if l.startswith(('BUILD','COMMENT','LAYOUT')): continue
|
|
m=re.match(r'(\$([\w,]+)\$)?(\w+)',l); ann=(m[2] or '').split(','); cols.append((m[3],ann))
|
|
rec=[c for c,a in cols if 'noninline' not in a]
|
|
rel=[]; nid=[c for c,a in cols if 'noninline' in a]
|
|
R=wdc5.rows(db2)
|
|
with open(out,'w') as f:
|
|
for k,(rid,v) in enumerate(sorted(R.items())):
|
|
vals=[x for x in v if not (isinstance(x,tuple) and x[0]=='rel')]; rv=[x[1] for x in v if isinstance(x,tuple) and x[0]=='rel']
|
|
assert len(vals)==len(rec),(db2,len(vals),rec)
|
|
d=dict(zip(rec,vals))
|
|
for c in nid: d[c]=rid
|
|
for c,x in zip(rel,rv): d[c]=x
|
|
f.write(str(k)+'\t'+'\t'.join(f'{c}={d[c]}' for c,_ in cols if c in d)+'\n')
|
|
print(out,len(R),'relation cols',rel)
|