-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
201 lines (183 loc) · 6.75 KB
/
settings.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
import os
import json
folder_structure = {
'Foto\'s': None,
'Plannen': None,
'Stabiliteit': [
'Algemene documenten',
'Berekeningen',
'Berekeningen Scia',
'Mails',
'Meetstaat & borderel',
'Plannen pdf',
'Sondeerverslag',
'Stabiliteitsplannen'
],
'Ventilatieverslaggever': None,
'vVGP + EPB': [
'EPB',
'VGP'],
'Werfverslagen': None
}
class Settings():
# dossier = dict in dict -- dossier[nr][name or path]
# load main settings
"""
#####################################################################
loading/saving functions
#####################################################################
"""
def loader(self):
"""
load setting / create default setting when none exists
- main settings
- folders
"""
print("loading settings")
# DEFAULT settings contents
self.main_settings = {"directory": "none selected"}
self.folder_scan = {}
# load settings
settings = self.loadjson("settings.txt", self.main_settings) # settings.py
folders = self.loadjson("folders.txt", self.folder_scan)
print("returning\n settings:{}\n folders:{}".format(settings, folders))
return settings, folders #returns a tuple
def loadjson(self, filename, setting):
"""
load filename if exists,
else create filename and populate with (default)setting
"""
print("creating\n{} \npopulating with\n{}".format(filename, setting))
try:
with open(filename,"r") as f:
setting = json.load(f)
except:
print("creating", setting)
with open(filename,"w") as f:
json.dump(setting, f)
return setting
"""---------------------------------------------------
save settings function change (filename, setting) input order
"""
def save_set(self, filename, setting):
with open(filename,"w") as f:
json.dump(setting, f, indent = 4) # changed order (settings, f) from (f, settings)
print(setting)
"""---------------------------------------------------
"""
def load_set(self, name):
"""? needs to save setting + does not load, but gets specific setting, change name?"""
print("loads specific setting(?) + checks for existing setting (?)")
with open("settings.txt", "r") as f:
self.settings = json.load(f)
if name in self.settings:
print("found setting")
print(self.settings[name])
return self.settings[name]
else:
print("nothing found")
pass
#loads dossier settings
"""ADD check for missing folders here?
"""
def load_dossier(self, dossier):
print("loads dossier(?)")
with open("folders.txt", "r") as f:
self.loading = json.load(f)
print(self.loading[dossier])
return self.loading[dossier]
def read_folders(self, scanpath):
"""
returns dictionary with name,path for each folder(/dossier)
from foldernames structure 'dossier - name'
"""
print("reading folders: dossier/name/path\n", scanpath)
dossier = {}
dirs = os.listdir(scanpath)
for folder in dirs:
path = os.path.join(scanpath, folder)
if ' - ' in folder and os.path.isdir(path):
print('processing\n', folder)
prefix, suffix = folder.split(' - ', 1)
dossier[prefix] = {"name": suffix, "path": path}
print("returning\n", dossier)
return dossier
"""
? one of the other = other ?
"""
"""
#check if dirs exist for ALL SAVED dossiers
def check_folders(self, folder_scan):
"""
#ONLY DO THIS FOR INPUT DOSSIER? ? ? ?
"""
for key in folder_scan:
print(key)
print(folder_scan[key])
print(folder_scan[key]['path'])
for maps in folder_structure:
if folder_structure[maps] == None:
print("none for {}".format(maps))
scanpath = folder_scan[key]['path'] + "//" + maps
print(scanpath)
self.create_dirs(scanpath)
else:
print("map for {}".format(maps))
print(folder_structure[maps])
#fix item is list iterate trough list?
for item in folder_structure[maps]:
print(item)
scanpath = folder_scan[key]['path'] + "//" + maps + "//" + item
print(scanpath)
self.create_dirs(scanpath)
"""
#check if dirs exist for SPECIFIC dossiers
def dossier_dir(self, folder_scan):
"""
creates paths for specific dossier
according to default dictionary structure
"""
global folder_structure
dossier = self.dossier.get()
for folder, subfolder in folder_structure.items():
if subfolder == None:
scanpath = folder_scan[dossier]['path'] + "//" + folder
self.create_dirs(scanpath)
else:
for subfolder in folder_structure[folder]:
scanpath = folder_scan[dossier]['path'] + "//" + folder + "//" + subfolder
self.create_dirs(scanpath)
# create dirs if they don't exist
def create_dirs(self, scanpath):
"""
ADD ??
if not os.path.exists(directory):
os.makedirs(directory)
"""
"""
BETTER ADD??
import errno
def make_sure_path_exists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
we try to create the directories, but if they
already exist we ignore the error.
On the other hand, any other error gets reported.
For example, if you create dir 'a' beforehand and
remove all permissions from it,you will get an
OSError raised with errno.EACCES (Permission denied,
error 13).
"""
print("create nonexisting dirs")
try:
print("only scans paths(?)")
os.makedirs(scanpath)
except OSError:
if not os.path.isdir(scanpath):
print("if not existing (?) ADD STUFF CHECK Settings.py")
raise
def structure(self):
return folder_structure