From ccd1a0daba9f5d4a68ce4b1d972da7137aa358de Mon Sep 17 00:00:00 2001 From: Place Jean-Marie Date: Mon, 27 Sep 2021 07:31:59 +0200 Subject: [PATCH] refactor templates to zip --- app/pe/pe_tools.py | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/app/pe/pe_tools.py b/app/pe/pe_tools.py index d54de1d85..2e7f03188 100644 --- a/app/pe/pe_tools.py +++ b/app/pe/pe_tools.py @@ -170,6 +170,15 @@ def add_local_file_to_zip(zipfile, ziproot, pathname, path_in_zip): # zipfile.writestr(rooted_path_in_zip, data) +def add_refs_to_register(register, directory): + """Ajoute les fichiers trouvés dans directory au registre (dictionaire) sous la forme + filename => pathname + """ + length = len(directory) + for pathname in list_directory_filenames(directory): + filename = pathname[length+1:] + register[filename] = pathname + def add_pe_stuff_to_zip(zipfile, ziproot): """Add auxiliary files to (already opened) zip Put all local files found under config/doc_poursuites_etudes/local @@ -178,30 +187,16 @@ def add_pe_stuff_to_zip(zipfile, ziproot): Also copy logos """ + register = {} + # first add standard (distrib references) distrib_dir = os.path.join(REP_DEFAULT_AVIS, "distrib") - distrib_pathnames = list_directory_filenames( - distrib_dir - ) # eg /opt/scodoc/tools/doc_poursuites_etudes/distrib/modeles/toto.tex - l = len(distrib_dir) - distrib_filenames = {x[l + 1 :] for x in distrib_pathnames} # eg modeles/toto.tex - + add_refs_to_register(register=register, directory=distrib_dir) + # then add local references (some oh them may overwrite distrib refs) local_dir = os.path.join(REP_LOCAL_AVIS, "local") - local_pathnames = list_directory_filenames(local_dir) - l = len(local_dir) - local_filenames = {x[l + 1 :] for x in local_pathnames} - - for filename in distrib_filenames | local_filenames: - if filename in local_filenames: - add_local_file_to_zip( - zipfile, ziproot, os.path.join(local_dir, filename), "avis/" + filename - ) - else: - add_local_file_to_zip( - zipfile, - ziproot, - os.path.join(distrib_dir, filename), - "avis/" + filename, - ) + add_refs_to_register(register=register, directory=local_dir) + # at this point register contains all refs (filename, pathname) to be saved + for filename, pathname in register.items(): + add_local_file_to_zip(zipfile, ziproot, pathname, "avis/" + filename) # Logos: (add to logos/ directory in zip) logos_names = ["logo_header.jpg", "logo_footer.jpg"]