Feat: rexrite produce_and_compile
This commit is contained in:
		| @@ -169,81 +169,68 @@ def texcompile(filename): | |||||||
| def produce_and_compile(options): | def produce_and_compile(options): | ||||||
|     """ Produce and compile subjects |     """ Produce and compile subjects | ||||||
|     """ |     """ | ||||||
|     working_dir = get_working_dir(options) |     logger.debug(f"CI parser gets {options}") | ||||||
|  |  | ||||||
|     if options["only_corr"]: |     template = Path(options["template"]).name | ||||||
|         options["corr"] = True |     directory = Path(options["template"]).parent | ||||||
|         tex_files = working_dir.glop("[0-9]*_*.tex") |     metadatas = subject_metadatas(options) | ||||||
|     else: |     logger.debug(f"Metadata {metadatas}") | ||||||
|         template = Path(options["template"]).name |  | ||||||
|         logger.debug(f"Template will be {template}") |  | ||||||
|  |  | ||||||
|         list_infos = subject_metadatas(options) |     for meta in metadatas: | ||||||
|  |         logger.debug(f"Feeding template toward {meta['texfile']}") | ||||||
|  |         if options["crazy"]: | ||||||
|  |             crazy_feed( | ||||||
|  |                 template=Path(meta["directory"]) / meta["template"], | ||||||
|  |                 data=meta, | ||||||
|  |                 output=meta["texfile"], | ||||||
|  |                 force=1, | ||||||
|  |             ) | ||||||
|  |         else: | ||||||
|  |             feed( | ||||||
|  |                 template=Path(meta["directory"]) / meta["template"], | ||||||
|  |                 data=meta, | ||||||
|  |                 output=meta["texfile"], | ||||||
|  |                 force=1, | ||||||
|  |             ) | ||||||
|  |         assert(Path(meta["texfile"]).exists()) | ||||||
|  |         logger.debug(f"{meta['texfile']} fed") | ||||||
|  |  | ||||||
|         logger.debug(f"Metadata {list_infos}") |         if options["corr"]: | ||||||
|  |             logger.debug(f"Building correction for {meta['texfile']}") | ||||||
|         tex_files = [] |             meta.update({ | ||||||
|         for infos in list_infos: |                 "corr_texfile": activate_printanswers(meta["texfile"]), | ||||||
|             dest = working_dir / Path(template.replace("tpl", infos["num"])) |             }) | ||||||
|             logger.debug(f"Feeding template toward {dest}") |  | ||||||
|             tex_files.append(dest) |  | ||||||
|             if options["crazy"]: |  | ||||||
|                 while True: |  | ||||||
|                     try: |  | ||||||
|                         pytex.feed( |  | ||||||
|                             working_dir / template, |  | ||||||
|                             {"infos": infos}, |  | ||||||
|                             output=dest, |  | ||||||
|                             force=1, |  | ||||||
|                         ) |  | ||||||
|                     except: |  | ||||||
|                         pass |  | ||||||
|                     else: |  | ||||||
|                         break |  | ||||||
|             else: |  | ||||||
|                 pytex.feed( |  | ||||||
|                     working_dir / template, {"infos": infos}, output=dest, force=1 |  | ||||||
|                 ) |  | ||||||
|             logger.debug(f"{dest} fed") |  | ||||||
|  |  | ||||||
|         if not options["no_compile"]: |         if not options["no_compile"]: | ||||||
|             pdf_files = [] |             for prefix in ["", "corr_"]: | ||||||
|             for texfile in tex_files: |                 key = prefix + "texfile" | ||||||
|                 logger.debug(f"Start compiling {texfile}") |                 try: | ||||||
|                 pytex.pdflatex(texfile) |                     meta[key] | ||||||
|                 logger.debug(f"End compiling {texfile}") |                 except KeyError: | ||||||
|                 pdf_files.append(str(texfile).split(".")[0] + ".pdf") |                     pass | ||||||
|             logger.debug(f"Compiled files : {pdf_files}") |                 else: | ||||||
|  |                     texcompile(meta[key]) | ||||||
|  |                     meta.update({ | ||||||
|  |                         prefix+'pdffile': meta[key].replace('tex', 'pdf') | ||||||
|  |                     }) | ||||||
|  |  | ||||||
|         if not options["no_join"] and not options["no_compile"]: |     if not options["no_join"]: | ||||||
|             pdfjoin( |         for prefix in ["", "corr_"]: | ||||||
|                 pdf_files, |             key = prefix + "pdffile" | ||||||
|                 template.replace("tpl", "all").replace(".tex", ".pdf"), |             try: | ||||||
|                 working_dir, |                 pdfs = [m[key] for m in metadatas] | ||||||
|                 rm_pdfs=1, |             except KeyError: | ||||||
|             ) |                 pass | ||||||
|  |             else: | ||||||
|     if options["corr"]: |                 pdfjoin( | ||||||
|         pdf_files = [] |                     pdfs, | ||||||
|         for texfile in tex_files: |                     template.replace("tpl", prefix+"all").replace(".tex", ".pdf"), | ||||||
|             corr_fname = activate_printanswers(texfile) |                     directory, | ||||||
|             if not options["no_compile"]: |                     rm_pdfs=1, | ||||||
|                 logger.debug(f"Start compiling {texfile}") |                 ) | ||||||
|                 pytex.pdflatex(corr_fname) |  | ||||||
|                 logger.debug(f"End compiling {texfile}") |  | ||||||
|                 pdf_files.append(str(corr_fname).split(".")[0] + ".pdf") |  | ||||||
|                 deactivate_printanswers(corr_fname) |  | ||||||
|  |  | ||||||
|         if not options["no_join"] and not options["no_compile"]: |  | ||||||
|             pdfjoin( |  | ||||||
|                 pdf_files, |  | ||||||
|                 template.replace("tpl", "corr").replace(".tex", ".pdf"), |  | ||||||
|                 working_dir, |  | ||||||
|                 rm_pdfs=1, |  | ||||||
|             ) |  | ||||||
|  |  | ||||||
|     if not options["dirty"]: |     if not options["dirty"]: | ||||||
|         pytex.clean(working_dir) |         clean(directory) | ||||||
|  |  | ||||||
|  |  | ||||||
| # ----------------------------- | # ----------------------------- | ||||||
|   | |||||||
| @@ -3,18 +3,7 @@ | |||||||
|  |  | ||||||
|  |  | ||||||
| import click | import click | ||||||
| import logging | from .bopytex import produce_and_compile | ||||||
| from pathlib import Path |  | ||||||
| from .bopytex import subject_metadatas, crazy_feed, pdfjoin, feed, clean, texcompile |  | ||||||
|  |  | ||||||
| formatter = logging.Formatter("%(name)s :: %(levelname)s :: %(message)s") |  | ||||||
| steam_handler = logging.StreamHandler() |  | ||||||
| steam_handler.setLevel(logging.DEBUG) |  | ||||||
| steam_handler.setFormatter(formatter) |  | ||||||
| logger = logging.getLogger(__name__) |  | ||||||
| logger.setLevel(logging.DEBUG) |  | ||||||
| logger.addHandler(steam_handler) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @click.command() | @click.command() | ||||||
| @click.argument( | @click.argument( | ||||||
| @@ -87,70 +76,7 @@ def new(**options): | |||||||
|     Feed the template (tpl_...) and then compile it with latex. |     Feed the template (tpl_...) and then compile it with latex. | ||||||
|  |  | ||||||
|     """ |     """ | ||||||
|     logger.debug(f"CI parser gets {options}") |     produce_and_compile(options) | ||||||
|  |  | ||||||
|     template = Path(options["template"]).name |  | ||||||
|     directory = Path(options["template"]).parent |  | ||||||
|     metadatas = subject_metadatas(options) |  | ||||||
|     logger.debug(f"Metadata {metadatas}") |  | ||||||
|  |  | ||||||
|     for meta in metadatas: |  | ||||||
|         logger.debug(f"Feeding template toward {meta['texfile']}") |  | ||||||
|         if options["crazy"]: |  | ||||||
|             crazy_feed( |  | ||||||
|                 template=Path(meta["directory"]) / meta["template"], |  | ||||||
|                 data=meta, |  | ||||||
|                 output=meta["texfile"], |  | ||||||
|                 force=1, |  | ||||||
|             ) |  | ||||||
|         else: |  | ||||||
|             feed( |  | ||||||
|                 template=Path(meta["directory"]) / meta["template"], |  | ||||||
|                 data=meta, |  | ||||||
|                 output=meta["texfile"], |  | ||||||
|                 force=1, |  | ||||||
|             ) |  | ||||||
|         assert(Path(meta["texfile"]).exists()) |  | ||||||
|         logger.debug(f"{meta['texfile']} fed") |  | ||||||
|  |  | ||||||
|         if options["corr"]: |  | ||||||
|             logger.debug(f"Building correction for {meta['texfile']}") |  | ||||||
|             meta.update({ |  | ||||||
|                 "corr_texfile": activate_printanswers(meta["texfile"]), |  | ||||||
|             }) |  | ||||||
|  |  | ||||||
|         if not options["no_compile"]: |  | ||||||
|             for prefix in ["", "corr_"]: |  | ||||||
|                 key = prefix + "texfile" |  | ||||||
|                 try: |  | ||||||
|                     meta[key] |  | ||||||
|                 except KeyError: |  | ||||||
|                     pass |  | ||||||
|                 else: |  | ||||||
|                     texcompile(meta[key]) |  | ||||||
|                     meta.update({ |  | ||||||
|                         prefix+'pdffile': meta[key].replace('tex', 'pdf') |  | ||||||
|                     }) |  | ||||||
|  |  | ||||||
|     if not options["no_join"]: |  | ||||||
|         for prefix in ["", "corr_"]: |  | ||||||
|             key = prefix + "pdffile" |  | ||||||
|             try: |  | ||||||
|                 pdfs = [m[key] for m in metadatas] |  | ||||||
|             except KeyError: |  | ||||||
|                 pass |  | ||||||
|             else: |  | ||||||
|                 pdfjoin( |  | ||||||
|                     pdfs, |  | ||||||
|                     template.replace("tpl", prefix+"all").replace(".tex", ".pdf"), |  | ||||||
|                     directory, |  | ||||||
|                     rm_pdfs=1, |  | ||||||
|                 ) |  | ||||||
|  |  | ||||||
|     if not options["dirty"]: |  | ||||||
|         clean(directory) |  | ||||||
|  |  | ||||||
|     # produce_and_compile(options) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|   | |||||||
| @@ -111,6 +111,7 @@ def test_metadatas(prepare_test_template): | |||||||
|                 "classe": "1ST", |                 "classe": "1ST", | ||||||
|                 "elo": "1000", |                 "elo": "1000", | ||||||
|                 "texfile": "01_test.tex", |                 "texfile": "01_test.tex", | ||||||
|  |                 "template": "tpl_test.tex", | ||||||
|                 "directory": ".", |                 "directory": ".", | ||||||
|             }, |             }, | ||||||
|             { |             { | ||||||
| @@ -119,6 +120,7 @@ def test_metadatas(prepare_test_template): | |||||||
|                 "classe": "1ST", |                 "classe": "1ST", | ||||||
|                 "elo": "1300", |                 "elo": "1300", | ||||||
|                 "texfile": "02_test.tex", |                 "texfile": "02_test.tex", | ||||||
|  |                 "template": "tpl_test.tex", | ||||||
|                 "directory": ".", |                 "directory": ".", | ||||||
|             }, |             }, | ||||||
|             { |             { | ||||||
| @@ -127,6 +129,7 @@ def test_metadatas(prepare_test_template): | |||||||
|                 "classe": "1ST", |                 "classe": "1ST", | ||||||
|                 "elo": "100", |                 "elo": "100", | ||||||
|                 "texfile": "03_test.tex", |                 "texfile": "03_test.tex", | ||||||
|  |                 "template": "tpl_test.tex", | ||||||
|                 "directory": ".", |                 "directory": ".", | ||||||
|             }, |             }, | ||||||
|             { |             { | ||||||
| @@ -135,6 +138,7 @@ def test_metadatas(prepare_test_template): | |||||||
|                 "classe": "1ST", |                 "classe": "1ST", | ||||||
|                 "elo": "4000", |                 "elo": "4000", | ||||||
|                 "texfile": "04_test.tex", |                 "texfile": "04_test.tex", | ||||||
|  |                 "template": "tpl_test.tex", | ||||||
|                 "directory": ".", |                 "directory": ".", | ||||||
|             }, |             }, | ||||||
|             { |             { | ||||||
| @@ -143,6 +147,7 @@ def test_metadatas(prepare_test_template): | |||||||
|                 "classe": "1ST", |                 "classe": "1ST", | ||||||
|                 "elo": "1300", |                 "elo": "1300", | ||||||
|                 "texfile": "05_test.tex", |                 "texfile": "05_test.tex", | ||||||
|  |                 "template": "tpl_test.tex", | ||||||
|                 "directory": ".", |                 "directory": ".", | ||||||
|             }, |             }, | ||||||
|         ] |         ] | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user