"""Vendor the official XBRL 2.1 XML schemas for offline internal report validation."""
from pathlib import Path
import urllib.request,urllib.parse,xml.etree.ElementTree as ET,json,hashlib
r=Path(__file__).resolve().parents[1];out=r/'resources/xbrl/schemas';out.mkdir(parents=True,exist_ok=True)
pending=['https://www.xbrl.org/2003/xbrl-instance-2003-12-31.xsd'];visited={}
while pending:
 url=pending.pop();url=url.replace('http://','https://')
 if url in visited:continue
 parsed=urllib.parse.urlparse(url)
 assert parsed.hostname in ['www.xbrl.org','www.w3.org'],url
 name=Path(parsed.path).name;assert name.endswith('.xsd') and len(visited)<15
 data=urllib.request.urlopen(url,timeout=45).read();tree=ET.fromstring(data)
 visited[url]={'file':name,'source_sha256':hashlib.sha256(data).hexdigest()}
 text=data.decode('utf-8-sig')
 for element in tree.iter():
  location=element.get('schemaLocation')
  if location:
   absolute=urllib.parse.urljoin(url,location).replace('http://','https://');pending.append(absolute)
   text=text.replace('schemaLocation="'+location+'"','schemaLocation="'+Path(urllib.parse.urlparse(absolute).path).name+'"')
 (out/name).write_text(text,encoding='utf-8')
 visited[url]['packaged_sha256']=hashlib.sha256((out/name).read_bytes()).hexdigest()
(out/'sources.json').write_text(json.dumps(visited,indent=2),encoding='utf-8')
print('Vendored official schemas:',len(visited))
