File packages/config.py changed (mode: 100644) (index 9b97c8b..87eb981) |
1 |
1 |
import tkinter |
import tkinter |
2 |
2 |
import tkinter.filedialog |
import tkinter.filedialog |
3 |
|
import csv |
|
4 |
3 |
import os |
import os |
|
4 |
|
import csv |
|
5 |
|
import enum |
|
6 |
|
import datetime |
|
7 |
|
|
|
8 |
|
|
|
9 |
|
class State(enum.Enum): |
|
10 |
|
CONFIG_UPLOAD_EXIST = 0 |
|
11 |
|
CONFIG_DATA_EXIST = 1 |
|
12 |
|
CONFIG_ATTR_EXIST = 2 |
|
13 |
|
CONFIG_NOT_FOUND = 3 |
|
14 |
|
CONFIG_BAD = 4 |
|
15 |
|
|
|
16 |
|
|
|
17 |
|
fields = ['upload_folder', 'data_folder', 'attribute_file', 'file_change_date'] |
|
18 |
|
|
|
19 |
|
|
|
20 |
|
def config_open(path, option): |
|
21 |
|
if(option not in ['r', 'w', 'a']): |
|
22 |
|
print("Bad file opening option: [{1}] used with path {0}" |
|
23 |
|
.format(path, option)) |
|
24 |
|
return [] |
|
25 |
|
|
|
26 |
|
with open(path, mode=option) as item: |
|
27 |
|
# remove spaces/newlines, create list of options between ; |
|
28 |
|
rows = [row.strip(' ').strip('\n').split(';') for row in item] |
|
29 |
|
|
|
30 |
|
return rows |
|
31 |
|
|
5 |
32 |
|
|
6 |
33 |
def config_creation(): |
def config_creation(): |
7 |
|
# variable declaration |
|
8 |
|
current_path = str('') |
|
9 |
|
configpath = str('') |
|
|
34 |
|
current_path = '' |
|
35 |
|
configpath = '' |
10 |
36 |
|
|
11 |
|
# path choosing dialog gui |
|
12 |
37 |
root = tkinter.Tk() |
root = tkinter.Tk() |
13 |
38 |
root.withdraw() |
root.withdraw() |
14 |
39 |
|
|
15 |
40 |
current_path = os.getcwd() |
current_path = os.getcwd() |
16 |
41 |
configpath = os.path.join(current_path, 'config.txt') |
configpath = os.path.join(current_path, 'config.txt') |
17 |
|
if(not( os.path.isfile(configpath) )): |
|
|
42 |
|
if(not( os.path.isfile(configpath))): |
18 |
43 |
with open(configpath, mode='w') as item: |
with open(configpath, mode='w') as item: |
19 |
44 |
item.write('upload_folder=\n') |
item.write('upload_folder=\n') |
20 |
45 |
item.write('data_folder=\n') |
item.write('data_folder=\n') |
|
... |
... |
def config_creation(): |
25 |
50 |
else: |
else: |
26 |
51 |
return None |
return None |
27 |
52 |
|
|
|
53 |
|
|
28 |
54 |
def config_read(configpath): |
def config_read(configpath): |
29 |
|
config = {'upload_folder':'', 'data_folder':'', |
|
30 |
|
'attribute_file':'', 'file_change_date':''} |
|
31 |
|
if(not(configpath)): |
|
32 |
|
configpath = config_creation() |
|
33 |
|
with open(configpath, mode='r') as item: |
|
34 |
|
rows = [row.strip(' ').strip('\n').split(';') for row in item] |
|
|
55 |
|
config = {'upload_folder': '', 'data_folder': '', |
|
56 |
|
'attribute_file': '', 'file_change_date': ''} |
|
57 |
|
rows = config_open(path=configpath, option='r') |
35 |
58 |
|
|
36 |
|
for row in rows: |
|
37 |
|
option = "".join(row[0]).split('=') |
|
|
59 |
|
for row in rows: |
|
60 |
|
option = "".join(row[0]).split('=') |
38 |
61 |
|
|
39 |
|
if(option[0].strip(' ') == 'upload_folder'): |
|
40 |
|
config[ 'upload_folder' ] = option[1].strip(' ') |
|
41 |
|
if(option[0].strip(' ') == 'data_folder'): |
|
42 |
|
config[ 'data_folder' ] = option[1].strip(' ') |
|
43 |
|
if(option[0].strip(' ') == 'attribute_file'): |
|
44 |
|
config[ 'attribute_file' ] = option[1].strip(' ') |
|
45 |
|
if(option[0].strip(' ') == 'file_change_date'): |
|
46 |
|
config[ 'file_change_date' ] = option[1].strip(' ') |
|
|
62 |
|
for field in fields: |
|
63 |
|
if(option[0].strip(' ') == field): |
|
64 |
|
config[field] = option[1].strip(' ') |
47 |
65 |
|
|
48 |
66 |
return config |
return config |
49 |
67 |
|
|
|
68 |
|
|
50 |
69 |
def config_write(configpath, data): |
def config_write(configpath, data): |
51 |
|
# read the current content of the config |
|
52 |
70 |
with open(configpath, mode='w') as item: |
with open(configpath, mode='w') as item: |
53 |
|
writer = csv.DictWriter(item, delimiter='=', lineterminator='\n', fieldnames=['title', 'value']) |
|
|
71 |
|
writer = csv.DictWriter(item, delimiter='=', lineterminator='\n', |
|
72 |
|
fieldnames=['title', 'value']) |
|
73 |
|
try: |
|
74 |
|
for row in data: |
|
75 |
|
writer.writerow(data[row]) |
|
76 |
|
except Exception as err: |
|
77 |
|
print("Error @ config write: error: {0}" |
|
78 |
|
.format(err)) |
|
79 |
|
|
|
80 |
|
|
|
81 |
|
def config_check(path): |
|
82 |
|
states = [] |
|
83 |
|
if(not(path)): |
|
84 |
|
return [State.CONFIG_NOT_FOUND] |
|
85 |
|
|
|
86 |
|
rows = config_open(path=path, option='r') |
|
87 |
|
|
|
88 |
|
# Check if all required fields are present |
|
89 |
|
for field in fields: |
|
90 |
|
# DEBUG TEST NOT FINAL |
|
91 |
|
if(field not in rows): |
|
92 |
|
print("ERROR: field:{0} not found in {1}" |
|
93 |
|
.format(field, rows)) |
|
94 |
|
return [State.CONFIG_BAD] |
|
95 |
|
|
|
96 |
|
for row in rows: |
|
97 |
|
# Check if a equal sign exists |
|
98 |
|
if('=' not in row): |
|
99 |
|
print("ERROR: config options have to be split by =, = not found") |
|
100 |
|
return [State.CONFIG_BAD] |
|
101 |
|
|
|
102 |
|
# Check if only one equal sign was used |
|
103 |
|
elif(row.count('=') > 1): |
|
104 |
|
print("ERROR: equal sign can only be used as delimiter!") |
|
105 |
|
return [State.CONFIG_BAD] |
|
106 |
|
|
|
107 |
|
# Add each working file/dir from the config to the state list |
|
108 |
|
for index, field in enumerate(fields): |
|
109 |
|
option = "".join(row[0]).split('=') |
|
110 |
|
if(field == 'file_change_date' and option[1]): |
|
111 |
|
states.append(index) |
|
112 |
|
if(option[0].strip(' ') == field and option[1]): |
|
113 |
|
if(field == 'attribute_file'): |
|
114 |
|
if(os.path.isfile(option[1].strip(' '))): |
|
115 |
|
states.append(index) |
|
116 |
|
else: |
|
117 |
|
if(os.path.exists(option[1].strip(' '))): |
|
118 |
|
states.append(index) |
|
119 |
|
|
|
120 |
|
return states |
|
121 |
|
|
|
122 |
|
|
|
123 |
|
def get_path(message, path_type, initialdir): |
|
124 |
|
path = '' |
|
125 |
|
if(path_type == 'dir'): |
|
126 |
|
path = tkinter.filedilag.askdirectory(title=message, |
|
127 |
|
initialdir=initialdir) |
|
128 |
|
elif(path_type == 'file'): |
|
129 |
|
path = tkinter.filedialog.askopenfilename(title=message, |
|
130 |
|
initialdir=initialdir) |
|
131 |
|
return path |
|
132 |
|
|
|
133 |
|
|
|
134 |
|
def get_options(configpath, initialdir): |
|
135 |
|
options = {'uploadpath': '', |
|
136 |
|
'datapath': '', |
|
137 |
|
'attributefile': '', |
|
138 |
|
'date': ''} |
|
139 |
|
|
|
140 |
|
# Check the config for mistakes and correct them |
|
141 |
|
config_state = [] |
|
142 |
|
config_state = config_check(path=configpath) |
|
143 |
|
|
|
144 |
|
if(State.CONFIG_NOT_FOUND in config_state): |
|
145 |
|
configpath = config_creation() |
|
146 |
|
|
|
147 |
|
elif(State.CONFIG_BAD in config_state): |
|
148 |
|
os.remove(configpath) |
|
149 |
|
configpath = config_creation |
|
150 |
|
|
|
151 |
|
if(State.CONFIG_UPLOAD_EXIST not in config_state or |
|
152 |
|
State.CONFIG_DATA_EXIST not in config_state or |
|
153 |
|
State.CONFIG_ATTR_EXIST not in config_state or |
|
154 |
|
State.CONFIG_NOT_FOUND in config_state or |
|
155 |
|
State.CONFIG_BAD in config_state): |
|
156 |
|
options['uploadpath'] = get_path(message='Path for Upload files', |
|
157 |
|
path_type='dir', |
|
158 |
|
initialdir=initialdir) |
|
159 |
|
options['datapath'] = get_path(message='Path for Input files', |
|
160 |
|
path_type='dir', |
|
161 |
|
initialdir=initialdir) |
|
162 |
|
options['attributefile'] = get_path(message='Path for attribute file', |
|
163 |
|
path_type='file', |
|
164 |
|
initialdir=initialdir) |
|
165 |
|
|
|
166 |
|
options['date'] = datetime.datetime.now().strftime("%d.%m.%Y-%H:%M") |
54 |
167 |
|
|
55 |
|
for row in data: |
|
56 |
|
writer.writerow(data[row]) |
|
|
168 |
|
config_write(configpath=configpath, data=options) |
57 |
169 |
|
|
|
170 |
|
return options |