From ee90417e15bb33e18f995600b6e761ca87e3a63e Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Thu, 17 Sep 2020 11:48:26 +0200 Subject: Make templates searchable by path --- fracture/__main__.py | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'fracture') diff --git a/fracture/__main__.py b/fracture/__main__.py index b385116..e7700f3 100644 --- a/fracture/__main__.py +++ b/fracture/__main__.py @@ -13,7 +13,10 @@ from configparser import ConfigParser import os from os import path + +# MOVE THIS TO Conf CONFIG_FILE = "" +TEMPLATE_DIR = "" def load_config(): @@ -53,12 +56,13 @@ def load_config(): Conf.CURRENCY = conf["invoice"].get("currency", "€") Conf.CURRENCY_DECIMAL = conf["invoice"].getint("currency_decimal", 2) - # TEMPLATE ? - # TODO - # Or dump a json or something? - templatefile = conf["invoice"]["template"] - if not path.isabs(templatefile): - templatefile = path.join(confile, templatefile) + # TEMPLATE directory + global TEMPLATE_DIR + TEMPLATE_DIR = conf["invoice"].get("templatedir", "") + TEMPLATE_DIR = path.expandvars(TEMPLATE_DIR) + TEMPLATE_DIR = path.expanduser(TEMPLATE_DIR) + if not path.isabs(TEMPLATE_DIR): + TEMPLATE_DIR = path.join(basedir, TEMPLATE_DIR) # INVOICE LEVEL TAXES (like IRPF in Spain) tax = () @@ -134,9 +138,17 @@ def summarize(xlsx=False, year=None, quarter=None): wrtr.writerow(r) @command -def render(id, type=None, format=None): +def render(id, template=None, type=None, list_templates=None): + + if list_templates: + # List the template directory and exit + print("Current templatedir:", TEMPLATE_DIR) + print("Available files: ") + for i in os.listdir(TEMPLATE_DIR): + print("\t", i) + return - # TODO AUTOMATE TEMPLATE SEARCH AND THAT, THIS IS THIS FOR DEV + # Find templates and make them work if format is None: raise ValueError("No format specified") @@ -144,7 +156,7 @@ def render(id, type=None, format=None): if invoice is None: raise ValueError("No invoice found") - with open("templates/template." + format, "r") as f: + with open(path.join(TEMPLATE_DIR, template) , "r") as f: template_text = f.read() template = Template(template_text) print(template.render(invoice=invoice.to_dict())) @@ -184,19 +196,22 @@ if __name__ == "__main__": help="Obtain the summary of the year") summary_parser.set_defaults(func=summarize) - # Dump invoice + # Render invoice summary_parser = subparsers.add_parser("render", aliases=["rend", "r"], - help="Render chosen invoice in json format") + help="Render chosen invoice in the format defined by a template") summary_parser.add_argument("id", nargs="?", type=str, help="Invoice identification string") summary_parser.add_argument("--type", type=str, help="Invoice type", default="sent") - summary_parser.add_argument("--format", type=str, - help="Invoice type", default="tex") - + summary_parser.add_argument("--template", "-t", type=str, + help="Template to use", default="template.tex") + summary_parser.add_argument("--list-templates", "-l", + action="store_true", default=False, + help="List available templates, ignores other options.") summary_parser.set_defaults(func=render) - # json + + # jsonify json_parser = subparsers.add_parser("json", aliases=["j"], help="Dump chosen invoice in json format") json_parser.add_argument("id", nargs="?", type=str, -- cgit v1.2.3