-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherp_basic_tools.py
47 lines (37 loc) · 1.81 KB
/
erp_basic_tools.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
# contains basic functionality for erp data generation and transfer
import re
import csv
class CreateFile:
def __init__(self, outputfile, outputfile_structure, **kwrest):
testmode = kwrest.get('test', False) # in testmode, output is written to display instead of file
self.testmode = testmode
self.outputfile = outputfile
self.outputfile_structure = outputfile_structure
# replace dummy entries in output file (like 'company_name') with actual data
def replace_strings_variables(self, variable_dict):
outputfile_structure = str(self.outputfile_structure)
for key, value in variable_dict.items():
if type(value) != str:
value = str(value)
if bool(re.search("'", value)):
value = value.replace("'", "\\\'")
key = str("'" + key + "'")
outputfile_structure = re.sub(key, str("'" + value + "'"), outputfile_structure)
return outputfile_structure
def output_csv(self, output_dict):
row_output = self.replace_strings_variables(output_dict) # replace strings in document raw structure with created oontent
with open(self.outputfile, 'a+', newline='') as csvfile:
dbwriter = csv.writer(csvfile, delimiter=',',
quotechar="'", quoting=csv.QUOTE_MINIMAL)
dbwriter.writerow(eval(row_output))
def generate_transaction_number(number_format, start, increment=1):
if start < 0:
start = 0
else:
start = int(start)
search_pattern = r"{(.+)}"
string_of_zeros = re.search(search_pattern, number_format)
leading_zeros = len(string_of_zeros.group(1))
raw_number = str(start - 1 + increment).zfill(leading_zeros)
number_gen = re.sub(search_pattern, raw_number, number_format)
return number_gen