summaryrefslogtreecommitdiff
path: root/fracture/__main__.py
diff options
context:
space:
mode:
Diffstat (limited to 'fracture/__main__.py')
-rw-r--r--fracture/__main__.py88
1 files changed, 88 insertions, 0 deletions
diff --git a/fracture/__main__.py b/fracture/__main__.py
new file mode 100644
index 0000000..71ca639
--- /dev/null
+++ b/fracture/__main__.py
@@ -0,0 +1,88 @@
+from invoice import Invoice, Tax
+import db
+
+import argparse
+import tempfile
+import subprocess
+import sqlite3
+from configparser import ConfigParser
+import os
+from os import path
+
+
+def load_config():
+ basedir = os.environ.get('XDG_CONFIG_HOME', None) or \
+ path.join(os.environ["HOME"], ".config")
+
+ basedir = path.join(basedir, "fracture")
+ confile = path.join(basedir, "config")
+
+ conf = ConfigParser()
+ conf.read(confile)
+
+ # SERIES
+ for k in conf["series"]:
+ Invoice.SERIES[int(k)] = conf["series"][k]
+
+ if Invoice.SERIES == {}:
+ raise "Invoice series not configured correctly: no series found"
+
+ # ID FORMAT
+ FORMAT = conf["invoice"].get("id_format",
+ '"%s/%s/%s" % (series, date.year, id)')
+
+ def f(series, date, id):
+ return eval(FORMAT, None, {"series": series, "date": date, "id": id})
+
+ Invoice.ID_FORMAT = f
+
+ # CURRENCY
+ Invoice.CURRENCY = conf["invoice"].get("currency", "€")
+ Invoice.CURRENCY_DECIMAL = conf["invoice"].getint("currency_decimal", 2)
+
+ # INVOICE LEVEL TAXES (like IRPF in Spain)
+ tax = []
+ for k in conf["taxes"]:
+ tax.append(Tax(k, conf.getfloat("taxes",k)))
+ Invoice.DEFAULT_TAXES = tuple(tax)
+
+ # DATABASE
+ Invoice.DB_FILE = path.join(basedir, "invoice.db")
+ if not os.path.exists(Invoice.DB_FILE):
+ db.create(Invoice.DB_FILE)
+
+def call_editor(filename):
+ """ Edit filename with $EDITOR (fallback to Vim) """
+ if not os.path.exists(filename):
+ raise FileNotFoundError("File not found: " + filename)
+ process = subprocess.Popen([os.environ.get("EDITOR", "vim"), filename],)
+ process.wait()
+
+def edit(contents):
+ """ Edit temporary file with initial `contents` and return it's edited
+ content """
+
+ with tempfile.NamedTemporaryFile(mode="w", delete=False) as t:
+ t.write(contents)
+
+ call_editor(t.name)
+
+ with open(t.name) as t:
+ edited_content = t.read()
+
+ if os.path.exists(t.name):
+ os.remove(t.name)
+
+ return edited_content
+
+
+if __name__ == "__main__":
+ load_config()
+
+
+ # TODO
+ a = Invoice()
+ a.from_config( edit( a.to_config() ))
+ num = a.persist()
+ print(num)
+ edit(Invoice.load(num).to_config())