annotate tools/memory.py @ 34:a544b376b6f0

use proper utf-8 encoding for parsing xml in parameters
author Goffi <goffi@goffi.org>
date Sun, 13 Dec 2009 18:36:20 +1100
parents bb72c29f3432
children 3e24753b9e0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #!/usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
goffi@necton2
parents:
diff changeset
4 """
goffi@necton2
parents:
diff changeset
5 SAT: a jabber client
goffi@necton2
parents:
diff changeset
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
goffi@necton2
parents:
diff changeset
7
goffi@necton2
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
goffi@necton2
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
goffi@necton2
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
goffi@necton2
parents:
diff changeset
11 (at your option) any later version.
goffi@necton2
parents:
diff changeset
12
goffi@necton2
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
goffi@necton2
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
goffi@necton2
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
goffi@necton2
parents:
diff changeset
16 GNU General Public License for more details.
goffi@necton2
parents:
diff changeset
17
goffi@necton2
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
goffi@necton2
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
goffi@necton2
parents:
diff changeset
20 """
goffi@necton2
parents:
diff changeset
21
goffi@necton2
parents:
diff changeset
22 from __future__ import with_statement
goffi@necton2
parents:
diff changeset
23
goffi@necton2
parents:
diff changeset
24 import os.path
goffi@necton2
parents:
diff changeset
25 import time
goffi@necton2
parents:
diff changeset
26 import pickle
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
27 from xml.dom import minidom
0
goffi@necton2
parents:
diff changeset
28 from logging import debug, info, error
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
29 import pdb
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
30 from twisted.internet import defer
0
goffi@necton2
parents:
diff changeset
31
goffi@necton2
parents:
diff changeset
32 const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save")
goffi@necton2
parents:
diff changeset
33 const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save")
goffi@necton2
parents:
diff changeset
34
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
35 class Param():
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
36 """This class manage parameter with xml"""
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
37 ### TODO: add desciption in params
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
38
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
39 #TODO: mettre Watched dans un plugin
34
a544b376b6f0 use proper utf-8 encoding for parsing xml in parameters
Goffi <goffi@goffi.org>
parents: 22
diff changeset
40 default_xml = u"""
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
41 <params>
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
42 <category name="Connection">
18
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
43 <param name="JabberID" value="goffi@necton2.int/TestScript" type="string" />
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
44 <param name="Password" value="toto" type="password" />
18
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
45 <param name="Server" value="necton2.int" type="string" />
21
633c5ed65701 parameters: new button type (not finished)
Goffi <goffi@goffi.org>
parents: 20
diff changeset
46 <param name="NewAccount" value="Register new account" type="button" callback="registerNewAccount"/>
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
47 </category>
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
48 <category name="Misc">
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
49 <param name="Watched" value="test@Jabber.goffi.int" type="string" />
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
50 </category>
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
51 </params>
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
52 """
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
53
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
54 def load_default_params(self):
34
a544b376b6f0 use proper utf-8 encoding for parsing xml in parameters
Goffi <goffi@goffi.org>
parents: 22
diff changeset
55 self.dom = minidom.parseString(Param.default_xml.encode('utf-8'))
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
56
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
57 def __init__(self, host):
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
58 debug("Parameters init")
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
59 self.host = host
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
60 self.load_default_params()
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
61 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
62
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
63 def __get_unique_node(self, parent, tag, name):
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
64 """return node with given tag, create a new one if the node doesn't exist
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
65 @param parent: parent of nodes to check (e.g. documentElement)
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
66 @param tag: tag to check (e.g. "category")
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
67 @param name: name to check (e.g. "JID")
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
68 @return: node if it exist or None
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
69 """
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
70 for node in parent.childNodes:
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
71 if node.nodeName == tag and node.getAttribute("name") == name:
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
72 #the node already exists
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
73 return node
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
74 #the node is new
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
75 return None
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
76
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
77 def importParams(self, parent, xml):
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
78 """import xml in parameters, do nothing if the param already exist
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
79 @param parent: parent class (usefull for callbacks)
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
80 @param xml: parameters in xml form"""
34
a544b376b6f0 use proper utf-8 encoding for parsing xml in parameters
Goffi <goffi@goffi.org>
parents: 22
diff changeset
81 src_dom = minidom.parseString(xml.encode('utf-8'))
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
82
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
83 def import_node(tgt_parent, src_parent):
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
84 for child in src_parent.childNodes:
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
85 if child.nodeName == '#text':
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
86 continue
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
87 node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name"))
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
88 if not node: #The node is new
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
89 tgt_parent.appendChild(child)
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
90 else:
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
91 import_node(node, child)
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
92
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
93 import_node(self.dom.documentElement, src_dom.documentElement)
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
94
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
95 def __default_ok(self, value, name, category):
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
96 self.setParam(name, value, category)
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
97
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
98 def __default_ko(self, failure, name, category):
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
99 error ("Can't determine default value for [%s/%s]: %s" % (category, name, str(failure.value)))
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
100
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
101 def setDefault(self, name, category, callback, errback=None):
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
102 """Set default value of parameter
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
103 'default_cb' attibute of parameter must be set to 'yes'
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
104 @param name: name of the parameter
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
105 @param category: category of the parameter
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
106 @param callback: must return a string with the value (use deferred if needed)
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
107 @param errback: must manage the error with args failure, name, category
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
108 """
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
109 #TODO: send signal param update if value changed
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
110 node = self.__getParamNode(name, category)
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
111 if not node:
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
112 error("Requested param [%s] in category [%s] doesn't exist !" , name, category)
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
113 return
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
114 if node.getAttribute('default_cb') == 'yes':
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
115 del node.attributes['default_cb']
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
116 d = defer.maybeDeferred(callback)
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
117 d.addCallback(self.__default_ok, name, category)
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
118 d.addErrback(errback or self.__default_ko, name, category)
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
119
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
120 def getParamA(self, name, category, attr="value"):
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
121 """Helper method to get a specific attribute
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
122 @param name: name of the parameter
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
123 @param category: category of the parameter
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
124 @param attr: name of the attribute (default: "value")
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
125
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
126 @return: attribute"""
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
127 node = self.__getParamNode(name, category)
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
128 if not node:
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
129 error("Requested param [%s] in category [%s] doesn't exist !" , name, category)
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
130 return None
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
131 return node.getAttribute(attr)
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
132
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
133 def getParams(self):
18
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
134 """Return the whole params XML"""
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
135 return self.dom.toxml()
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
136
18
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
137 def getParamsForCategory(self, category):
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
138 """Return node's xml for selected category"""
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
139 for node in self.dom.documentElement.childNodes:
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
140 if node.nodeName == "category" and node.getAttribute("name") == category:
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
141 return node.toxml()
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
142 return "<category />"
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
143
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
144 def __getParamNode(self, name, category):
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
145 for node in self.dom.documentElement.childNodes:
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
146 if node.nodeName == "category" and node.getAttribute("name") == category:
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
147 params = node.getElementsByTagName("param")
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
148 for param in params:
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
149 if param.getAttribute("name") == name:
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
150 return param
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
151 return None
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
152
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
153 def getParamsCategories(self):
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
154 """return the categories availables"""
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
155 categories=[]
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
156 for cat in self.dom.getElementsByTagName("category"):
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
157 categories.append(cat.getAttribute("name"))
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
158 return categories
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
159
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
160 def setParam(self, name, value, category):
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
161 node = self.__getParamNode(name, category)
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
162 if not node:
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
163 return #TODO: throw an error
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
164 type = node.getAttribute("type")
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
165 if type=="button":
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
166 print "clique",node.toxml()
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
167 else:
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
168 node.setAttribute("value", value)
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
169 self.host.bridge.paramUpdate(name, value, category)
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
170
0
goffi@necton2
parents:
diff changeset
171 class Memory:
goffi@necton2
parents:
diff changeset
172 """This class manage all persistent informations"""
goffi@necton2
parents:
diff changeset
173
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
174 def __init__(self, host):
0
goffi@necton2
parents:
diff changeset
175 info ("Memory manager init")
goffi@necton2
parents:
diff changeset
176 self.contact={}
goffi@necton2
parents:
diff changeset
177 self.presenceStatus={}
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
178 self.params=Param(host)
0
goffi@necton2
parents:
diff changeset
179 self.history={}
goffi@necton2
parents:
diff changeset
180 self.disco={} #XXX: maybe best in a separate class
goffi@necton2
parents:
diff changeset
181 self.features={}
goffi@necton2
parents:
diff changeset
182 self.load()
goffi@necton2
parents:
diff changeset
183
goffi@necton2
parents:
diff changeset
184 def load(self):
goffi@necton2
parents:
diff changeset
185 """Load parameters and all memory things from file/db"""
goffi@necton2
parents:
diff changeset
186
goffi@necton2
parents:
diff changeset
187 #first parameters
goffi@necton2
parents:
diff changeset
188 if os.path.exists(const_SAVEFILE_PARAM):
goffi@necton2
parents:
diff changeset
189 try:
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
190 #gof: FIXME FIXME FIXME: sauver l'xml et non plus le pickle !!!!!!!
0
goffi@necton2
parents:
diff changeset
191 with open(const_SAVEFILE_PARAM, 'r') as params_pickle:
goffi@necton2
parents:
diff changeset
192 self.params=pickle.load(params_pickle)
goffi@necton2
parents:
diff changeset
193 debug("params loaded")
goffi@necton2
parents:
diff changeset
194 except:
goffi@necton2
parents:
diff changeset
195 error ("Can't load params !")
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
196 self.params.load_default_params()
0
goffi@necton2
parents:
diff changeset
197 else:
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
198 error ("No params, using default parameters")
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
199 self.params.load_default_params()
0
goffi@necton2
parents:
diff changeset
200
goffi@necton2
parents:
diff changeset
201 #history
goffi@necton2
parents:
diff changeset
202 if os.path.exists(const_SAVEFILE_HISTORY):
goffi@necton2
parents:
diff changeset
203 try:
goffi@necton2
parents:
diff changeset
204 with open(const_SAVEFILE_HISTORY, 'r') as history_pickle:
goffi@necton2
parents:
diff changeset
205 self.history=pickle.load(history_pickle)
goffi@necton2
parents:
diff changeset
206 debug("history loaded")
goffi@necton2
parents:
diff changeset
207 except:
goffi@necton2
parents:
diff changeset
208 error ("Can't load history !")
goffi@necton2
parents:
diff changeset
209
goffi@necton2
parents:
diff changeset
210
goffi@necton2
parents:
diff changeset
211 def save(self):
goffi@necton2
parents:
diff changeset
212 """Save parameters and all memory things to file/db"""
goffi@necton2
parents:
diff changeset
213 with open(const_SAVEFILE_PARAM, 'w') as params_pickle:
goffi@necton2
parents:
diff changeset
214 pickle.dump(self.params, params_pickle)
goffi@necton2
parents:
diff changeset
215 with open(const_SAVEFILE_HISTORY, 'w') as history_pickle:
goffi@necton2
parents:
diff changeset
216 pickle.dump(self.history, history_pickle)
goffi@necton2
parents:
diff changeset
217
goffi@necton2
parents:
diff changeset
218 def addToHistory(self, me_jid, from_jid, to_jid, type, message):
goffi@necton2
parents:
diff changeset
219 me_short=me_jid.userhost()
goffi@necton2
parents:
diff changeset
220 from_short=from_jid.userhost()
goffi@necton2
parents:
diff changeset
221 to_short=to_jid.userhost()
goffi@necton2
parents:
diff changeset
222
goffi@necton2
parents:
diff changeset
223 if from_jid==me_jid:
goffi@necton2
parents:
diff changeset
224 key=to_short
goffi@necton2
parents:
diff changeset
225 else:
goffi@necton2
parents:
diff changeset
226 key=from_short
goffi@necton2
parents:
diff changeset
227
goffi@necton2
parents:
diff changeset
228 if not self.history.has_key(me_short):
goffi@necton2
parents:
diff changeset
229 self.history[me_short]={}
goffi@necton2
parents:
diff changeset
230 if not self.history[me_short].has_key(key):
goffi@necton2
parents:
diff changeset
231 self.history[me_short][key]={}
goffi@necton2
parents:
diff changeset
232
goffi@necton2
parents:
diff changeset
233 self.history[me_short][key][int(time.time())] = (from_short, message)
goffi@necton2
parents:
diff changeset
234
goffi@necton2
parents:
diff changeset
235 def getHistory(self, from_jid, to_jid, size):
goffi@necton2
parents:
diff changeset
236 ret={}
goffi@necton2
parents:
diff changeset
237 if not self.history.has_key(from_jid):
goffi@necton2
parents:
diff changeset
238 error("source JID not found !")
goffi@necton2
parents:
diff changeset
239 #TODO: throw an error here
goffi@necton2
parents:
diff changeset
240 return {}
goffi@necton2
parents:
diff changeset
241 if not self.history[from_jid].has_key(to_jid):
goffi@necton2
parents:
diff changeset
242 error("dest JID not found !")
goffi@necton2
parents:
diff changeset
243 #TODO: throw an error here
goffi@necton2
parents:
diff changeset
244 return {}
goffi@necton2
parents:
diff changeset
245 stamps=self.history[from_jid][to_jid].keys()
goffi@necton2
parents:
diff changeset
246 stamps.sort()
goffi@necton2
parents:
diff changeset
247 for stamp in stamps[-size:]:
goffi@necton2
parents:
diff changeset
248 ret[stamp]=self.history[from_jid][to_jid][stamp]
goffi@necton2
parents:
diff changeset
249
goffi@necton2
parents:
diff changeset
250 return ret
goffi@necton2
parents:
diff changeset
251
goffi@necton2
parents:
diff changeset
252 def addContact(self, JID, attributes, groups):
goffi@necton2
parents:
diff changeset
253 debug("Memory addContact: %s",JID)
goffi@necton2
parents:
diff changeset
254 assert(isinstance(attributes,dict))
13
bd9e9997d540 wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents: 0
diff changeset
255 assert(isinstance(groups,set))
0
goffi@necton2
parents:
diff changeset
256 self.contact[JID]=[attributes, groups]
goffi@necton2
parents:
diff changeset
257
goffi@necton2
parents:
diff changeset
258 def addPresenceStatus(self, jid, type, show, status, priority):
goffi@necton2
parents:
diff changeset
259 self.presenceStatus[jid]=[type, show, status, priority]
goffi@necton2
parents:
diff changeset
260
goffi@necton2
parents:
diff changeset
261 def getContacts(self):
goffi@necton2
parents:
diff changeset
262 debug ("Memory getContact OK (%s)", self.contact)
goffi@necton2
parents:
diff changeset
263 ret=[]
goffi@necton2
parents:
diff changeset
264 for contact in self.contact:
goffi@necton2
parents:
diff changeset
265 ret.append([contact] + [self.contact[contact][0]] + [self.contact[contact][1]]) #very ugly I know !
goffi@necton2
parents:
diff changeset
266 return ret
goffi@necton2
parents:
diff changeset
267
goffi@necton2
parents:
diff changeset
268 def getPresenceStatus(self):
goffi@necton2
parents:
diff changeset
269 status=[]
goffi@necton2
parents:
diff changeset
270 for contact, contactStatus in self.presenceStatus.items():
goffi@necton2
parents:
diff changeset
271 status.append([contact]+contactStatus)
goffi@necton2
parents:
diff changeset
272 debug ("Memory getPresenceStatus (%s)", status)
goffi@necton2
parents:
diff changeset
273 return status
goffi@necton2
parents:
diff changeset
274
22
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
275 def getParamA(self, name, category, attr="value"):
bb72c29f3432 added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents: 21
diff changeset
276 return self.params.getParamA(name, category, attr)
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
277
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
278 def getParams(self):
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
279 return self.params.getParams()
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
280
18
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
281 def getParamsForCategory(self, category):
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
282 return self.params.getParamsForCategory(category)
6928e3cb73a8 refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents: 17
diff changeset
283
0
goffi@necton2
parents:
diff changeset
284 def getParamsCategories(self):
17
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
285 return self.params.getParamsCategories()
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
286
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
287 def setParam(self, name, value, category):
74a39f40eb6d refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents: 14
diff changeset
288 return self.params.setParam(name, value, category)
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 18
diff changeset
289
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
290 def importParams(self, parent, xml):
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
291 return self.params.importParams(parent, xml)
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
292
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
293 def setDefault(self, name, category, callback, errback=None):
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
294 return self.params.setDefault(name, category, callback, errback)