Commit 7aea6865 authored by sumpfralle's avatar sumpfralle

switched the "Settings" class to a dict-compatible interface


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1256 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 0f89dcab
...@@ -61,72 +61,61 @@ def get_config_filename(filename=None): ...@@ -61,72 +61,61 @@ def get_config_filename(filename=None):
return os.path.join(config_dir, filename) return os.path.join(config_dir, filename)
class Settings(object): class Settings(dict):
GET_INDEX = 0 GET_INDEX = 0
SET_INDEX = 1 SET_INDEX = 1
VALUE_INDEX = 2 VALUE_INDEX = 2
def __init__(self): def __getitem_orig(self, key):
self.items = {} return super(Settings, self).__getitem__(key)
self.values = {}
def __setitem_orig(self, key, value):
super(Settings, self).__setitem__(key, value)
def add_item(self, key, get_func=None, set_func=None): def add_item(self, key, get_func=None, set_func=None):
self.items[key] = [None, None, None] self.__setitem_orig(key, [None, None, None])
self.define_get_func(key, get_func) self.define_get_func(key, get_func)
self.define_set_func(key, set_func) self.define_set_func(key, set_func)
self.items[key][self.VALUE_INDEX] = None self.__getitem_orig(key)[self.VALUE_INDEX] = None
def remove_item(self, key): def set(self, key, value):
if not self.items.has_key(key): self[key] = value
return
del self.items[key] def get(self, key, default=None):
try:
return self.__getitem__(key)
except KeyError:
return default
def define_get_func(self, key, get_func=None): def define_get_func(self, key, get_func=None):
if not self.items.has_key(key): if not self.has_key(key):
return return
if get_func is None: if get_func is None:
get_func = lambda: self.items[key][self.VALUE_INDEX] get_func = lambda: self.__getitem_orig(key)[self.VALUE_INDEX]
self.items[key][self.GET_INDEX] = get_func self.__getitem_orig(key)[self.GET_INDEX] = get_func
def define_set_func(self, key, set_func=None): def define_set_func(self, key, set_func=None):
if not self.items.has_key(key): if not self.has_key(key):
return return
def default_set_func(value): def default_set_func(value):
self.items[key][self.VALUE_INDEX] = value self.__getitem_orig(key)[self.VALUE_INDEX] = value
if set_func is None: if set_func is None:
set_func = default_set_func set_func = default_set_func
self.items[key][self.SET_INDEX] = set_func self.__getitem_orig(key)[self.SET_INDEX] = set_func
def get(self, key, default=None): def __getitem__(self, key):
if self.items.has_key(key): try:
try: return self.__getitem_orig(key)[self.GET_INDEX]()
result = self.items[key][self.GET_INDEX]() except TypeError, err_msg:
except TypeError, err_msg: log.info("Failed to retrieve setting '%s': %s" % (key, err_msg))
log.info("Failed to retrieve setting '%s': %s" % (key, err_msg)) return None
result = None
else:
result = default
return result
def set(self, key, value): def __setitem__(self, key, value):
if not self.items.has_key(key): if not self.has_key(key):
self.add_item(key) self.add_item(key)
self.items[key][self.SET_INDEX](value) self.__getitem_orig(key)[self.SET_INDEX](value)
self.items[key][self.VALUE_INDEX] = value self.__getitem_orig(key)[self.VALUE_INDEX] = value
def has_key(self, key):
""" expose the "has_key" function of the items list """
return self.items.has_key(key)
def __str__(self):
result = {}
for key in self.items.keys():
result[key] = self.get(key)
return str(result)
def get_keys(self):
return self.items.keys()
class ProcessSettings(object): class ProcessSettings(object):
......
...@@ -99,7 +99,7 @@ class Fonts(pycam.Plugins.PluginBase): ...@@ -99,7 +99,7 @@ class Fonts(pycam.Plugins.PluginBase):
return True return True
def teardown(self): def teardown(self):
self.core.remove_item("fonts") del self.core["fonts"]
if self.gui: if self.gui:
font_toggle = self.gui.get_object("ShowFontDialog") font_toggle = self.gui.get_object("ShowFontDialog")
self.core.unregister_ui("edit_menu", None) self.core.unregister_ui("edit_menu", None)
......
...@@ -123,7 +123,7 @@ class GCodeSafetyHeight(pycam.Plugins.PluginBase): ...@@ -123,7 +123,7 @@ class GCodeSafetyHeight(pycam.Plugins.PluginBase):
def teardown(self): def teardown(self):
self.clear_state_items() self.clear_state_items()
self.core.remove_item("gcode_safety_height") del self.core["gcode_safety_height"]
self.safety_height.destroy() self.safety_height.destroy()
...@@ -146,7 +146,7 @@ class GCodeFilenameExtension(pycam.Plugins.PluginBase): ...@@ -146,7 +146,7 @@ class GCodeFilenameExtension(pycam.Plugins.PluginBase):
return True return True
def teardown(self): def teardown(self):
self.core.remove_item("gcode_filename_extension") del self.core["gcode_filename_extension"]
self.filename_extension.destroy() self.filename_extension.destroy()
...@@ -181,7 +181,7 @@ class GCodeStepWidth(pycam.Plugins.PluginBase): ...@@ -181,7 +181,7 @@ class GCodeStepWidth(pycam.Plugins.PluginBase):
while self.controls: while self.controls:
self.core.unregister_ui("gcode_step_width", self.controls.pop()) self.core.unregister_ui("gcode_step_width", self.controls.pop())
for key in "xyz": for key in "xyz":
self.core.remove_item("gcode_minimum_step_%s" % key) del self.core["gcode_minimum_step_%s" % key]
class GCodeSpindle(pycam.Plugins.PluginBase): class GCodeSpindle(pycam.Plugins.PluginBase):
...@@ -212,7 +212,7 @@ class GCodeSpindle(pycam.Plugins.PluginBase): ...@@ -212,7 +212,7 @@ class GCodeSpindle(pycam.Plugins.PluginBase):
return True return True
def teardown(self): def teardown(self):
self.core.remove_item("gcode_spindle_delay") del self.core["gcode_spindle_delay"]
self.core.unregister_ui("gcode_spindle", self.core.unregister_ui("gcode_spindle",
self.spindle_delay.get_widget()) self.spindle_delay.get_widget())
self.core.unregister_ui("gcode_spindle", self.core.unregister_ui("gcode_spindle",
......
...@@ -56,7 +56,7 @@ class GCodeTouchOff(pycam.Plugins.PluginBase): ...@@ -56,7 +56,7 @@ class GCodeTouchOff(pycam.Plugins.PluginBase):
self.gui.get_object("TouchOffBox")) self.gui.get_object("TouchOffBox"))
self.unregister_gtk_handlers(self._gtk_handlers) self.unregister_gtk_handlers(self._gtk_handlers)
for setting in ("touch_off_on_startup", "touch_off_on_tool_change"): for setting in ("touch_off_on_startup", "touch_off_on_tool_change"):
self.core.remove_item(setting) del self.core[setting]
def update_widgets(self, widget=None): def update_widgets(self, widget=None):
# tool change controls # tool change controls
......
...@@ -50,7 +50,7 @@ class OpenGLViewToolpath(pycam.Plugins.PluginBase): ...@@ -50,7 +50,7 @@ class OpenGLViewToolpath(pycam.Plugins.PluginBase):
self.core.get("unregister_color")("color_toolpath_cut") self.core.get("unregister_color")("color_toolpath_cut")
self.core.get("unregister_color")("color_toolpath_return") self.core.get("unregister_color")("color_toolpath_return")
self.core.get("unregister_display_item")("show_toolpath") self.core.get("unregister_display_item")("show_toolpath")
self.core.remove_item("draw_toolpath_moves_func") del self.core["draw_toolpath_moves_func"]
self.core.emit_event("visual-item-updated") self.core.emit_event("visual-item-updated")
def get_draw_dimension(self, low, high): def get_draw_dimension(self, low, high):
......
...@@ -71,7 +71,7 @@ class ToolpathSimulation(pycam.Plugins.PluginBase): ...@@ -71,7 +71,7 @@ class ToolpathSimulation(pycam.Plugins.PluginBase):
def teardown(self): def teardown(self):
if self.gui: if self.gui:
self.core.remove_item("show_simulation") del self.core["show_simulation"]
self.core.unregister_ui("toolpath_handling", self._frame) self.core.unregister_ui("toolpath_handling", self._frame)
self.core.unregister_event("visualize-items", self.show_simulation) self.core.unregister_event("visualize-items", self.show_simulation)
self.unregister_event_handlers(self._event_handlers) self.unregister_event_handlers(self._event_handlers)
......
...@@ -141,8 +141,8 @@ def get_all_program_locations(core): ...@@ -141,8 +141,8 @@ def get_all_program_locations(core):
# import all external program locations into a dict # import all external program locations into a dict
program_locations = {} program_locations = {}
prefix = "external_program_" prefix = "external_program_"
for key in core.get_keys(): for key in core:
if key.startswith(prefix) and core.get(key): if key.startswith(prefix) and core[key]:
program_locations[key[len(prefix):]] = core.get(key) program_locations[key[len(prefix):]] = core[key]
return program_locations return program_locations
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment