Feat: add Integer and Decimal in mapytex_tools

This commit is contained in:
Bertrand Benjamin 2019-10-15 19:50:54 +02:00
parent de8d3e574d
commit f5f9e9193f

View File

@ -12,10 +12,10 @@ import sys
from path import Path from path import Path
import pytex import pytex
from mapytex import Expression from mapytex import Expression, Integer, Decimal
import bopytex.filters as filters import bopytex.filters as filters
formatter = logging.Formatter('%(name)s :: %(levelname)s :: %(message)s') formatter = logging.Formatter("%(name)s :: %(levelname)s :: %(message)s")
steam_handler = logging.StreamHandler() steam_handler = logging.StreamHandler()
steam_handler.setLevel(logging.DEBUG) steam_handler.setLevel(logging.DEBUG)
steam_handler.setFormatter(formatter) steam_handler.setFormatter(formatter)
@ -29,6 +29,8 @@ def setup():
logger.debug(f"Render for Expression is {Expression.RENDER}") logger.debug(f"Render for Expression is {Expression.RENDER}")
mapytex_tools = { mapytex_tools = {
"Expression": Expression, "Expression": Expression,
"Integer": Integer,
"Decimal": Decimal,
# "Polynom": mapytex.Polynom, # "Polynom": mapytex.Polynom,
# "Fraction": mapytex.Fraction, # "Fraction": mapytex.Fraction,
# "Equation": mapytex.Equation, # "Equation": mapytex.Equation,
@ -36,7 +38,7 @@ def setup():
# "random_pythagore": mapytex.random_pythagore, # "random_pythagore": mapytex.random_pythagore,
# "Dataset": mapytex.Dataset, # "Dataset": mapytex.Dataset,
# "WeightedDataset": mapytex.WeightedDataset, # "WeightedDataset": mapytex.WeightedDataset,
} }
pytex.update_export_dict(mapytex_tools) pytex.update_export_dict(mapytex_tools)
pytex.add_filter("calculus", filters.do_calculus) pytex.add_filter("calculus", filters.do_calculus)
@ -50,24 +52,25 @@ def get_working_dir(options):
try: try:
template = Path(options.template) template = Path(options.template)
except TypeError: except TypeError:
raise ValueError("Need to set the working directory \ raise ValueError(
or to give a template") "Need to set the working directory \
or to give a template"
)
else: else:
working_dir = template.dirname() working_dir = template.dirname()
logger.debug(f"The output directory will be {working_dir}") logger.debug(f"The output directory will be {working_dir}")
return working_dir return working_dir
def activate_printanswers(texfile): def activate_printanswers(
texfile, noans=r"solution/print = false", ans=r"solution/print = true"
):
""" Activate printanswers mod in texfile """ """ Activate printanswers mod in texfile """
output_fname = "corr_" + texfile output_fname = "corr_" + texfile
with open(texfile, 'r') as input_f: with open(texfile, "r") as input_f:
with open(output_fname, "w") as output_f: with open(output_fname, "w") as output_f:
for line in input_f.readlines(): for line in input_f.readlines():
output_f.write(line.replace( output_f.write(line.replace(noans, ans))
r'solution/print = false',
r'solution/print = true',
))
return output_fname return output_fname
@ -109,25 +112,14 @@ def produce_and_compile(options):
template = Path(options.template).name template = Path(options.template).name
logger.debug(f"Template will be {template}") logger.debug(f"Template will be {template}")
list_infos = [ list_infos = [{"num": f"{i+1:02d}"} for i in range(options.num_subj)]
{"num": f"{i+1:02d}"}
for i in range(options.num_subj)
]
tex_files = [] tex_files = []
for infos in list_infos: for infos in list_infos:
dest = ( dest = working_dir / Path(template.replace("tpl", infos["num"]))
working_dir
/ Path(template.replace("tpl", infos["num"]))
)
logger.debug(f"Feeding template toward {dest}") logger.debug(f"Feeding template toward {dest}")
tex_files.append(dest) tex_files.append(dest)
pytex.feed( pytex.feed(working_dir / template, {"infos": infos}, output=dest, force=1)
working_dir/template,
{"infos": infos},
output=dest,
force=1
)
logger.debug(f"{dest} fed") logger.debug(f"{dest} fed")
if not options.no_compil: if not options.no_compil:
@ -142,10 +134,10 @@ def produce_and_compile(options):
if not options.no_join and not options.no_compil: if not options.no_join and not options.no_compil:
pdfjoin( pdfjoin(
pdf_files, pdf_files,
template.replace('tpl', "all").replace(".tex",".pdf"), template.replace("tpl", "all").replace(".tex", ".pdf"),
working_dir, working_dir,
rm_pdfs=1 rm_pdfs=1,
) )
if options.corr: if options.corr:
pdf_files = [] pdf_files = []
@ -161,10 +153,10 @@ def produce_and_compile(options):
if not options.no_join and not options.no_compil: if not options.no_join and not options.no_compil:
pdfjoin( pdfjoin(
pdf_files, pdf_files,
template.replace('tpl', "corr").replace(".tex",".pdf"), template.replace("tpl", "corr").replace(".tex", ".pdf"),
working_dir, working_dir,
rm_pdfs=1 rm_pdfs=1,
) )
if not options.dirty: if not options.dirty:
pytex.clean(working_dir) pytex.clean(working_dir)
@ -180,60 +172,60 @@ def main():
action="store", action="store",
type="string", type="string",
dest="template", dest="template",
help="File with the template. The name should have the following form tpl_... ." help="File with the template. The name should have the following form tpl_... .",
) )
parser.add_option( parser.add_option(
"-w", "-w",
"--working-dir", "--working-dir",
action="store", action="store",
type="string", type="string",
dest="working_dir", dest="working_dir",
help="Where fed templates and compiled files will be placed" help="Where fed templates and compiled files will be placed",
) )
parser.add_option( parser.add_option(
"-N", "-N",
"--number_subjects", "--number_subjects",
action="store", action="store",
type="int", type="int",
dest="num_subj", dest="num_subj",
default = 1, default=1,
help="The number of subjects to make" help="The number of subjects to make",
) )
parser.add_option( parser.add_option(
"-d", "-d",
"--dirty", "--dirty",
action="store_true", action="store_true",
dest="dirty", dest="dirty",
help="Do not clean after compilation" help="Do not clean after compilation",
) )
parser.add_option( parser.add_option(
"-n", "-n",
"--no-compile", "--no-compile",
action="store_true", action="store_true",
dest="no_compil", dest="no_compil",
help="Do not compile source code" help="Do not compile source code",
) )
parser.add_option( parser.add_option(
"-j", "-j",
"--no-join", "--no-join",
action="store_true", action="store_true",
dest="no_join", dest="no_join",
help="Do not join pdf and clean single pdf" help="Do not join pdf and clean single pdf",
) )
parser.add_option( parser.add_option(
"-O", "-O",
"--only-corr", "--only-corr",
action="store_true", action="store_true",
dest="only_corr", dest="only_corr",
help="Create and compile only correction from existing subjects" help="Create and compile only correction from existing subjects",
) )
parser.add_option( parser.add_option(
"-c", "-c",
"--corr", "--corr",
action="store_true", action="store_true",
dest="corr", dest="corr",
help="Create and compile correction while making subjects" help="Create and compile correction while making subjects",
) )
(options, _) = parser.parse_args() (options, _) = parser.parse_args()
@ -246,7 +238,7 @@ def main():
produce_and_compile(options) produce_and_compile(options)
if __name__ == '__main__': if __name__ == "__main__":
main() main()