Skip to content

Commit 809e10e

Browse files
committed
Introduce SettingsManager and inject into gcode
1 parent cb6d09c commit 809e10e

File tree

3 files changed

+70
-51
lines changed

3 files changed

+70
-51
lines changed

src/gcode.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import numpy as np
33
from typing import List, Optional
44

5-
import src.settings
65
from src import gui_utils
76
import vtk
87

@@ -242,16 +241,20 @@ def readGCode(filename):
242241
return parseGCode(lines)
243242

244243

245-
def parseGCode(lines):
244+
def parseGCode(lines, settings=None):
246245
layer = []
247246
planes = []
248247

249-
s = src.settings.sett()
248+
if settings is None:
249+
from src import settings as settings_module
250+
settings = settings_module.sett()
251+
252+
s = settings
250253

251254
current_layer = 0
252255

253256
t = 0 # select extruder (t==0) or incline (t==2) or rotate (t==1)
254-
printer = Printer(src.settings.sett())
257+
printer = Printer(settings)
255258

256259
for line in lines:
257260
line = line.strip()

src/settings.py

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,62 @@
1111
import yaml
1212
import vtk
1313

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)
1570

1671
# setup app path
1772
if getattr(sys, "frozen", False):
@@ -25,10 +80,6 @@
2580
APP_PATH = path.join(path.dirname(__file__), "..")
2681

2782

28-
def sett():
29-
return _sett
30-
31-
3283
_colors = {} # Available colors: https://en.wikipedia.org/wiki/File:SVG_Recognized_color_keyword_names.svg
3384
_vtk_colors = vtk.vtkNamedColors()
3485

@@ -55,9 +106,9 @@ def get_color_rgb(color_name):
55106

56107
def copy_project_files(project_path: str):
57108
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 = ""
61112
save_settings()
62113

63114

@@ -215,21 +266,6 @@ def add_recent_project(recent_projects, project_path):
215266
save_recent_projects(recent_projects)
216267

217268

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-
233269
def read_settings(filename=""):
234270
if not filename:
235271
logging.debug("retrieving settings")
@@ -260,33 +296,13 @@ def check_children(obj):
260296

261297
check_children(data)
262298

263-
return data
299+
return data
264300

265301
return None
266302

267303

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)
290306
temp = temp.replace("!!python/object:src.settings.Settings", "").strip()
291307
temp = temp.replace("!!python/object/apply:pathlib.PosixPath", "").strip()
292308

test/gcode_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def testParseGCode(self):
6464
"G0 U35;rotation",
6565
";End",
6666
]
67-
result = parseGCode(gcode)
67+
result = parseGCode(gcode, gcode_stubs.sett())
6868
self.assertEqual(4, len(result.layers))
6969
self.assertEqual(
7070
[(0, 0), (0, 35.0)],

0 commit comments

Comments
 (0)