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):
return os.path.join(config_dir, filename)
class Settings(object):
class Settings(dict):
GET_INDEX = 0
SET_INDEX = 1
VALUE_INDEX = 2
def __init__(self):
self.items = {}
self.values = {}
def __getitem_orig(self, key):
return super(Settings, self).__getitem__(key)
def __setitem_orig(self, key, value):
super(Settings, self).__setitem__(key, value)
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_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):
if not self.items.has_key(key):
return
del self.items[key]
def set(self, key, value):
self[key] = value
def get(self, key, default=None):
try:
return self.__getitem__(key)
except KeyError:
return default
def define_get_func(self, key, get_func=None):
if not self.items.has_key(key):
if not self.has_key(key):
return
if get_func is None:
get_func = lambda: self.items[key][self.VALUE_INDEX]
self.items[key][self.GET_INDEX] = get_func
get_func = lambda: self.__getitem_orig(key)[self.VALUE_INDEX]
self.__getitem_orig(key)[self.GET_INDEX] = get_func
def define_set_func(self, key, set_func=None):
if not self.items.has_key(key):
if not self.has_key(key):
return
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:
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):
if self.items.has_key(key):
def __getitem__(self, key):
try:
result = self.items[key][self.GET_INDEX]()
return self.__getitem_orig(key)[self.GET_INDEX]()
except TypeError, err_msg:
log.info("Failed to retrieve setting '%s': %s" % (key, err_msg))
result = None
else:
result = default
return result
return None
def set(self, key, value):
if not self.items.has_key(key):
def __setitem__(self, key, value):
if not self.has_key(key):
self.add_item(key)
self.items[key][self.SET_INDEX](value)
self.items[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()
self.__getitem_orig(key)[self.SET_INDEX](value)
self.__getitem_orig(key)[self.VALUE_INDEX] = value
class ProcessSettings(object):
......
......@@ -99,7 +99,7 @@ class Fonts(pycam.Plugins.PluginBase):
return True
def teardown(self):
self.core.remove_item("fonts")
del self.core["fonts"]
if self.gui:
font_toggle = self.gui.get_object("ShowFontDialog")
self.core.unregister_ui("edit_menu", None)
......
......@@ -123,7 +123,7 @@ class GCodeSafetyHeight(pycam.Plugins.PluginBase):
def teardown(self):
self.clear_state_items()
self.core.remove_item("gcode_safety_height")
del self.core["gcode_safety_height"]
self.safety_height.destroy()
......@@ -146,7 +146,7 @@ class GCodeFilenameExtension(pycam.Plugins.PluginBase):
return True
def teardown(self):
self.core.remove_item("gcode_filename_extension")
del self.core["gcode_filename_extension"]
self.filename_extension.destroy()
......@@ -181,7 +181,7 @@ class GCodeStepWidth(pycam.Plugins.PluginBase):
while self.controls:
self.core.unregister_ui("gcode_step_width", self.controls.pop())
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):
......@@ -212,7 +212,7 @@ class GCodeSpindle(pycam.Plugins.PluginBase):
return True
def teardown(self):
self.core.remove_item("gcode_spindle_delay")
del self.core["gcode_spindle_delay"]
self.core.unregister_ui("gcode_spindle",
self.spindle_delay.get_widget())
self.core.unregister_ui("gcode_spindle",
......
......@@ -56,7 +56,7 @@ class GCodeTouchOff(pycam.Plugins.PluginBase):
self.gui.get_object("TouchOffBox"))
self.unregister_gtk_handlers(self._gtk_handlers)
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):
# tool change controls
......
......@@ -50,7 +50,7 @@ class OpenGLViewToolpath(pycam.Plugins.PluginBase):
self.core.get("unregister_color")("color_toolpath_cut")
self.core.get("unregister_color")("color_toolpath_return")
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")
def get_draw_dimension(self, low, high):
......
......@@ -71,7 +71,7 @@ class ToolpathSimulation(pycam.Plugins.PluginBase):
def teardown(self):
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_event("visualize-items", self.show_simulation)
self.unregister_event_handlers(self._event_handlers)
......
......@@ -141,8 +141,8 @@ def get_all_program_locations(core):
# import all external program locations into a dict
program_locations = {}
prefix = "external_program_"
for key in core.get_keys():
if key.startswith(prefix) and core.get(key):
program_locations[key[len(prefix):]] = core.get(key)
for key in core:
if key.startswith(prefix) and core[key]:
program_locations[key[len(prefix):]] = core[key]
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