34 lines
3.4 KiB
Python
34 lines
3.4 KiB
Python
from pathlib import Path
|
|
import json,hashlib,datetime,unicodedata,zipfile,xml.etree.ElementTree as ET
|
|
root=Path(__file__).resolve().parents[2];out=Path(__file__).resolve().parent
|
|
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
|
|
norm=lambda s:unicodedata.normalize('NFKD',s.casefold()).encode('ascii','ignore').decode()
|
|
old=root/'outputs/investmoz-fintech-correspondencia-20261015/INVENTARIO-ACERVO-ACESSIVEL-LOCAL.json';previous=json.loads(old.read_text('utf-8'))
|
|
attachments=Path('C:/Users/macbook/.codex/attachments');current=[{'path':str(p),'bytes':p.stat().st_size,'suffix':p.suffix.lower()}for p in sorted(attachments.rglob('*'))if p.is_file()]
|
|
inventory={'at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'corpus_inventory_reused':{'path':str(old.relative_to(root)),'sha256':sha(old),'at':previous['at'],'root':previous['root'],'files':len(previous['files']),'fresh_full_rescan_claimed':False},'corpus_files':previous['files'],'current_instruction_attachments':current,'note':'Whole existing corpus inventory retained before selecting park sources; no functional-use filter applied to the retained inventory. Selected exemplars are independently hashed below. Originals remain private.'}
|
|
(out/'INVENTARIO-ACERVO.json').write_text(json.dumps(inventory,ensure_ascii=False,indent=2)+'\n','utf-8')
|
|
def path_of(r):
|
|
return Path(r.get('path') or r.get('absolute_path') or Path(previous['root'])/r.get('relative_path',r.get('relative','')))
|
|
candidates=[]
|
|
for r in previous['files']:
|
|
p=path_of(r)
|
|
if any(k in norm(str(p)) for k in ['parques industriais','parque industrial','mozparks','beluluane','munhava','topuito','zonas francas','zonas economicas','special economic','free zone','industrial park']) and p.suffix.lower()in ['.docx','.pdf','.xlsx','.doc','.txt','.html']:
|
|
candidates.append(p)
|
|
for extra in ['database/data/investment-zones-initial.json','database/data/industrial-parks-reconciliation.json','database/data/industrial-park-candidate-drafts.json','docs/investmoz/zones/industrial-parks-reconciliation-2026-10-04.md']:
|
|
candidates.append(root/extra)
|
|
selected=[]
|
|
for p in dict.fromkeys(candidates):
|
|
if not p.is_file():selected.append({'path':str(p),'available':False});continue
|
|
r={'path':str(p),'available':True,'bytes':p.stat().st_size,'sha256':sha(p),'suffix':p.suffix.lower()}
|
|
if p.suffix.lower()=='.docx':
|
|
try:
|
|
with zipfile.ZipFile(p)as z:
|
|
tree=ET.fromstring(z.read('word/document.xml'));ns={'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}
|
|
r['paragraphs']=[''.join(x.itertext()) for x in []]
|
|
r['paragraphs']=[''.join(n.text or ''for n in para.findall('.//w:t',ns))for para in tree.findall('.//w:p',ns)]
|
|
r['embedded']=[{'entry':i.filename,'bytes':i.file_size,'CRC32':format(i.CRC,'08x')}for i in z.infolist()if not i.is_dir()and(i.filename.startswith('word/media/')or i.filename.startswith('word/embeddings/'))]
|
|
except Exception as e:r['extraction_error']=type(e).__name__
|
|
selected.append(r)
|
|
(out/'FONTES-LEVANTADAS.json').write_text(json.dumps({'selection_basis':'Names identify candidate documents only; no master relationship is inferred from name similarity','sources':selected},ensure_ascii=False,indent=2)+'\n','utf-8')
|
|
print(json.dumps({'corpus_files_in_existing_inventory':len(previous['files']),'attachments_current':len(current),'selected_sources':[{'path':r['path'],'bytes':r.get('bytes'),'available':r['available'],'sha256':r.get('sha256'),'paragraphs':len(r.get('paragraphs',[]))}for r in selected]},ensure_ascii=True))
|