""" test_boxoffice.py — test isolato per BOXOFFICE. Primo run: costruisce df_anagr e df_diritti dai parquet e li salva in cache. Run successivi: carica la cache (secondi invece di minuti). Uso: python test_boxoffice.py # usa cache se esiste python test_boxoffice.py --no-cache # rigenera sempre la cache python test_boxoffice.py --dry-run # mostra righe senza scrivere """ import argparse import logging import pickle import sys from pathlib import Path # -- WriteToAccess Lego brick ------------------------------------------------ _PIPELINE_DIR = Path(__file__).parent.parent if str(_PIPELINE_DIR) not in sys.path: sys.path.insert(0, str(_PIPELINE_DIR)) from WriteToAccess import df_to_access from core.custom_emesso import build as build_emesso from core.custom_anagrafica import build as build_anagrafica from core.custom_diritti import build as build_diritti from core.boxoffice import build as build_boxoffice import json logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%H:%M:%S') log = logging.getLogger(__name__) _SCRIPT_DIR = Path(__file__).parent _SETTINGS = _SCRIPT_DIR / 'config' / 'settings.json' _TYFX_CSV = str(_SCRIPT_DIR / 'config' / 'emesso_tyfx0160.csv') _CACHE_ANAGR = _SCRIPT_DIR / '_cache_anagr.pkl' _CACHE_DIR = _SCRIPT_DIR / '_cache_diritti.pkl' _CACHE_EM = _SCRIPT_DIR / '_cache_emesso.pkl' def main(): parser = argparse.ArgumentParser() parser.add_argument('--no-cache', action='store_true', help='Rigenera cache') parser.add_argument('--dry-run', action='store_true', help='Non scrive su Access') args = parser.parse_args() with open(_SETTINGS, encoding='utf-8') as f: cfg = json.load(f) parquet_dir = Path(cfg['parquet_dir']) db_warehouse = cfg.get('db_warehouse', '') db_path = Path(cfg['output_db']) # ------------------------------------------------------------------ # # Carica o costruisci df_anagr, df_diritti, df_emesso # # ------------------------------------------------------------------ # use_cache = (not args.no_cache and _CACHE_ANAGR.exists() and _CACHE_DIR.exists() and _CACHE_EM.exists()) if use_cache: log.info('Carico cache da disco...') with open(_CACHE_ANAGR, 'rb') as f: df_anagr, df_ediz, df_super = pickle.load(f) with open(_CACHE_DIR, 'rb') as f: df_diritti = pickle.load(f) with open(_CACHE_EM, 'rb') as f: df_emesso = pickle.load(f) log.info('Cache caricata — ANAGR=%d DIRITTI=%d EMESSO=%d', len(df_anagr), len(df_diritti), len(df_emesso)) else: log.info('Build df_emesso...') df_emesso = build_emesso(parquet_dir, _TYFX_CSV, db_warehouse) log.info(' %d righe', len(df_emesso)) log.info('Build df_anagr...') df_anagr, df_ediz, df_super = build_anagrafica(parquet_dir, _TYFX_CSV) log.info(' ANAGR=%d', len(df_anagr)) log.info('Build df_diritti...') df_diritti = build_diritti(parquet_dir, df_emesso) log.info(' %d righe', len(df_diritti)) log.info('Salvo cache su disco...') with open(_CACHE_ANAGR, 'wb') as f: pickle.dump((df_anagr, df_ediz, df_super), f) with open(_CACHE_DIR, 'wb') as f: pickle.dump(df_diritti, f) with open(_CACHE_EM, 'wb') as f: pickle.dump(df_emesso, f) log.info('Cache salvata.') # ------------------------------------------------------------------ # # Build BOXOFFICE # # ------------------------------------------------------------------ # log.info('Build BOXOFFICE...') df_box = build_boxoffice(df_anagr, df_diritti) log.info(' %d righe', len(df_box)) log.info('Colonne: %s', list(df_box.columns)) if not df_box.empty: log.info('Prima riga:\n%s', df_box.iloc[0].to_string()) if args.dry_run: log.info('[dry-run] — nessuna scrittura su Access') return # ------------------------------------------------------------------ # # Scrivi su Access # # ------------------------------------------------------------------ # if not db_path.exists(): log.error('DB non trovato: %s — lancia prima il bat completo', db_path) sys.exit(1) log.info('Scrivo BOXOFFICE su %s ...', db_path) n = df_to_access(df_box, db_path, 'BOXOFFICE') log.info('Importate %d righe.', n) if __name__ == '__main__': main()