Commit 262eaa26 authored by sumpfralle's avatar sumpfralle

simplified xml dump of arbitrary objects


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1197 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 5d3779c9
...@@ -135,7 +135,6 @@ class Bounds(pycam.Plugins.ListPluginBase): ...@@ -135,7 +135,6 @@ class Bounds(pycam.Plugins.ListPluginBase):
continue continue
self._gtk_handlers.append((self.gui.get_object("NameCell"), self._gtk_handlers.append((self.gui.get_object("NameCell"),
"edited", self._edit_bounds_name)) "edited", self._edit_bounds_name))
self.core.register_chain("xml_dump", self.dump_xml)
self._event_handlers.extend(( self._event_handlers.extend((
("bounds-selection-changed", self._switch_bounds), ("bounds-selection-changed", self._switch_bounds),
("bounds-changed", self._store_bounds_settings), ("bounds-changed", self._store_bounds_settings),
...@@ -146,6 +145,7 @@ class Bounds(pycam.Plugins.ListPluginBase): ...@@ -146,6 +145,7 @@ class Bounds(pycam.Plugins.ListPluginBase):
self._switch_bounds() self._switch_bounds()
self._update_model_list() self._update_model_list()
self._event_handlers.append(("bounds-changed", "visual-item-updated")) self._event_handlers.append(("bounds-changed", "visual-item-updated"))
self.core.register_chain("xml_dump", self.dump_xml)
self.register_event_handlers(self._event_handlers) self.register_event_handlers(self._event_handlers)
return True return True
...@@ -392,15 +392,15 @@ class Bounds(pycam.Plugins.ListPluginBase): ...@@ -392,15 +392,15 @@ class Bounds(pycam.Plugins.ListPluginBase):
def dump_xml(self, result): def dump_xml(self, result):
root = ET.Element("bounds-list") root = ET.Element("bounds-list")
for bounds in self.core.get("bounds"): for bounds in self:
root.append(bounds.dump_xml()) root.append(bounds.dump_xml())
result.append((None, root)) result.append((None, root))
class BoundsDict(dict): class BoundsDict(pycam.Plugins.ObjectWithAttributes):
def __init__(self, core, *args, **kwargs): def __init__(self, core, *args, **kwargs):
super(BoundsDict, self).__init__(*args, **kwargs) super(BoundsDict, self).__init__("bounds", *args, **kwargs)
self.core = core self.core = core
self.update({ self.update({
"BoundaryLowX": 0, "BoundaryLowX": 0,
......
...@@ -100,7 +100,6 @@ class Processes(pycam.Plugins.ListPluginBase): ...@@ -100,7 +100,6 @@ class Processes(pycam.Plugins.ListPluginBase):
self._gtk_handlers.append((self.gui.get_object("StrategySelector"), self._gtk_handlers.append((self.gui.get_object("StrategySelector"),
"changed", "process-strategy-changed")) "changed", "process-strategy-changed"))
self.register_model_update(update_model) self.register_model_update(update_model)
self.core.register_chain("xml_dump", self.dump_xml)
self._event_handlers = ( self._event_handlers = (
("process-strategy-list-changed", self._update_widgets), ("process-strategy-list-changed", self._update_widgets),
("process-selection-changed", self._process_switch), ("process-selection-changed", self._process_switch),
...@@ -108,6 +107,7 @@ class Processes(pycam.Plugins.ListPluginBase): ...@@ -108,6 +107,7 @@ class Processes(pycam.Plugins.ListPluginBase):
("process-strategy-changed", self._store_process_settings)) ("process-strategy-changed", self._store_process_settings))
self.register_gtk_handlers(self._gtk_handlers) self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers) self.register_event_handlers(self._event_handlers)
self.core.register_chain("xml_dump", self.dump_xml)
return True return True
def teardown(self): def teardown(self):
...@@ -258,20 +258,20 @@ class Processes(pycam.Plugins.ListPluginBase): ...@@ -258,20 +258,20 @@ class Processes(pycam.Plugins.ListPluginBase):
strategies = self.core.get("get_parameter_sets")("process").values() strategies = self.core.get("get_parameter_sets")("process").values()
strategies.sort(key=lambda item: item["weight"]) strategies.sort(key=lambda item: item["weight"])
strategy = strategies[0] strategy = strategies[0]
new_process = {"strategy": strategy["name"], new_process = ProcessEntity({"strategy": strategy["name"],
"parameters": strategy["parameters"].copy(), "parameters": strategy["parameters"].copy()})
}
self.append(new_process) self.append(new_process)
self.select(new_process) self.select(new_process)
def dump_xml(self, result): def dump_xml(self, result):
root = ET.Element("processes") root = ET.Element("processes")
for process in self: for process in self:
leaf = ET.SubElement(root, "process") root.append(process.get_xml())
leaf.set("name", repr(self.get_attr(process, "name")))
leaf.set("strategy", process["strategy"])
parameters = ET.SubElement(leaf, "parameters")
for key, value in process["parameters"].iteritems():
parameters.set(key, repr(value))
result.append((None, root)) result.append((None, root))
class ProcessEntity(pycam.Plugins.ObjectWithAttributes):
def __init__(self, parameters):
super(ProcessEntity, self).__init__("process", parameters)
...@@ -21,6 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -21,6 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
import time import time
import xml.etree.ElementTree as ET
import pycam.Plugins import pycam.Plugins
import pycam.Utils import pycam.Utils
...@@ -131,10 +132,12 @@ class Tasks(pycam.Plugins.ListPluginBase): ...@@ -131,10 +132,12 @@ class Tasks(pycam.Plugins.ListPluginBase):
self._update_widgets() self._update_widgets()
self._update_table() self._update_table()
self._task_switch() self._task_switch()
self.core.register_chain("xml_dump", self.dump_xml)
self.core.set("tasks", self) self.core.set("tasks", self)
return True return True
def teardown(self): def teardown(self):
self.core.unregister_chain("xml_dump", self.dump_xml)
if self.gui: if self.gui:
self.core.unregister_ui("main", self.gui.get_object("TaskBox")) self.core.unregister_ui("main", self.gui.get_object("TaskBox"))
self.core.unregister_ui("task_parameters", self.core.unregister_ui("task_parameters",
...@@ -264,9 +267,8 @@ class Tasks(pycam.Plugins.ListPluginBase): ...@@ -264,9 +267,8 @@ class Tasks(pycam.Plugins.ListPluginBase):
types = self.core.get("get_parameter_sets")("task").values() types = self.core.get("get_parameter_sets")("task").values()
types.sort(key=lambda item: item["weight"]) types.sort(key=lambda item: item["weight"])
one_type = types[0] one_type = types[0]
new_task = {"type": one_type["name"], new_task = TaskEntity({"type": one_type["name"],
"parameters": one_type["parameters"].copy(), "parameters": one_type["parameters"].copy()})
}
self.append(new_task) self.append(new_task)
self.select(new_task) self.select(new_task)
...@@ -356,3 +358,15 @@ class Tasks(pycam.Plugins.ListPluginBase): ...@@ -356,3 +358,15 @@ class Tasks(pycam.Plugins.ListPluginBase):
progress.finish() progress.finish()
return result return result
def dump_xml(self, result):
root = ET.Element("tasks")
for task in self:
root.append(task.get_xml())
result.append((None, root))
class TaskEntity(pycam.Plugins.ObjectWithAttributes):
def __init__(self, parameters):
super(TaskEntity, self).__init__("task", parameters)
...@@ -23,9 +23,6 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -23,9 +23,6 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import pycam.Plugins import pycam.Plugins
from pycam.Cutters.CylindricalCutter import CylindricalCutter
from pycam.Cutters.SphericalCutter import SphericalCutter
from pycam.Cutters.ToroidalCutter import ToroidalCutter
class Tools(pycam.Plugins.ListPluginBase): class Tools(pycam.Plugins.ListPluginBase):
...@@ -124,12 +121,12 @@ class Tools(pycam.Plugins.ListPluginBase): ...@@ -124,12 +121,12 @@ class Tools(pycam.Plugins.ListPluginBase):
("tool-selection-changed", self._tool_switch), ("tool-selection-changed", self._tool_switch),
("tool-changed", self._store_tool_settings), ("tool-changed", self._store_tool_settings),
("tool-shape-changed", self._store_tool_settings)) ("tool-shape-changed", self._store_tool_settings))
self.core.register_chain("xml_dump", self.dump_xml)
self.register_model_update(update_model) self.register_model_update(update_model)
self.register_gtk_handlers(self._gtk_handlers) self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers) self.register_event_handlers(self._event_handlers)
self._update_widgets() self._update_widgets()
self._tool_switch() self._tool_switch()
self.core.register_chain("xml_dump", self.dump_xml)
return True return True
def teardown(self): def teardown(self):
...@@ -284,21 +281,20 @@ class Tools(pycam.Plugins.ListPluginBase): ...@@ -284,21 +281,20 @@ class Tools(pycam.Plugins.ListPluginBase):
shapes = self.core.get("get_parameter_sets")("tool").values() shapes = self.core.get("get_parameter_sets")("tool").values()
shapes.sort(key=lambda item: item["weight"]) shapes.sort(key=lambda item: item["weight"])
shape = shapes[0] shape = shapes[0]
new_tool = {"shape": shape["name"], new_tool = ToolEntity({"shape": shape["name"],
"parameters": shape["parameters"].copy(), "parameters": shape["parameters"].copy()})
}
self.append(new_tool) self.append(new_tool)
self.select(new_tool) self.select(new_tool)
def dump_xml(self, result): def dump_xml(self, result):
root = ET.Element("tools") root = ET.Element("tools")
for tool in self: for tool in self:
leaf = ET.SubElement(root, "tool") root.append(tool.get_xml())
for key in self.LIST_ATTRIBUTE_MAP:
leaf.set(key, repr(self.get_attr(tool, key)))
leaf.set("shape", tool["shape"])
parameters = ET.SubElement(leaf, "parameters")
for key, value in tool["parameters"].iteritems():
parameters.set(key, repr(value))
result.append((None, root)) result.append((None, root))
class ToolEntity(pycam.Plugins.ObjectWithAttributes):
def __init__(self, parameters):
super(ToolEntity, self).__init__("tool", parameters)
...@@ -29,6 +29,7 @@ import gobject ...@@ -29,6 +29,7 @@ import gobject
import pycam.Utils.log import pycam.Utils.log
import pycam.Utils.locations import pycam.Utils.locations
import pycam.Utils.xml_handling
_log = pycam.Utils.log.get_logger() _log = pycam.Utils.log.get_logger()
...@@ -485,3 +486,18 @@ class ListPluginBase(PluginBase, list): ...@@ -485,3 +486,18 @@ class ListPluginBase(PluginBase, list):
raise KeyError("Attribute '%s' is not part of this list: %s" % \ raise KeyError("Attribute '%s' is not part of this list: %s" % \
(attr, ", ".join(self.LIST_ATTRIBUTE_MAP.keys()))) (attr, ", ".join(self.LIST_ATTRIBUTE_MAP.keys())))
class ObjectWithAttributes(dict):
def __init__(self, key, params=None):
if params is None:
params = {}
self.update(params)
self.node_key = key
def get_xml(self, name=None):
if name is None:
name = self.node_key
return pycam.Utils.xml_handling.get_xml(self, name=name,
ignore_self=True)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
$Id$
Copyright 2010 Lars Kruse <devel@sumpfralle.de>
This file is part of PyCAM.
PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import xml.etree.ElementTree as ET
def get_xml(item, name="value", ignore_self=False):
if not ignore_self and hasattr(item, "get_xml"):
return item.get_xml(name=name)
elif isinstance(item, (list, tuple, set)):
leaf = ET.Element("list", name=name)
for single in item:
leaf.append(get_xml(single))
return leaf
elif isinstance(item, dict):
leaf = ET.Element("dict", name=name)
for key, value in item.iteritems():
leaf.append(get_xml(value, name=key))
return leaf
else:
leaf = ET.Element(name)
leaf.text=repr(item)
return leaf
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