comparison tools/memory.py @ 17:74a39f40eb6d

refactoring: using xml params (not finished yet)
author Goffi <goffi@goffi.org>
date Fri, 06 Nov 2009 19:48:57 +0100
parents a62d7d453f22
children 6928e3cb73a8
comparison
equal deleted inserted replaced
16:0a024d5e0cd0 17:74a39f40eb6d
22 from __future__ import with_statement 22 from __future__ import with_statement
23 23
24 import os.path 24 import os.path
25 import time 25 import time
26 import pickle 26 import pickle
27 from xml.dom import minidom
27 from logging import debug, info, error 28 from logging import debug, info, error
29 import pdb
28 30
29 const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save") 31 const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save")
30 const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save") 32 const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save")
33
34 class Param():
35 """This class manage parameter with xml"""
36
37 #TODO: mettre Watched dans un plugin
38 default_xml = """
39 <params>
40 <category name="Connection">
41 <param name="JabberID" value="goffi@jabber.goffi.int/TestScript" type="string" />
42 <param name="Password" value="toto" type="password" />
43 <param name="Server" value="jabber.goffi.int" type="string" />
44 </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">
50 <param name="Watched" value="test@Jabber.goffi.int" type="string" />
51 </category>
52 </params>
53 """
54
55 def load_default_params(self):
56 self.dom = minidom.parseString(Param.default_xml)
57
58 def __init__(self):
59 debug("Parameters init")
60 self.load_default_params()
61
62
63 def getParamV(self, name, category):
64 """Helper method to get the value in the good type
65 getParamV stands for GetParamValue"""
66 node = self.__getParamNode(name, category)
67 if not node:
68 error("Requested param [%s] in category [%s] doesn't exist !" , name, category)
69 return none
70 return node.getAttribute("value")
71
72 def getParams(self):
73 """Return the wold params XML"""
74 return self.dom.toxml()
75
76
77 """def getParam(self, name, category):
78 if self.params.has_key(namespace) and self.params[namespace].has_key(name):
79 return self.params[namespace][name]
80 return ["",""]"""
81
82 def __getParamNode(self, name, category):
83 for node in self.dom.documentElement.childNodes:
84 if node.nodeName == "category" and node.getAttribute("name") == category:
85 params = node.getElementsByTagName("param")
86 for param in params:
87 if param.getAttribute("name") == name:
88 return param
89 return None
90
91
92 """def getParams(self, namespace):
93 if self.params.has_key(namespace):
94 ret=[]
95 for name in self.params[namespace]:
96 ret.append([name] + self.params[namespace][name])
97 return ret
98 return [[]]"""
99
100 def getParamsCategories(self):
101 """return the categories availables"""
102 categories=[]
103 for cat in self.dom.getElementsByTagName("category"):
104 categories.append(cat.getAttribute("name"))
105 return categories
106
107 def setParam(self, name, value, category):
108 node = self.__getParamNode(name, category)
109 if not node:
110 return #TODO: throw an error
111 node.setAttribute(name, value)
112
113 """def createParam (self, name, value, type, namespace):
114 ### TODO: add desciption in params
115 if not self.params.has_key(namespace):
116 self.params[namespace]={}
117 self.params[namespace][name]=[value,type];"""
31 118
32 class Memory: 119 class Memory:
33 """This class manage all persistent informations""" 120 """This class manage all persistent informations"""
34 121
35 def __init__(self): 122 def __init__(self):
36 info ("Memory manager init") 123 info ("Memory manager init")
37 self.contact={} 124 self.contact={}
38 self.presenceStatus={} 125 self.presenceStatus={}
39 self.params={} 126 self.params=Param()
40 self.history={} 127 self.history={}
41 self.disco={} #XXX: maybe best in a separate class 128 self.disco={} #XXX: maybe best in a separate class
42 self.features={} 129 self.features={}
43 self.load() 130 self.load()
44
45 def load_default_params(self):
46 """Load default value, in case of no save file or error."""
47
48 self.createParam("JabberID", "goffi@jabber.goffi.int/TestScript", "string", "Connection")
49 self.createParam("Password", "toto", "password", "Connection")
50 self.createParam("Server", "jabber.goffi.int", "string", "Connection")
51 self.createParam("IP", "192.168.0.2", "string", "File Transfert")
52 self.createParam("Port", "28915", "string", "File Transfert")
53 self.createParam("Watched", "test@jabber.goffi.int", "string", "Misc") #TODO: a mettre dans un plugin
54 131
55 def load(self): 132 def load(self):
56 """Load parameters and all memory things from file/db""" 133 """Load parameters and all memory things from file/db"""
57 134
58 #first parameters 135 #first parameters
61 with open(const_SAVEFILE_PARAM, 'r') as params_pickle: 138 with open(const_SAVEFILE_PARAM, 'r') as params_pickle:
62 self.params=pickle.load(params_pickle) 139 self.params=pickle.load(params_pickle)
63 debug("params loaded") 140 debug("params loaded")
64 except: 141 except:
65 error ("Can't load params !") 142 error ("Can't load params !")
66 self.load_default_params() 143 self.params.load_default_params()
67 else: 144 else:
68 self.load_default_params() 145 self.params.load_default_params()
69 146
70 #history 147 #history
71 if os.path.exists(const_SAVEFILE_HISTORY): 148 if os.path.exists(const_SAVEFILE_HISTORY):
72 try: 149 try:
73 with open(const_SAVEFILE_HISTORY, 'r') as history_pickle: 150 with open(const_SAVEFILE_HISTORY, 'r') as history_pickle:
139 for contact, contactStatus in self.presenceStatus.items(): 216 for contact, contactStatus in self.presenceStatus.items():
140 status.append([contact]+contactStatus) 217 status.append([contact]+contactStatus)
141 debug ("Memory getPresenceStatus (%s)", status) 218 debug ("Memory getPresenceStatus (%s)", status)
142 return status 219 return status
143 220
144 def getParamV(self, name, namespace): 221 def getParamV(self, name, category):
145 """Helper method to get the value in the good type 222 return self.params.getParamV(name, category)
146 getParamV stands for GetParamValue""" 223
147 if not self.params.has_key(namespace) or not self.params[namespace].has_key(name): 224 def getParams(self):
148 error("Requested param %s in namespace %s doesn't exist !" , name, namespace) 225 return self.params.getParams()
149 return None 226
150 return self.params[namespace][name][0]
151
152
153 def getParam(self, name, namespace):
154 if self.params.has_key(namespace) and self.params[namespace].has_key(name):
155 return self.params[namespace][name]
156 return ["",""]
157
158 def getParams(self, namespace):
159 if self.params.has_key(namespace):
160 ret=[]
161 for name in self.params[namespace]:
162 ret.append([name] + self.params[namespace][name])
163 return ret
164 return [[]]
165
166 def getParamsCategories(self): 227 def getParamsCategories(self):
167 """return the namespaces availables""" 228 return self.params.getParamsCategories()
168 return self.params.keys() 229
169 230 def setParam(self, name, value, category):
170 def setParam(self, name, value, namespace): 231 return self.params.setParam(name, value, category)
171 if not self.params.has_key(namespace) or not self.params[namespace].has_key(name):
172 return #TODO: throw an error
173 self.params[namespace][name][0]=value;
174
175 def createParam (self, name, value, type, namespace):
176 ### TODO: add desciption in params
177 if not self.params.has_key(namespace):
178 self.params[namespace]={}
179 self.params[namespace][name]=[value,type];