-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.py
234 lines (205 loc) · 9.8 KB
/
configure.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import json
from customer_project import Customer, Project
import shutil
from pathlib import Path
from util import nr2str
class Database():
def __init__(self, configuration):
self.path = Path(configuration.dict['database'])
self.dict = self.load_database()
self.superfolder = configuration.dict['superfolder']
self.subfolder = configuration.dict["extra_subfolder"]
def load_database(self):
if self.path.exists():
print(f'load database from {self.path}')
with open(self.path) as path:
dict = json.load(path)
return dict
else:
print('new database')
self.new_database()
self.save()
return self.dict
def new_database(self):
self.dict = {'customers': {},
'customer_index': 0
}
def save(self):
with open(self.path, 'w') as outfile:
json.dump(self.dict, outfile, indent=4)
def new_customer(self, customer_name):
if self.dict['customers'].get(customer_name) is not None:
return False
self.dict['customer_index'] += 1
new_customer = Customer(customer_name, self.dict['customer_index'])
self.dict['customers'][customer_name] = new_customer.dict
self.save()
return True
def new_customer_from_path(self, customer_index, customer_name, in_subfolder=False):
if self.dict['customers'].get(customer_name) is not None:
return False
if self.dict['customer_index'] < customer_index:
self.dict['customer_index'] = customer_index
if in_subfolder:
subfolder = self.subfolder
else:
subfolder = None
new_customer = Customer(customer_name, customer_index, subfolder=subfolder)
self.dict['customers'][customer_name] = new_customer.dict
self.save()
return True
def new_project(self, customer_name, project_name):
if self.dict['customers'][customer_name]['projects'].get(project_name):
return False
self.dict['customers'][customer_name]['project_index'] += 1
new_project = Project(project_name, customer_name, self.superfolder, self.dict)
self.dict['customers'][customer_name]['projects'][project_name] = new_project.dict
self.save()
return new_project
def new_project_from_path(self, customer_name, project_index, project_name):
if self.dict['customers'][customer_name]['projects'].get(project_name):
print(f'Project with name {project_name} already exists.')
project_name = project_name + str(project_index)
if self.dict['customers'][customer_name]['project_index'] < project_index:
self.dict['customers'][customer_name]['project_index'] = project_index
new_project = Project(project_name, customer_name, self.superfolder, self.dict)
self.dict['customers'][customer_name]['projects'][project_name] = new_project.dict
self.save()
return new_project
def reset_database(self):
self.new_database()
self.save()
def customer_in_both_folders(self, customer_name):
customer = self.dict['customers'][customer_name]
customer_index = nr2str(customer['index'])
match_folders_subfolder = list(Path(self.subfolder).glob(f'{customer_index}*{customer_name}*'))
match_folders_superfolder = list(Path(self.superfolder).glob(f'{customer_index}*{customer_name}*'))
if len(match_folders_subfolder) > 0 and len(match_folders_superfolder) > 0:
return True, match_folders_subfolder, match_folders_superfolder
else:
return False, match_folders_subfolder, match_folders_superfolder
def track_subfolder_superfolder_status(self, customer_name):
customer = self.dict['customers'][customer_name]
customer_index = nr2str(customer['index'])
match_folders_subfolder = list((Path(self.superfolder) / self.subfolder).glob(f'*{customer_index}*{customer_name}*'))
match_folders_superfolder = list(Path(self.superfolder).glob(f'*{customer_index}*{customer_name}*'))
if ((len(match_folders_subfolder) > 0 and len(match_folders_superfolder) > 0) or
len(match_folders_subfolder) > 1 or len(match_folders_superfolder) > 1):
raise CustomerDuplication(
f"""
Für den Kunden {customer_name} mit dem index {customer_index} existieren mehrere Ordner.
Bitte führe die Ordner {match_folders_subfolder} und {match_folders_superfolder} zuerst zusammen.
"""
)
if customer['subfolder'] and len(match_folders_superfolder) == 1:
customer['subfolder'] = False
customer['rel_folder'] = match_folders_superfolder[0].name
elif not customer['subfolder'] and len(match_folders_subfolder) == 1:
customer['subfolder'] = True
customer['rel_folder'] = self.subfolder + '/' + match_folders_subfolder[0].name
class Configuration:
def __init__(self, debug=False):
self.debug = debug
if not debug:
self.path = Path.home() / '.promanCFG.json'
else:
self.path = Path(__file__).parent / 'promanCFG_debug.json'
self.dict, save_config = self.get_configure_file()
print('config file', self.path)
if save_config:
self.save()
self.database = Database(self)
pass
def get_configure_file(self):
if self.path.exists():
with open(self.path) as path:
dict = json.load(path)
if dict.get('extra_subfolder') is None:
dict['extra_subfolder'] = '! INAKTIVE KUNDEN & LEADS'
save_config = True
else:
save_config = False
return dict, save_config
else:
print('new config')
save_config = True
config = self.new_configuration()
return config, save_config
def new_configuration(self):
if not self.debug:
home = Path.home()
db_path = home / '.database.json'
else:
db_path = Path(__file__).parent / 'database_debug.json'
with open(self.path, 'w') as outfile:
json.dump({}, outfile, indent=4)
return {'superfolder': None,
'import_folder': None,
'extra_subfolder': '! INAKTIVE KUNDEN & LEADS',
'database': str(db_path)
}
def change_database_location(self, folder):
src = self.database.path
des = Path(folder) / src.name
self.dict['database'] = str(des)
self.save()
self.database.path = des
shutil.move(src, des)
def save(self):
with open(self.path, 'w') as outfile:
json.dump(self.dict, outfile, indent=4)
def set_superfolder(self, directory):
self.dict['superfolder'] = directory
self.database.superfolder = directory
self.save()
def set_subfolder(self, directory):
self.dict['extra_subfolder'] = directory
self.database.subfolder = directory
self.save()
def set_import_folder(self, directory):
self.dict['import_folder'] = directory
self.save()
pass
def create_database_from_path(self, startpath):
startpath = Path(startpath)
subfolder_full_path = startpath / self.dict["extra_subfolder"]
for i, dir_to_iter in enumerate([subfolder_full_path, startpath]):
if i == 0 and not subfolder_full_path.exists():
continue
for dir in dir_to_iter.iterdir():
if not dir.is_dir():
continue
in_subfolder = i == 0
customer_index = dir.name[:3]
customer_name = dir.name[4:]
if customer_index.isdigit():
customer_in_database = self.database.dict['customers'].get(customer_name)
if customer_in_database is None:
self.database.new_customer_from_path(int(customer_index),
customer_name,
in_subfolder=in_subfolder)
else:
in_db_index = customer_in_database['index']
if customer_index != in_db_index:
raise CustomerDuplication(
f'Der Kunde {customer_name} wurde mehrmals gefunden mit verschiedenen Indices:'
f' {customer_index} und {in_db_index}')
else:
raise CustomerDuplication(
f"""
Der Kunde {customer_name} wurde mehrmals gefunden! Bitte führe zuerst die Ordner zusammen
""")
self.database.dict['customers'][customer_name]['subfolder'] = in_subfolder
for subdir in dir.iterdir():
if not subdir.is_dir() or len(subdir.name) < 9:
continue
project_index = subdir.name[4:7]
project_name = subdir.name[8:]
if project_index.isdigit() and subdir.name[:3] == self.database.dict['customers'][customer_name]['index']:
self.database.new_project_from_path(customer_name,
int(project_index),
project_name,
)
print("number customers: ", len(self.database.dict["customers"]))
class CustomerDuplication(Exception):
pass