diff --git a/src/pif_compiler/functions/common_func.py b/src/pif_compiler/functions/common_func.py index 333dac4..e24703c 100644 --- a/src/pif_compiler/functions/common_func.py +++ b/src/pif_compiler/functions/common_func.py @@ -130,28 +130,27 @@ async def generate_project_source_pdfs(project, output_dir: str = "pdfs") -> lis log.warning(f"PDF tox non generato per {pdf_name}") # --- COSING PDF --- + # Un solo PDF per ingrediente (il primo CosingInfo con reference valida). if ing.cosing_info: - seen_refs = set() - for cosing in ing.cosing_info: - if not cosing.reference or cosing.reference in seen_refs: - continue - seen_refs.add(cosing.reference) + pdf_name = f"{pi.cas}_cosing" + pdf_path = os.path.join(output_dir, f"{pdf_name}.pdf") - pdf_name = f"{pi.cas}_cosing" - pdf_path = os.path.join(output_dir, f"{pdf_name}.pdf") - - if os.path.exists(pdf_path): - generated.append(pdf_path) - continue - - log.info(f"Download COSING PDF: {pdf_name} (reference={cosing.reference})") - content = cosing_download(cosing.reference) - if isinstance(content, bytes): - with open(pdf_path, 'wb') as f: - f.write(content) - generated.append(pdf_path) - else: - log.warning(f"COSING PDF non scaricato per {pdf_name}: {content}") + if os.path.exists(pdf_path): + generated.append(pdf_path) + else: + reference = next( + (c.reference for c in ing.cosing_info if c.reference), + None + ) + if reference: + log.info(f"Download COSING PDF: {pdf_name} (reference={reference})") + content = cosing_download(reference) + if isinstance(content, bytes): + with open(pdf_path, 'wb') as f: + f.write(content) + generated.append(pdf_path) + else: + log.warning(f"COSING PDF non scaricato per {pdf_name}: {content}") log.info(f"Generazione fonti completata: {len(generated)} PDF generati") return generated @@ -190,10 +189,14 @@ def create_sources_zip(pdf_paths: list, zip_path: str) -> str: if zip_dir: os.makedirs(zip_dir, exist_ok=True) + seen_names: set[str] = set() with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf: for path in pdf_paths: - if os.path.exists(path): - zf.write(path, os.path.basename(path)) + name = os.path.basename(path) + if not os.path.exists(path) or name in seen_names: + continue + seen_names.add(name) + zf.write(path, name) log.info(f"ZIP creato: {zip_path} ({len(pdf_paths)} file)") return zip_path