Mercurial > libervia-backend
annotate tools/memory.py @ 64:d46f849664aa
SàT: multi-profile, plugins updated
- core: 2 new convenient methods: getJidNStream and getClient
- new param in plugin info: "handler" to know if there is a handler to plug on profiles clients
- plugins with handler now use an other class which is returned to profile client with the new method "getHandler" and pluged when connecting
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 30 Jan 2010 16:17:33 +1100 |
parents | 0db25931b60d |
children | d35c5edab53f |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT: a jabber client | |
57 | 6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
0 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from __future__ import with_statement | |
23 | |
24 import os.path | |
25 import time | |
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 | 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 |
47 | 31 from twisted.words.protocols.jabber import jid |
0 | 32 |
61 | 33 SAVEFILE_PARAM_XML="/param" #xml parameters template |
34 SAVEFILE_PARAM_DATA="/param" #individual & general parameters; _ind and _gen suffixes will be added | |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
35 SAVEFILE_HISTORY="/history" |
47 | 36 SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins) |
0 | 37 |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
38 class Param(): |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
39 """This class manage parameter with xml""" |
19
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
40 ### TODO: add desciption in params |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
41 |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
42 #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
|
43 default_xml = u""" |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
44 <params> |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
45 <general> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
46 </general> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
47 <individual> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
48 <category name="Connection"> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
49 <param name="JabberID" value="goffi@necton2.int/TestScript" type="string" /> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
50 <param name="Password" value="toto" type="password" /> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
51 <param name="Server" value="necton2.int" type="string" /> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
52 <param name="NewAccount" value="Register new account" type="button" callback="registerNewAccount"/> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
53 </category> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
54 <category name="Misc"> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
55 <param name="Watched" value="test@Jabber.goffi.int" type="string" /> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
56 </category> |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
57 </individual> |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
58 </params> |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
59 """ |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
60 |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
61 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
|
62 self.dom = minidom.parseString(Param.default_xml.encode('utf-8')) |
38 | 63 |
61 | 64 def load_xml(self, file): |
65 """Load parameters template from file""" | |
38 | 66 self.dom = minidom.parse(file) |
67 | |
61 | 68 def load_data(self, file): |
69 """Load parameters data from file""" | |
70 file_ind = file + '_ind' | |
71 file_gen = file + '_gen' | |
72 | |
73 if os.path.exists(file_gen): | |
74 try: | |
75 with open(file_gen, 'r') as file_gen_pickle: | |
76 self.params_gen=pickle.load(file_gen_pickle) | |
77 debug("general params data loaded") | |
78 except: | |
79 error ("Can't load general params data !") | |
80 | |
81 if os.path.exists(file_ind): | |
82 try: | |
83 with open(file_ind, 'r') as file_ind_pickle: | |
84 self.params=pickle.load(file_ind_pickle) | |
85 debug("individual params data loaded") | |
86 except: | |
87 error ("Can't load individual params data !") | |
88 | |
89 def save_xml(self, file): | |
90 """Save parameters template to xml file""" | |
38 | 91 with open(file, 'wb') as xml_file: |
92 self.dom.writexml(xml_file) | |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
93 |
61 | 94 def save_data(self, file): |
95 """Save parameters data to file""" | |
96 #TODO: save properly in a separate file/database, | |
97 # use different behaviour depending of the data type (e.g. password encrypted) | |
98 | |
99 #general params | |
100 with open(file+'_gen', 'w') as param_gen_pickle: | |
101 pickle.dump(self.params_gen, param_gen_pickle) | |
102 | |
103 #then individual params | |
104 with open(file+'_ind', 'w') as param_ind_pickle: | |
105 pickle.dump(self.params, param_ind_pickle) | |
106 | |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
107 def __init__(self, host): |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
108 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
|
109 self.host = host |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
110 self.default_profile = None |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
111 self.params = {'goffi':{}} #gof: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
112 self.params_gen = {} |
61 | 113 host.set_const('savefile_param_xml', SAVEFILE_PARAM_XML) |
114 host.set_const('savefile_param_data', SAVEFILE_PARAM_DATA) | |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
115 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB) |
19
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
116 |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
117 def getProfilesList(self): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
118 return self.params.keys() |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
119 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
120 def createProfile(self, name, default=False): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
121 """Create a new profile |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
122 @param name: Name of the profile |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
123 @param default: True if default value""" |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
124 if self.params.has_key(name): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
125 info ('The profile name already exists') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
126 return 1 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
127 self.params[name]={} |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
128 return 0 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
129 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
130 def getProfileName(self, profile_key): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
131 """return profile according to profile_key |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
132 @param profile_key: profile name or key which can be |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
133 @ALL@ for all profiles |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
134 @DEFAULT@ for default profile |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
135 @return: requested profile name or None if it doesn't exist""" |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
136 if profile_key=='@DEFAULT@': |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
137 if not self.params: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
138 return None |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
139 info('No default profile, returning first one') #TODO: manage real default profile |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
140 return self.params.keys()[0] #FIXME: gof: temporary, must use real default value, and fallback to first one if it doesn't exists |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
141 if not self.params.has_key(profile_key): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
142 error ('Trying to access an unknown profile') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
143 return None |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
144 return profile_key |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
145 |
19
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
146 def __get_unique_node(self, parent, tag, name): |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
147 """return node with given tag |
19
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
148 @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
|
149 @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
|
150 @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
|
151 @return: node if it exist or None |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
152 """ |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
153 for node in parent.childNodes: |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
154 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
|
155 #the node already exists |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
156 return node |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
157 #the node is new |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
158 return None |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
159 |
38 | 160 def importParams(self, xml): |
19
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
161 """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
|
162 @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
|
163 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
|
164 |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
165 def import_node(tgt_parent, src_parent): |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
166 for child in src_parent.childNodes: |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
167 if child.nodeName == '#text': |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
168 continue |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
169 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
|
170 if not node: #The node is new |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
171 tgt_parent.appendChild(child) |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
172 else: |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
173 import_node(node, child) |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
174 |
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
175 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
|
176 |
20
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
177 def __default_ok(self, value, name, category): |
63
0db25931b60d
SàT: multi-profiles: somes fixes in core
Goffi <goffi@goffi.org>
parents:
62
diff
changeset
|
178 #FIXME: gof: will not work with individual parameters |
61 | 179 self.setParam(name, value, category) #FIXME: better to set param xml value ??? |
20
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
180 |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
181 def __default_ko(self, failure, name, category): |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
182 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
|
183 |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
184 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
|
185 """Set default value of parameter |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
186 '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
|
187 @param name: name of the parameter |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
188 @param category: category of the parameter |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
189 @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
|
190 @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
|
191 """ |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
192 #TODO: send signal param update if value changed |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
193 node = self.__getParamNode(name, category, '@ALL@') |
20
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
194 if not node: |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
195 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
|
196 return |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
197 if node[1].getAttribute('default_cb') == 'yes': |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
198 del node[1].attributes['default_cb'] |
20
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
199 d = defer.maybeDeferred(callback) |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
200 d.addCallback(self.__default_ok, name, category) |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
201 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
|
202 |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
203 def getParamA(self, name, category, attr="value", profile_key="@DEFAULT@"): |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
204 """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
|
205 @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
|
206 @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
|
207 @param attr: name of the attribute (default: "value") |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
208 @param profile: owner of the param (@ALL@ for everyone) |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
209 |
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
210 @return: attribute""" |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
211 node = self.__getParamNode(name, category) |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
212 if not node: |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
213 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
|
214 return None |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
215 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
216 if node[0] == 'general': |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
217 value = self.__getParam(None, category, name, 'general') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
218 return value or node[1].getAttribute(attr) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
219 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
220 assert(node[0] == 'individual') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
221 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
222 profile = self.getProfileName(profile_key) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
223 if not profile: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
224 error('Requesting a param for an non-existant profile') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
225 return None |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
226 |
64 | 227 if attr == "value": |
228 return self.__getParam(profile, category, name) or node[1].getAttribute(attr) | |
229 else: | |
230 return node[1].getAttribute(attr) | |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
231 |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
232 |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
233 def __getParam(self, profile, category, name, type='individual'): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
234 """Return the param, or None if it doesn't exist |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
235 @param profile: the profile name (not profile key, i.e. name and not something like @DEFAULT@) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
236 @param category: param category |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
237 @param name: param name |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
238 """ |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
239 if type == 'general': |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
240 if self.params_gen.has_key((category, name)): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
241 return self.params_gen[(category, name)] |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
242 return None #This general param has the default value |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
243 assert (type == 'individual') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
244 if not self.params.has_key(profile) or not self.params[profile].has_key((category, name)): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
245 return None |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
246 return self.params[profile][(category, name)] |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
247 |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
248 def __constructProfileXml(self, profile): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
249 """Construct xml for asked profile, filling values when needed |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
250 /!\ as noticed in doc, don't forget to unlink the minidom.Document |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
251 @param profile: profile name (not key !) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
252 @return: minidom.Document of the profile xml (cf warning above) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
253 """ |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
254 prof_xml = minidom.parseString('<params/>') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
255 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
256 for type_node in self.dom.documentElement.childNodes: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
257 if type_node.nodeName == 'general' or type_node.nodeName == 'individual': #we use all params, general and individual |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
258 for cat_node in type_node.childNodes: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
259 if cat_node.nodeName == 'category': |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
260 category = cat_node.getAttribute('name') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
261 cat_copy = cat_node.cloneNode(True) #we make a copy for the new xml |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
262 params = cat_copy.getElementsByTagName("param") |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
263 for param_node in params: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
264 name = param_node.getAttribute('name') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
265 profile_value = self.__getParam(profile, category, name, type_node.nodeName) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
266 if profile_value: #there is a value for this profile, we must change the default |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
267 param_node.setAttribute('value', profile_value) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
268 prof_xml.documentElement.appendChild(cat_copy) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
269 return prof_xml |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
270 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
271 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
272 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
273 def getParams(self, profile_key='@DEFAULT@'): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
274 """Construct xml for asked profile |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
275 Take params xml as skeleton""" |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
276 profile = self.getProfileName(profile_key) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
277 if not profile: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
278 error("Asking params for inexistant profile") |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
279 return "" |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
280 prof_xml = self.__constructProfileXml(profile) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
281 return_xml = prof_xml.toxml() |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
282 prof_xml.unlink() |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
283 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
284 return return_xml |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
285 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
286 def getParamsForCategory(self, category, profile_key='@DEFAULT@'): |
18
6928e3cb73a8
refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
287 """Return node's xml for selected category""" |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
288 #TODO: manage category of general type (without existant profile) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
289 profile = self.getProfileName(profile_key) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
290 if not profile: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
291 error("Asking params for inexistant profile") |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
292 return "" |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
293 prof_xml = self.__constructProfileXml(profile) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
294 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
295 for node in prof_xml.getElementsByTagName("category"): |
18
6928e3cb73a8
refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
296 if node.nodeName == "category" and node.getAttribute("name") == category: |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
297 result = node.toxml() |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
298 prof_xml.unlink() |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
299 return result |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
300 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
301 prof_xml.unlink() |
18
6928e3cb73a8
refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
302 return "<category />" |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
303 |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
304 def __getParamNode(self, name, category, type="@ALL@"): #FIXME: is type useful ? |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
305 """Return a node from the param_xml |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
306 @param name: name of the node |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
307 @param category: category of the node |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
308 @type: keyword for search: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
309 @ALL@ search everywhere |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
310 @GENERAL@ only search in general type |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
311 @INDIVIDUAL@ only search in individual type |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
312 @return: a tuple with the node type and the the node, or None if not found""" |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
313 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
314 for type_node in self.dom.documentElement.childNodes: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
315 if ( ((type == "@ALL@" or type == "@GENERAL@") and type_node.nodeName == 'general') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
316 or ( (type == "@ALL@" or type == "@INDIVIDUAL@") and type_node.nodeName == 'individual') ): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
317 for node in type_node.getElementsByTagName('category'): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
318 if node.getAttribute("name") == category: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
319 params = node.getElementsByTagName("param") |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
320 for param in params: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
321 if param.getAttribute("name") == name: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
322 return (type_node.nodeName, param) |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
323 return None |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
324 |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
325 def getParamsCategories(self): |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
326 """return the categories availables""" |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
327 categories=[] |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
328 for cat in self.dom.getElementsByTagName("category"): |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
329 categories.append(cat.getAttribute("name")) |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
330 return categories |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
331 |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
332 def setParam(self, name, value, category, profile_key='@DEFAULT@'): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
333 """Set a parameter, return None if the parameter is not in param xml""" |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
334 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
335 node = self.__getParamNode(name, category, '@ALL@') |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
336 if not node: |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
337 error('Requesting an unknown parameter (%s/%s)' % (category, name)) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
338 return |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
339 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
340 if node[0] == 'general': |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
341 self.params_gen[(category, name)] = value |
61 | 342 self.host.bridge.paramUpdate(name, value, category) #TODO: add profile in signal |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
343 return |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
344 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
345 assert (node[0] == 'individual') |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
346 |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
347 profile = self.getProfileName(profile_key) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
348 if not profile: |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
349 error('Trying to set parameter for an unknown profile') |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
350 return #TODO: throw an error |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
351 |
61 | 352 type = node[1].getAttribute("type") |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
353 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
|
354 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
|
355 else: |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
356 self.params[profile][(category, name)] = value |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
357 self.host.bridge.paramUpdate(name, value, category) #TODO: add profile in signal |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
358 |
0 | 359 class Memory: |
360 """This class manage all persistent informations""" | |
361 | |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
362 def __init__(self, host): |
0 | 363 info ("Memory manager init") |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
364 self.host = host |
47 | 365 self.contacts={} |
0 | 366 self.presenceStatus={} |
47 | 367 self.subscriptions={} |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
368 self.params=Param(host) |
47 | 369 self.history={} #used to store chat history (key: short jid) |
370 self.private={} #used to store private value | |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
371 host.set_const('savefile_history', SAVEFILE_HISTORY) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
372 host.set_const('savefile_private', SAVEFILE_PRIVATE) |
0 | 373 self.load() |
374 | |
375 def load(self): | |
376 """Load parameters and all memory things from file/db""" | |
61 | 377 param_file_xml = os.path.expanduser(self.host.get_const('local_dir')+ |
378 self.host.get_const('savefile_param_xml')) | |
379 param_file_data = os.path.expanduser(self.host.get_const('local_dir')+ | |
380 self.host.get_const('savefile_param_data')) | |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
381 history_file = os.path.expanduser(self.host.get_const('local_dir')+ |
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
382 self.host.get_const('savefile_history')) |
47 | 383 private_file = os.path.expanduser(self.host.get_const('local_dir')+ |
384 self.host.get_const('savefile_private')) | |
0 | 385 |
38 | 386 #parameters |
61 | 387 if os.path.exists(param_file_xml): |
0 | 388 try: |
61 | 389 self.params.load_xml(param_file_xml) |
390 debug("params template loaded") | |
0 | 391 except: |
61 | 392 error ("Can't load params template !") |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
393 self.params.load_default_params() |
0 | 394 else: |
61 | 395 info ("No params template, using default template") |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
396 self.params.load_default_params() |
0 | 397 |
61 | 398 try: |
399 self.params.load_data(param_file_data) | |
400 debug("params loaded") | |
401 except: | |
402 error ("Can't load params !") | |
403 | |
0 | 404 #history |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
405 if os.path.exists(history_file): |
0 | 406 try: |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
407 with open(history_file, 'r') as history_pickle: |
0 | 408 self.history=pickle.load(history_pickle) |
409 debug("history loaded") | |
410 except: | |
411 error ("Can't load history !") | |
412 | |
47 | 413 #private |
414 if os.path.exists(private_file): | |
415 try: | |
416 with open(private_file, 'r') as private_pickle: | |
417 self.private=pickle.load(private_pickle) | |
418 debug("private values loaded") | |
419 except: | |
420 error ("Can't load private values !") | |
0 | 421 |
422 def save(self): | |
423 """Save parameters and all memory things to file/db""" | |
38 | 424 #TODO: need to encrypt files (at least passwords !) and set permissions |
61 | 425 param_file_xml = os.path.expanduser(self.host.get_const('local_dir')+ |
426 self.host.get_const('savefile_param_xml')) | |
427 param_file_data = os.path.expanduser(self.host.get_const('local_dir')+ | |
428 self.host.get_const('savefile_param_data')) | |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
429 history_file = os.path.expanduser(self.host.get_const('local_dir')+ |
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
430 self.host.get_const('savefile_history')) |
47 | 431 private_file = os.path.expanduser(self.host.get_const('local_dir')+ |
432 self.host.get_const('savefile_private')) | |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
433 |
61 | 434 self.params.save_xml(param_file_xml) |
435 self.params.save_data(param_file_data) | |
38 | 436 debug("params saved") |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
437 with open(history_file, 'w') as history_pickle: |
0 | 438 pickle.dump(self.history, history_pickle) |
38 | 439 debug("history saved") |
47 | 440 with open(private_file, 'w') as private_pickle: |
441 pickle.dump(self.private, private_pickle) | |
442 debug("private values saved") | |
0 | 443 |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
444 def getProfilesList(self): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
445 return self.params.getProfilesList() |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
446 |
62
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
447 |
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
448 def getProfileName(self, profile_key): |
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
449 """Return name of profile from keyword |
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
450 @param profile_key: can be the profile name or a keywork (like @DEFAULT@) |
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
451 @return: profile name or None if it doesn't exist""" |
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
452 return self.params.getProfileName(profile_key) |
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
453 |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
454 def createProfile(self, name, default=False): |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
455 """Create a new profile |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
456 @param name: Profile name |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
457 @param default: True if default profile (replace previous default) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
458 """ |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
459 return self.params.createProfile(name, default) |
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
460 |
0 | 461 def addToHistory(self, me_jid, from_jid, to_jid, type, message): |
462 me_short=me_jid.userhost() | |
463 from_short=from_jid.userhost() | |
464 to_short=to_jid.userhost() | |
465 | |
466 if from_jid==me_jid: | |
467 key=to_short | |
468 else: | |
469 key=from_short | |
470 | |
471 if not self.history.has_key(me_short): | |
472 self.history[me_short]={} | |
473 if not self.history[me_short].has_key(key): | |
474 self.history[me_short][key]={} | |
475 | |
476 self.history[me_short][key][int(time.time())] = (from_short, message) | |
477 | |
478 def getHistory(self, from_jid, to_jid, size): | |
479 ret={} | |
480 if not self.history.has_key(from_jid): | |
481 error("source JID not found !") | |
482 #TODO: throw an error here | |
483 return {} | |
484 if not self.history[from_jid].has_key(to_jid): | |
485 error("dest JID not found !") | |
486 #TODO: throw an error here | |
487 return {} | |
488 stamps=self.history[from_jid][to_jid].keys() | |
489 stamps.sort() | |
490 for stamp in stamps[-size:]: | |
491 ret[stamp]=self.history[from_jid][to_jid][stamp] | |
492 | |
493 return ret | |
494 | |
47 | 495 def setPrivate(self, key, value): |
496 """Save a misc private value (mainly useful for plugins)""" | |
497 self.private[key] = value | |
498 | |
499 def getPrivate(self, key): | |
500 """return a private value | |
501 @param key: name of wanted value | |
502 @return: value or None if value don't exist""" | |
503 if self.private.has_key(key): | |
504 return self.private[key] | |
505 return None | |
506 | |
507 | |
508 def addContact(self, contact_jid, attributes, groups): | |
509 debug("Memory addContact: %s",contact_jid.userhost()) | |
0 | 510 assert(isinstance(attributes,dict)) |
13
bd9e9997d540
wokkel integration (not finished yet)
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
511 assert(isinstance(groups,set)) |
47 | 512 self.contacts[contact_jid.userhost()]=[attributes, groups] |
0 | 513 |
47 | 514 def delContact(self, contact_jid): |
515 debug("Memory delContact: %s",contact_jid.userhost()) | |
516 if self.contacts.has_key(contact_jid.userhost()): | |
517 del self.contacts[contact_jid.userhost()] | |
518 | |
519 def getContact(self, contact_jid): | |
520 if self.contacts.has_key(contact_jid.userhost()): | |
521 self.contacts[contact_jid.userhost()] | |
522 else: | |
523 return None | |
524 | |
0 | 525 def getContacts(self): |
47 | 526 debug ("Memory getContact OK (%s)", self.contacts) |
0 | 527 ret=[] |
47 | 528 for contact in self.contacts: |
529 attr, groups = self.contacts[contact] | |
530 ret.append([contact, attr, groups ]) | |
0 | 531 return ret |
47 | 532 |
533 def addPresenceStatus(self, contact_jid, show, priority, statuses): | |
534 if not self.presenceStatus.has_key(contact_jid.userhost()): | |
535 self.presenceStatus[contact_jid.userhost()] = {} | |
536 resource = jid.parse(contact_jid.full())[2] or '' | |
537 self.presenceStatus[contact_jid.userhost()][resource] = (show, priority, statuses) | |
538 | |
539 def addWaitingSub(self, type, contact_jid): | |
540 """Called when a subcription request is received""" | |
541 self.subscriptions[contact_jid] = type | |
542 | |
543 def delWaitingSub(self, contact_jid): | |
544 """Called when a subcription request is finished""" | |
545 if self.subscriptions.has_key(contact_jid): | |
546 del self.subscriptions[contact_jid] | |
547 | |
548 def getWaitingSub(self): | |
549 """Called to get a list of currently waiting subscription requests""" | |
550 return self.subscriptions | |
0 | 551 |
552 def getPresenceStatus(self): | |
47 | 553 debug ("Memory getPresenceStatus (%s)", self.presenceStatus) |
554 return self.presenceStatus | |
0 | 555 |
64 | 556 def getParamA(self, name, category, attr="value", profile_key="@DEFAULT@"): |
557 return self.params.getParamA(name, category, attr, profile_key) | |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
558 |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
559 def getParams(self): |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
560 return self.params.getParams() |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
561 |
18
6928e3cb73a8
refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
562 def getParamsForCategory(self, category): |
6928e3cb73a8
refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
563 return self.params.getParamsForCategory(category) |
6928e3cb73a8
refactoring: using xml params part II
Goffi <goffi@goffi.org>
parents:
17
diff
changeset
|
564 |
0 | 565 def getParamsCategories(self): |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
566 return self.params.getParamsCategories() |
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
567 |
63
0db25931b60d
SàT: multi-profiles: somes fixes in core
Goffi <goffi@goffi.org>
parents:
62
diff
changeset
|
568 def setParam(self, name, value, category, profile_key='@DEFAULT@'): |
0db25931b60d
SàT: multi-profiles: somes fixes in core
Goffi <goffi@goffi.org>
parents:
62
diff
changeset
|
569 return self.params.setParam(name, value, category, profile_key) |
19
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
570 |
38 | 571 def importParams(self, xml): |
572 return self.params.importParams(xml) | |
20
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
573 |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
574 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
|
575 return self.params.setDefault(name, category, callback, errback) |