import os os.makedirs('sovereign_grid',exist_ok=True) os.chdir('sovereign_grid') open('README_V16.txt','w').write('SOVEREIGN GRID V16.0 - LOCAL-FIRST PERSONAL MESH\n\nWHY I BUILT THIS:\nBuilt as a passion project for anyone who wants their iPad, iPhone, and Apple Watch to talk directly over home Wi-Fi with zero cloud accounts, subscriptions, or tracking.\n\nWHAT IS NEW IN V16.0:\n- In-browser +Ticker, +Card, Find search, and Spaced-Repetition Quiz.\n- 3-second cellular timeout with cached stock fallback.\n- Live Audit activity log and one-tap local Backup.\n\nHONEST CAVEAT:\n- Keep a-Shell open in the foreground or Split View on your host iPad so iPadOS keeps Port 5002 awake.\n\n3-MINUTE SETUP:\n1. Run this installer in a-Shell on your iPad (your Master Hub).\n2. Open http://127.0.0.1:5002 in Safari to use your dashboard.\n3. Point an Apple Shortcut (Get Contents of URL -> Show Content) at http://:5002/watch for one-tap wrist briefings.\n') open('master_mesh.py','w').write('import os,urllib.parse,recv,watch\nnl=chr(10)\nfrom http.server import *\nclass H(BaseHTTPRequestHandler):\n def do_GET(self):\n u=urllib.parse.urlparse(self.path)\n c=u.path.strip(\'/\'); qs=u.query\n import time; L=open(\'audit_log.txt\').read().splitlines() if os.path.exists(\'audit_log.txt\') else []; L.insert(0,time.strftime(\'%H:%M:%S\')+" | "+self.client_address[0]+" | "+self.path); open(\'audit_log.txt\',\'w\').write(nl.join(L[:30])+nl); self.send_response(200); self.end_headers()\n if not c: self.wfile.write(open(\'dash.html\',\'rb\').read())\n elif c==\'receive\': recv.save(qs); self.wfile.write(b\'OK\')\n elif c==\'stocks\': self.wfile.write(watch.stocks.live().encode())\n elif c==\'watch\': self.wfile.write(watch.brief().encode())\n elif c==\'cards\': self.wfile.write(open(\'study_cards.txt\',\'rb\').read())\n elif c==\'todos\': self.wfile.write(open(\'todos.txt\',\'rb\').read())\n elif c==\'read\': self.wfile.write(open(\'clipboard_notes.txt\',\'rb\').read())\n elif c==\'add\': open(\'todos.txt\',\'a\').write(urllib.parse.unquote(qs).strip()+nl)\n elif c==\'note\': open(\'clipboard_notes.txt\',\'a\').write(urllib.parse.unquote(qs).strip()+nl)\n elif c==\'ticker\': open(\'tickers.txt\',\'a\').write(urllib.parse.unquote(qs).strip().upper()+nl); self.wfile.write(watch.stocks.live().encode())\n elif c==\'card\': open(\'study_cards.txt\',\'a\').write(urllib.parse.unquote(qs).strip()+nl); self.wfile.write(open(\'study_cards.txt\',\'rb\').read())\n elif c==\'del\': m=dict(urllib.parse.parse_qsl(qs)); fn=m[\'f\']; L=open(fn).read().splitlines(); L.pop(int(m[\'i\'])); open(fn,\'w\').write(nl.join(L)+(nl if L else \'\')); self.wfile.write(open(fn,\'rb\').read())\n elif c==\'find\': k=urllib.parse.unquote(qs).lower(); res=[f+" -> "+l for f in (\'todos.txt\',\'clipboard_notes.txt\',\'study_cards.txt\') for l in open(f).read().splitlines() if k in l.lower()]; self.wfile.write((nl.join(res) or \'NO MATCHES\').encode())\n elif c==\'backup\': import shutil,time; d=\'backups/snap_\'+time.strftime(\'%Y%m%d_%H%M%S\'); os.makedirs(d,exist_ok=True); [shutil.copy(f,d) for f in os.listdir(\'.\') if os.path.isfile(f)]; self.wfile.write(b\'BACKUP SAVED: \'+d.encode())\n elif c==\'audit\': self.wfile.write((open(\'audit_log.txt\',\'rb\').read() if os.path.exists(\'audit_log.txt\') else b\'NO LOGS YET\'))\nHTTPServer.allow_reuse_address=True\nprint(\'🚀 [MASTER MESH V16.0]: Online on Port 5002. [A_STABLE]\')\nHTTPServer((\'0.0.0.0\',5002),H).serve_forever()\n') open('dash.html','w').write('

Sovereign Grid V16.0

')
open('recv.py','w').write("import urllib.parse as p\ndef save(qs):\n    d=dict(p.parse_qsl(qs))\n    open('todos.txt','w').write(d.get('t',''))\n    open('clipboard_notes.txt','w').write(d.get('n',''))\n    open('tickers.txt','w').write(d.get('k',''))\n    open('study_cards.txt','w').write(d.get('c',''))\n")
open('stocks.py','w').write('import urllib.request as u,json,os\ncf=\'stock_cache.json\'\nc=json.loads(open(cf).read()) if os.path.exists(cf) else {}\ns=chr(47)\nh=\'https:\'+s+s+\'query1.finance.yahoo.com\'+s+\'v8\'+s+\'finance\'+s+\'chart\'+s\ndef quote(t):\n    try:\n        r=u.Request(h+t+\'?interval=1d\',headers={\'User-Agent\':\'Mozilla/5.0\'})\n        d=json.loads(u.urlopen(r,timeout=3).read()).get(\'chart\').get(\'result\').pop(0).get(\'meta\')\n        p=str(d.get(\'regularMarketPrice\')); c[t]=p; open(cf,\'w\').write(json.dumps(c)); return t+": $"+p\n    except Exception: return t+": $"+c.get(t,\'offline\')+" (cached)"\ndef live():\n    ts=list(filter(None,(x.strip() for x in open(\'tickers.txt\').read().splitlines())))\n    return chr(10).join(map(quote,ts))\n')
open('watch.py','w').write("import stocks\ndef brief():\n    t=open('todos.txt').read().strip() or 'ALL CLEAR'\n    n=open('clipboard_notes.txt').read().strip() or 'NO NOTES'\n    return 'TASKS:'+chr(10)+t+chr(10)+chr(10)+'NOTES:'+chr(10)+n+chr(10)+chr(10)+'STOCKS:'+chr(10)+stocks.live()\n")
open('todos.txt','w').write('Explore Sovereign Grid V16.0'+chr(10))
open('clipboard_notes.txt','w').write('Welcome to your local-first personal mesh!'+chr(10))
open('tickers.txt','w').write('AAPL'+chr(10))
open('study_cards.txt','w').write('What makes a mesh local-first?|Your devices talk directly over home Wi-Fi with zero cloud middleman.'+chr(10))
exec(open('master_mesh.py').read())