From aacf3313390ab07f36c817bcfb48c11813be4580 Mon Sep 17 00:00:00 2001
From: Ekaitz Zarraga <ekaitz@elenq.tech>
Date: Sun, 9 Aug 2020 14:38:34 +0200
Subject: Multiple template type handling:

    - Move json to an independent command
---
 fracture/__main__.py | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

(limited to 'fracture')

diff --git a/fracture/__main__.py b/fracture/__main__.py
index 44f2518..e7c3381 100644
--- a/fracture/__main__.py
+++ b/fracture/__main__.py
@@ -135,17 +135,24 @@ def summarize(xlsx=False, year=None, quarter=None):
 
 @command
 def render(id, type=None, format=None):
+
+    # TODO AUTOMATE TEMPLATE SEARCH AND THAT, THIS IS THIS FOR DEV
+    if format is None:
+        raise ValueError("No format specified")
+
     invoice = Invoice.load(id, type)
-    if format == "json":
-        print(invoice.to_json())
+    if invoice is None:
+        raise ValueError("No invoice found")
 
-    if format == "xelatex":
-        # TODO AUTOMATE TEMPLATE SEARCH AND THAT, THIS IS THIS FOR DEV
-        with open("templates/template.tex", "r") as f:
-            template_text = f.read()
-        template = Template(template_text)
-        if invoice is not None:
-            print(template.render(invoice=invoice.to_dict()))
+    with open("templates/template." + format, "r") as f:
+        template_text = f.read()
+    template = Template(template_text)
+    print(template.render(invoice=invoice.to_dict()))
+
+@command
+def to_json(id, type=None):
+    invoice = Invoice.load(id, type)
+    print(invoice.to_json())
 
 if __name__ == "__main__":
     load_config()
@@ -185,10 +192,19 @@ if __name__ == "__main__":
     summary_parser.add_argument("--type", type=str,
         help="Invoice type", default="sent")
     summary_parser.add_argument("--format", type=str,
-            choices={"json", "xelatex"}, help="Invoice type", default="json")
+        help="Invoice type", default="tex")
 
     summary_parser.set_defaults(func=render)
 
+    # json
+    json_parser = subparsers.add_parser("json", aliases=["j"],
+        help="Dump chosen invoice in json format")
+    json_parser.add_argument("id", nargs="?", type=str,
+        help="Invoice identification string")
+    json_parser.add_argument("--type", type=str,
+        help="Invoice type", default="sent")
+    json_parser.set_defaults(func=to_json)
+
     # parse
     args = parser.parse_args()
     args.func(args)
-- 
cgit v1.2.3