11
11
import yaml
12
12
import vtk
13
13
14
- _sett = None # do not forget to load_settings() at start
14
+
15
+ class SettingsManager :
16
+ """Encapsulates loading, saving and accessing application settings."""
17
+
18
+ def __init__ (self , settings = None ):
19
+ self ._sett = settings
20
+
21
+ @property
22
+ def settings (self ):
23
+ return self ._sett
24
+
25
+ def load (self , filename = "" ):
26
+ old_setts = self ._sett
27
+ data = read_settings (filename )
28
+ if data is not None :
29
+ logging .debug ("Settings loaded" )
30
+ self ._sett = Settings (data )
31
+
32
+ if old_setts is not None and not old_setts .has_same_attributes (self ._sett ):
33
+ self ._sett = old_setts
34
+ raise Exception ("Check the settings file" )
35
+
36
+ return self ._sett
37
+
38
+ def save (self , filename = "" ):
39
+ if not filename :
40
+ if self ._sett and getattr (self ._sett , "project_path" , None ):
41
+ app_path = self ._sett .project_path
42
+ elif getattr (sys , "frozen" , False ):
43
+ app_path = path .dirname (sys .executable )
44
+ else :
45
+ app_path = path .join (path .dirname (__file__ ), ".." )
46
+
47
+ filename = path .join (app_path , "settings.yaml" )
48
+
49
+ temp = prepare_temp_settings (self ._sett )
50
+
51
+ logging .info ("saving settings to %s" , filename )
52
+ with open (filename , "w" ) as f :
53
+ f .write (temp )
54
+
55
+
56
+ # default singleton used across the application
57
+ settings_manager = SettingsManager ()
58
+
59
+
60
+ def sett ():
61
+ return settings_manager .settings
62
+
63
+
64
+ def load_settings (filename = "" ):
65
+ return settings_manager .load (filename )
66
+
67
+
68
+ def save_settings (filename = "" ):
69
+ return settings_manager .save (filename )
15
70
16
71
# setup app path
17
72
if getattr (sys , "frozen" , False ):
25
80
APP_PATH = path .join (path .dirname (__file__ ), ".." )
26
81
27
82
28
- def sett ():
29
- return _sett
30
-
31
-
32
83
_colors = {} # Available colors: https://en.wikipedia.org/wiki/File:SVG_Recognized_color_keyword_names.svg
33
84
_vtk_colors = vtk .vtkNamedColors ()
34
85
@@ -55,9 +106,9 @@ def get_color_rgb(color_name):
55
106
56
107
def copy_project_files (project_path : str ):
57
108
load_settings ()
58
- global _sett
59
- _sett .project_path = project_path
60
- _sett .slicing .stl_file = ""
109
+ s = sett ()
110
+ s .project_path = project_path
111
+ s .slicing .stl_file = ""
61
112
save_settings ()
62
113
63
114
@@ -215,21 +266,6 @@ def add_recent_project(recent_projects, project_path):
215
266
save_recent_projects (recent_projects )
216
267
217
268
218
- def load_settings (filename = "" ):
219
- global _sett
220
- old_setts = _sett
221
-
222
- data = read_settings (filename )
223
- if data != None :
224
- logging .debug ("Settings loaded" )
225
- _sett = Settings (data )
226
-
227
- # check if the format is similar
228
- if old_setts is not None and not old_setts .has_same_attributes (_sett ):
229
- _sett = old_setts
230
- raise Exception ("Check the settings file" )
231
-
232
-
233
269
def read_settings (filename = "" ):
234
270
if not filename :
235
271
logging .debug ("retrieving settings" )
@@ -260,33 +296,13 @@ def check_children(obj):
260
296
261
297
check_children (data )
262
298
263
- return data
299
+ return data
264
300
265
301
return None
266
302
267
303
268
- def save_settings (filename = "" ):
269
- if not filename :
270
- if _sett .project_path :
271
- app_path = _sett .project_path
272
- elif getattr (sys , "frozen" , False ):
273
- app_path = path .dirname (sys .executable )
274
- else :
275
- # have to add .. because settings.py is under src folder
276
- app_path = path .join (path .dirname (__file__ ), ".." )
277
-
278
- settings_filename = "settings.yaml"
279
- filename = path .join (app_path , settings_filename )
280
-
281
- temp = prepare_temp_settings (_sett )
282
-
283
- logging .info ("saving settings to %s" , filename )
284
- with open (filename , "w" ) as f :
285
- f .write (temp )
286
-
287
-
288
- def prepare_temp_settings (_sett ):
289
- temp = yaml .dump (_sett )
304
+ def prepare_temp_settings (settings ):
305
+ temp = yaml .dump (settings )
290
306
temp = temp .replace ("!!python/object:src.settings.Settings" , "" ).strip ()
291
307
temp = temp .replace ("!!python/object/apply:pathlib.PosixPath" , "" ).strip ()
292
308
0 commit comments