import csv, os, re from decimal import Decimal, ROUND_HALF_UP def find(p): for base in ('.', os.path.dirname(os.path.abspath(__file__)), os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')): c = os.path.join(base, p) if os.path.exists(c): return c raise FileNotFoundError(p) specs = {r['sku']: r for r in csv.DictReader(open(find('inputs/S1-manufacturer-specs.csv'), encoding='utf-8'))} offers = list(csv.DictReader(open(find('inputs/S2-store-offers.csv'), encoding='utf-8'))) BUDGET = Decimal('2200'); TAX = Decimal('0.12'); DEADLINE = '2026-10-23' # S3-store-correction.md: O6 guaranteed arrival is October 27, supersedes S2 delivery field corrections = {'O6': ('2026-10-27', 'guaranteed', 'S3-store-correction.md (supersedes S2 delivery field for O6)')} q = lambda d: d.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) rows = [] for o in offers: s = specs[o['sku']] sub = Decimal(o['price_cad']) + Decimal(o['shipping_cad']) tax = q(sub * TAX); total = q(sub + tax) date, dtype, dsrc = corrections.get(o['offer_id'], (o['delivery'], o['delivery_type'], 'S2-store-offers.csv row ' + o['offer_id'])) fails, unknowns = [], [] budget_ok = total <= BUDGET if not budget_ok: fails.append('Over budget by CAD %s' % q(total - BUDGET)) ram_ok = int(s['ram_gb']) >= 32 if not ram_ok: fails.append('RAM %s GB < 32 GB per S1 (store title claim of 32 GB not verified)' % s['ram_gb']) ssd_ok = int(s['ssd_gb']) >= 1000 if not ssd_ok: fails.append('SSD %s GB < 1 TB per S1 (store title claim of 1 TB not verified)' % s['ssd_gb']) vram_ok = int(s['vram_gb']) >= 8 if not vram_ok: fails.append('VRAM %s GB < 8 GB per S1 (store title claim of 8 GB not verified)' % s['vram_gb']) os_ok = s['os'] == 'Windows 11' if not os_ok: fails.append('OS not Windows 11') if dtype == 'guaranteed': deliv = 'yes' if date <= DEADLINE else 'no' if deliv == 'no': fails.append('Guaranteed arrival %s is after %s (S3 correction)' % (date, DEADLINE)) else: deliv = 'unconfirmed' fails.append('Delivery by %s not confirmed (estimate only)' % DEADLINE) unknowns.append('Whether store will guarantee %s arrival; estimates are not commitments per 00-source-register.md' % date) verdict = 'QUALIFIES - RECOMMENDED' if not fails else 'REJECT' rows.append({ 'offer_id': o['offer_id'], 'sku': o['sku'], 'model': s['model'], 'store': o['store'], 'price_cad': q(Decimal(o['price_cad'])), 'shipping_cad': q(Decimal(o['shipping_cad'])), 'pretax_subtotal_cad': q(sub), 'tax_12pct_cad': tax, 'landed_total_cad': total, 'budget_headroom_cad': q(BUDGET - total), 'within_budget_2200': 'yes' if budget_ok else 'no', 'condition': 'new (00-source-register.md)', 'ram_gb_S1': s['ram_gb'], 'ssd_gb_S1': s['ssd_gb'], 'gpu_S1': s['gpu'], 'vram_gb_S1': s['vram_gb'], 'weight_kg_S1': s['weight_kg'], 'os_S1': s['os'], 'meets_ram_32': 'yes' if ram_ok else 'no', 'meets_ssd_1tb': 'yes' if ssd_ok else 'no', 'meets_vram_8': 'yes' if vram_ok else 'no', 'meets_windows_11': 'yes' if os_ok else 'no', 'effective_delivery_date': date, 'delivery_type': dtype, 'delivery_source': dsrc, 'confirmed_by_2026-10-23': deliv, 'store_listing_note_S2': o['listing_note'], 'hard_failures': '; '.join(fails) or 'none', 'unknowns': '; '.join(unknowns) or 'none', 'independent_test_S4': 'One Premiere Pro UHD H.264 export run on this SKU (not comparative)' if o['sku'] == 'F16-32' else 'Not tested in S4', 'verdict': verdict, 'sources': 'S1-manufacturer-specs.csv row %s; S2-store-offers.csv row %s; 00-source-register.md%s' % (o['sku'], o['offer_id'], '; S3-store-correction.md' if o['offer_id'] in corrections else ''), }) out = os.path.join(os.path.dirname(find('inputs/S2-store-offers.csv')), '..', 'output', 'comparison.csv') out = os.path.normpath(out) with open(out, 'w', newline='', encoding='utf-8') as f: w = csv.DictWriter(f, fieldnames=list(rows[0].keys())); w.writeheader(); w.writerows(rows) print('wrote', out) for r in rows: print(r['offer_id'], r['sku'], r['pretax_subtotal_cad'], r['tax_12pct_cad'], r['landed_total_cad'], r['verdict'], '|', r['hard_failures']) back = list(csv.DictReader(open(out, encoding='utf-8'))) print('rows read back:', len(back), 'qualifying:', [r['offer_id'] for r in back if r['verdict'].startswith('QUALIFIES')]) rec = os.path.join(os.path.dirname(out), 'recommendation.md') if os.path.exists(rec): txt = open(rec, encoding='utf-8').read() print('recommendation.md words (whitespace split):', len(txt.split())) for r in rows: print(r['offer_id'], 'total cited in recommendation:', str(r['landed_total_cad']) in txt.replace(',', ''))