Mercurial > libervia-backend
comparison tools/memory.py @ 19:f2a745ca0fbc
refactoring: using xml params part III (parameters import)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 07 Nov 2009 21:23:28 +0100 |
parents | 6928e3cb73a8 |
children | fc8c202cda87 |
comparison
equal
deleted
inserted
replaced
18:6928e3cb73a8 | 19:f2a745ca0fbc |
---|---|
31 const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save") | 31 const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save") |
32 const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save") | 32 const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save") |
33 | 33 |
34 class Param(): | 34 class Param(): |
35 """This class manage parameter with xml""" | 35 """This class manage parameter with xml""" |
36 ### TODO: add desciption in params | |
36 | 37 |
37 #TODO: mettre Watched dans un plugin | 38 #TODO: mettre Watched dans un plugin |
38 default_xml = """ | 39 default_xml = """ |
39 <params> | 40 <params> |
40 <category name="Connection"> | 41 <category name="Connection"> |
41 <param name="JabberID" value="goffi@necton2.int/TestScript" type="string" /> | 42 <param name="JabberID" value="goffi@necton2.int/TestScript" type="string" /> |
42 <param name="Password" value="toto" type="password" /> | 43 <param name="Password" value="toto" type="password" /> |
43 <param name="Server" value="necton2.int" type="string" /> | 44 <param name="Server" value="necton2.int" type="string" /> |
44 </category> | 45 </category> |
45 <category name="File Transfert"> | |
46 <param name="IP" value="192.168.0.10" type="string" /> | |
47 <param name="Port" value="28915" type="string" /> | |
48 </category> | |
49 <category name="Misc"> | 46 <category name="Misc"> |
50 <param name="Watched" value="test@Jabber.goffi.int" type="string" /> | 47 <param name="Watched" value="test@Jabber.goffi.int" type="string" /> |
51 </category> | 48 </category> |
52 </params> | 49 </params> |
53 """ | 50 """ |
56 self.dom = minidom.parseString(Param.default_xml) | 53 self.dom = minidom.parseString(Param.default_xml) |
57 | 54 |
58 def __init__(self): | 55 def __init__(self): |
59 debug("Parameters init") | 56 debug("Parameters init") |
60 self.load_default_params() | 57 self.load_default_params() |
61 | 58 |
59 def __get_unique_node(self, parent, tag, name): | |
60 """return node with given tag, create a new one if the node doesn't exist | |
61 @param parent: parent of nodes to check (e.g. documentElement) | |
62 @param tag: tag to check (e.g. "category") | |
63 @param name: name to check (e.g. "JID") | |
64 @return: node if it exist or None | |
65 """ | |
66 for node in parent.childNodes: | |
67 if node.nodeName == tag and node.getAttribute("name") == name: | |
68 #the node already exists | |
69 return node | |
70 #the node is new | |
71 return None | |
72 | |
73 def import_params(self, parent, xml): | |
74 """import xml in parameters, do nothing if the param already exist | |
75 @param parent: parent class (usefull for callbacks) | |
76 @param xml: parameters in xml form""" | |
77 | |
78 src_dom = minidom.parseString(xml) | |
79 | |
80 def import_node(tgt_parent, src_parent): | |
81 print "import_node [%s, %s]" % (tgt_parent.nodeName, src_parent.nodeName) | |
82 for child in src_parent.childNodes: | |
83 if child.nodeName == '#text': | |
84 continue | |
85 node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name")) | |
86 if not node: #The node is new | |
87 tgt_parent.appendChild(child) | |
88 else: | |
89 import_node(node, child) | |
90 | |
91 import_node(self.dom.documentElement, src_dom.documentElement) | |
92 | |
62 | 93 |
63 def getParamV(self, name, category): | 94 def getParamV(self, name, category): |
64 """Helper method to get the value in the good type | 95 """Helper method to get the value in the good type |
65 getParamV stands for GetParamValue""" | 96 getParamV stands for GetParamValue""" |
66 node = self.__getParamNode(name, category) | 97 node = self.__getParamNode(name, category) |
67 if not node: | 98 if not node: |
68 error("Requested param [%s] in category [%s] doesn't exist !" , name, category) | 99 error("Requested param [%s] in category [%s] doesn't exist !" , name, category) |
69 return none | 100 return None |
70 return node.getAttribute("value") | 101 return node.getAttribute("value") |
71 | 102 |
72 def getParams(self): | 103 def getParams(self): |
73 """Return the whole params XML""" | 104 """Return the whole params XML""" |
74 return self.dom.toxml() | 105 return self.dom.toxml() |
100 node = self.__getParamNode(name, category) | 131 node = self.__getParamNode(name, category) |
101 if not node: | 132 if not node: |
102 return #TODO: throw an error | 133 return #TODO: throw an error |
103 node.setAttribute("value", value) | 134 node.setAttribute("value", value) |
104 | 135 |
105 """def createParam (self, name, value, type, category): | |
106 ### TODO: add desciption in params | |
107 if not self.params.has_key(category): | |
108 self.params[category]={} | |
109 self.params[category][name]=[value,type];""" | |
110 | |
111 class Memory: | 136 class Memory: |
112 """This class manage all persistent informations""" | 137 """This class manage all persistent informations""" |
113 | 138 |
114 def __init__(self): | 139 def __init__(self): |
115 info ("Memory manager init") | 140 info ("Memory manager init") |
132 debug("params loaded") | 157 debug("params loaded") |
133 except: | 158 except: |
134 error ("Can't load params !") | 159 error ("Can't load params !") |
135 self.params.load_default_params() | 160 self.params.load_default_params() |
136 else: | 161 else: |
162 error ("No params, using default parameters") | |
137 self.params.load_default_params() | 163 self.params.load_default_params() |
138 | 164 |
139 #history | 165 #history |
140 if os.path.exists(const_SAVEFILE_HISTORY): | 166 if os.path.exists(const_SAVEFILE_HISTORY): |
141 try: | 167 try: |
222 def getParamsCategories(self): | 248 def getParamsCategories(self): |
223 return self.params.getParamsCategories() | 249 return self.params.getParamsCategories() |
224 | 250 |
225 def setParam(self, name, value, category): | 251 def setParam(self, name, value, category): |
226 return self.params.setParam(name, value, category) | 252 return self.params.setParam(name, value, category) |
253 | |
254 def import_params(self, parent, xml): | |
255 return self.params.import_params(parent, xml) |