-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
173 lines (154 loc) · 5.8 KB
/
main.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# noinspection PyUnresolvedReferences
import readline
import subprocess
import sys
import jinja2
from invoice import SingleInvoice, HourlyInvoice, Invoice
from utils import *
def create_invoice():
current_id = config["last_id"]
mode = ask("Mode", "set", set=["single", "hourly"], default="hourly")
if mode == "single":
invoice = SingleInvoice()
elif mode == "hourly":
invoice = HourlyInvoice()
else:
invoice = Invoice()
current_id += 1
config["last_id"] = current_id
invoice.locale = ask("locale", "set", set=["de", "en"], default="de")
invoice.id = ask("id", "int", default=current_id)
invoice.title = ask("title", default=config["title"])
invoice.recipient = ask("recipient", "set", set=get_possible_recipents(), default=config["default_recipient"])
invoice.date = ask("date", "date", default="today")
invoice.description = ask("description", default=config["description"])
invoice.range = ask("range", default=config["range"])
if invoice.mode == "single":
invoice.price = ask("price", "money")
elif invoice.mode == "hourly":
invoice.hours = ask("hours", "int", default=config["hours"])
invoice.minutes = ask("minutes", "int", default="0")
invoice.per_hour = ask("rate per hour", "money", default=config["default_hourly_rate"])
directory = invoice_dir + "/" + str(invoice.id)
if os.path.exists(directory):
if not ask("overwrite", "boolean"):
exit()
else:
os.mkdir(directory)
save_yaml(vars(invoice), directory + "/data.yaml")
save_yaml(config, "config.yaml")
def compile_invoice(id):
directory = invoice_dir + "/" + str(id)
if os.path.exists(directory + "/locked"):
print("The invoice has already been locked")
exit()
invoicedata = load_yaml(directory + "/data.yaml")
mode = invoicedata["mode"]
del invoicedata["mode"]
if mode == "single":
invoice = SingleInvoice(**invoicedata)
elif mode == "hourly":
invoice = HourlyInvoice(**invoicedata)
else:
invoice = Invoice(**invoicedata)
env = jinja2.Environment(
block_start_string='\BLOCK{',
block_end_string='}',
variable_start_string='\VAR{',
variable_end_string='}',
comment_start_string='\#{',
comment_end_string='}',
line_statement_prefix='%#',
line_comment_prefix='%%',
trim_blocks=True,
autoescape=False,
loader=jinja2.FileSystemLoader(os.path.abspath('.'))
)
fromdata = load_yaml("from.yaml")
fromdata["country"] = fromdata["countryDE"] if invoice.locale == "de" else fromdata["countryEN"]
data = {
"from": fromdata,
"to": load_yaml("recipients/{id}.yaml".format(id=invoice.recipient)),
"invoice": invoice,
"config": config
}
strings = load_yaml("strings.yaml")
def translate(key):
if key in strings:
return strings[key][invoice.locale]
else:
print("Translation key for '{key}' is missing".format(key=key))
exit()
def format_digit(integer):
integer = integer / 100
string = "{0:.2f}".format(integer)
if invoice.locale == "de":
string = string.replace(".", ",")
return string
def format_date(date):
"""
:type date: datetime.datetime
"""
if invoice.locale == "de":
return date.strftime("%d. %m. %Y")
else:
return date.strftime("%Y-%m-%d")
env.filters['formatdigit'] = format_digit
env.filters['formatdate'] = format_date
env.filters['t'] = translate
with open(directory + "/{name}.tex".format(name=translate("invoice")), "w") as fh:
template = env.get_template('template.tex')
fh.write(template.render(section1='Long Form', section2='Short Form', **data))
os.chdir(directory)
for _ in range(2):
subprocess.check_call(['pdflatex', '-interaction=nonstopmode', '{name}.tex'.format(name=translate("invoice"))])
print(directory)
remove_tmp_files(translate("invoice"))
def sign_invoice(id):
directory = invoice_dir + "/" + str(id)
if os.path.exists(directory + "/locked"):
print("The invoice has already been locked")
exit()
if os.path.exists(directory + "/Rechnung.pdf"):
name = "Rechnung"
elif os.path.exists(directory + "/Invoice.pdf"):
name = "Invoice"
else:
print("Invoice not found")
name = ""
exit()
command = [
"/usr/local/PDF-Over/scripts/pdf-over_linux.sh",
"-i", "{dir}/{name}.pdf".format(dir=directory, name=name),
"-o", "{dir}/{name}_{signed}.pdf".format(
dir=directory, name=name, signed=("signiert" if name == "Rechnung" else "signed")
),
"-b", "LOCAL", # use local Bürgerkarte
"-a", # automatically position signature
"-v", "true" if name == "Rechnung" else "false", # set visibility
"-s" # save without asking
]
print(" ".join(command))
subprocess.check_call(command)
if __name__ == "__main__":
if len(sys.argv) == 1 or len(sys.argv) > 3 or sys.argv[1] not in ["create", "compile", "sign"]:
print("please use 'create', 'compile' or 'sign'")
exit()
config = load_yaml("config.yaml")
invoice_dir = config["invoice_dir"]
if sys.argv[1] == "create":
create_invoice()
if sys.argv[1] == "compile" or sys.argv[1] == "sign":
if len(sys.argv) == 3:
try:
invoice_id = int(sys.argv[2])
except ValueError:
invoice_id = False
print("invalid id")
exit()
else:
invoice_id = config["last_id"]
if sys.argv[1] == "compile":
compile_invoice(invoice_id)
else:
sign_invoice(invoice_id)