1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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())
|