Mercurial > libervia-backend
comparison sat/memory/params.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | 5f546dd910e0 |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
27 log = getLogger(__name__) | 27 log = getLogger(__name__) |
28 from twisted.internet import defer | 28 from twisted.internet import defer |
29 from twisted.python.failure import Failure | 29 from twisted.python.failure import Failure |
30 from twisted.words.xish import domish | 30 from twisted.words.xish import domish |
31 from twisted.words.protocols.jabber import jid | 31 from twisted.words.protocols.jabber import jid |
32 from sat.tools.xml_tools import paramsXML2XMLUI, getText | 32 from sat.tools.xml_tools import params_xml_2_xmlui, get_text |
33 from sat.tools.common import data_format | 33 from sat.tools.common import data_format |
34 from xml.sax.saxutils import quoteattr | 34 from xml.sax.saxutils import quoteattr |
35 | 35 |
36 # TODO: params should be rewritten using Twisted directly instead of minidom | 36 # TODO: params should be rewritten using Twisted directly instead of minidom |
37 # general params should be linked to sat.conf and kept synchronised | 37 # general params should be linked to sat.conf and kept synchronised |
38 # this need an overall simplification to make maintenance easier | 38 # this need an overall simplification to make maintenance easier |
39 | 39 |
40 | 40 |
41 def createJidElts(jids): | 41 def create_jid_elts(jids): |
42 """Generator which return <jid/> elements from jids | 42 """Generator which return <jid/> elements from jids |
43 | 43 |
44 @param jids(iterable[id.jID]): jids to use | 44 @param jids(iterable[id.jID]): jids to use |
45 @return (generator[domish.Element]): <jid/> elements | 45 @return (generator[domish.Element]): <jid/> elements |
46 """ | 46 """ |
99 } | 99 } |
100 | 100 |
101 def load_default_params(self): | 101 def load_default_params(self): |
102 self.dom = minidom.parseString(Params.default_xml.encode("utf-8")) | 102 self.dom = minidom.parseString(Params.default_xml.encode("utf-8")) |
103 | 103 |
104 def _mergeParams(self, source_node, dest_node): | 104 def _merge_params(self, source_node, dest_node): |
105 """Look for every node in source_node and recursively copy them to dest if they don't exists""" | 105 """Look for every node in source_node and recursively copy them to dest if they don't exists""" |
106 | 106 |
107 def getNodesMap(children): | 107 def get_nodes_map(children): |
108 ret = {} | 108 ret = {} |
109 for child in children: | 109 for child in children: |
110 if child.nodeType == child.ELEMENT_NODE: | 110 if child.nodeType == child.ELEMENT_NODE: |
111 ret[(child.tagName, child.getAttribute("name"))] = child | 111 ret[(child.tagName, child.getAttribute("name"))] = child |
112 return ret | 112 return ret |
113 | 113 |
114 source_map = getNodesMap(source_node.childNodes) | 114 source_map = get_nodes_map(source_node.childNodes) |
115 dest_map = getNodesMap(dest_node.childNodes) | 115 dest_map = get_nodes_map(dest_node.childNodes) |
116 source_set = set(source_map.keys()) | 116 source_set = set(source_map.keys()) |
117 dest_set = set(dest_map.keys()) | 117 dest_set = set(dest_map.keys()) |
118 to_add = source_set.difference(dest_set) | 118 to_add = source_set.difference(dest_set) |
119 | 119 |
120 for node_key in to_add: | 120 for node_key in to_add: |
121 dest_node.appendChild(source_map[node_key].cloneNode(True)) | 121 dest_node.appendChild(source_map[node_key].cloneNode(True)) |
122 | 122 |
123 to_recurse = source_set - to_add | 123 to_recurse = source_set - to_add |
124 for node_key in to_recurse: | 124 for node_key in to_recurse: |
125 self._mergeParams(source_map[node_key], dest_map[node_key]) | 125 self._merge_params(source_map[node_key], dest_map[node_key]) |
126 | 126 |
127 def load_xml(self, xml_file): | 127 def load_xml(self, xml_file): |
128 """Load parameters template from xml file""" | 128 """Load parameters template from xml file""" |
129 self.dom = minidom.parse(xml_file) | 129 self.dom = minidom.parse(xml_file) |
130 default_dom = minidom.parseString(Params.default_xml.encode("utf-8")) | 130 default_dom = minidom.parseString(Params.default_xml.encode("utf-8")) |
131 self._mergeParams(default_dom.documentElement, self.dom.documentElement) | 131 self._merge_params(default_dom.documentElement, self.dom.documentElement) |
132 | 132 |
133 def loadGenParams(self): | 133 def load_gen_params(self): |
134 """Load general parameters data from storage | 134 """Load general parameters data from storage |
135 | 135 |
136 @return: deferred triggered once params are loaded | 136 @return: deferred triggered once params are loaded |
137 """ | 137 """ |
138 return self.storage.loadGenParams(self.params_gen) | 138 return self.storage.load_gen_params(self.params_gen) |
139 | 139 |
140 def loadIndParams(self, profile, cache=None): | 140 def load_ind_params(self, profile, cache=None): |
141 """Load individual parameters | 141 """Load individual parameters |
142 | 142 |
143 set self.params cache or a temporary cache | 143 set self.params cache or a temporary cache |
144 @param profile: profile to load (*must exist*) | 144 @param profile: profile to load (*must exist*) |
145 @param cache: if not None, will be used to store the value, as a short time cache | 145 @param cache: if not None, will be used to store the value, as a short time cache |
146 @return: deferred triggered once params are loaded | 146 @return: deferred triggered once params are loaded |
147 """ | 147 """ |
148 if cache is None: | 148 if cache is None: |
149 self.params[profile] = {} | 149 self.params[profile] = {} |
150 return self.storage.loadIndParams( | 150 return self.storage.load_ind_params( |
151 self.params[profile] if cache is None else cache, profile | 151 self.params[profile] if cache is None else cache, profile |
152 ) | 152 ) |
153 | 153 |
154 def purgeProfile(self, profile): | 154 def purge_profile(self, profile): |
155 """Remove cache data of a profile | 155 """Remove cache data of a profile |
156 | 156 |
157 @param profile: %(doc_profile)s | 157 @param profile: %(doc_profile)s |
158 """ | 158 """ |
159 try: | 159 try: |
174 self.storage = storage | 174 self.storage = storage |
175 self.default_profile = None | 175 self.default_profile = None |
176 self.params = {} | 176 self.params = {} |
177 self.params_gen = {} | 177 self.params_gen = {} |
178 | 178 |
179 def createProfile(self, profile, component): | 179 def create_profile(self, profile, component): |
180 """Create a new profile | 180 """Create a new profile |
181 | 181 |
182 @param profile(unicode): name of the profile | 182 @param profile(unicode): name of the profile |
183 @param component(unicode): entry point if profile is a component | 183 @param component(unicode): entry point if profile is a component |
184 @param callback: called when the profile actually exists in database and memory | 184 @param callback: called when the profile actually exists in database and memory |
185 @return: a Deferred instance | 185 @return: a Deferred instance |
186 """ | 186 """ |
187 if self.storage.hasProfile(profile): | 187 if self.storage.has_profile(profile): |
188 log.info(_("The profile name already exists")) | 188 log.info(_("The profile name already exists")) |
189 return defer.fail(exceptions.ConflictError()) | 189 return defer.fail(exceptions.ConflictError()) |
190 if not self.host.trigger.point("ProfileCreation", profile): | 190 if not self.host.trigger.point("ProfileCreation", profile): |
191 return defer.fail(exceptions.CancelError()) | 191 return defer.fail(exceptions.CancelError()) |
192 return self.storage.createProfile(profile, component or None) | 192 return self.storage.create_profile(profile, component or None) |
193 | 193 |
194 def asyncDeleteProfile(self, profile, force=False): | 194 def profile_delete_async(self, profile, force=False): |
195 """Delete an existing profile | 195 """Delete an existing profile |
196 | 196 |
197 @param profile: name of the profile | 197 @param profile: name of the profile |
198 @param force: force the deletion even if the profile is connected. | 198 @param force: force the deletion even if the profile is connected. |
199 To be used for direct calls only (not through the bridge). | 199 To be used for direct calls only (not through the bridge). |
200 @return: a Deferred instance | 200 @return: a Deferred instance |
201 """ | 201 """ |
202 if not self.storage.hasProfile(profile): | 202 if not self.storage.has_profile(profile): |
203 log.info(_("Trying to delete an unknown profile")) | 203 log.info(_("Trying to delete an unknown profile")) |
204 return defer.fail(Failure(exceptions.ProfileUnknownError(profile))) | 204 return defer.fail(Failure(exceptions.ProfileUnknownError(profile))) |
205 if self.host.isConnected(profile): | 205 if self.host.is_connected(profile): |
206 if force: | 206 if force: |
207 self.host.disconnect(profile) | 207 self.host.disconnect(profile) |
208 else: | 208 else: |
209 log.info(_("Trying to delete a connected profile")) | 209 log.info(_("Trying to delete a connected profile")) |
210 return defer.fail(Failure(exceptions.ProfileConnected)) | 210 return defer.fail(Failure(exceptions.ProfileConnected)) |
211 return self.storage.deleteProfile(profile) | 211 return self.storage.delete_profile(profile) |
212 | 212 |
213 def getProfileName(self, profile_key, return_profile_keys=False): | 213 def get_profile_name(self, profile_key, return_profile_keys=False): |
214 """return profile according to profile_key | 214 """return profile according to profile_key |
215 | 215 |
216 @param profile_key: profile name or key which can be | 216 @param profile_key: profile name or key which can be |
217 C.PROF_KEY_ALL for all profiles | 217 C.PROF_KEY_ALL for all profiles |
218 C.PROF_KEY_DEFAULT for default profile | 218 C.PROF_KEY_DEFAULT for default profile |
227 if not default: | 227 if not default: |
228 log.info(_("No default profile, returning first one")) | 228 log.info(_("No default profile, returning first one")) |
229 try: | 229 try: |
230 default = self.host.memory.memory_data[ | 230 default = self.host.memory.memory_data[ |
231 "Profile_default" | 231 "Profile_default" |
232 ] = self.storage.getProfilesList()[0] | 232 ] = self.storage.get_profiles_list()[0] |
233 except IndexError: | 233 except IndexError: |
234 log.info(_("No profile exist yet")) | 234 log.info(_("No profile exist yet")) |
235 raise exceptions.ProfileUnknownError(profile_key) | 235 raise exceptions.ProfileUnknownError(profile_key) |
236 return ( | 236 return ( |
237 default | 237 default |
238 ) # FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists | 238 ) # FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists |
239 elif profile_key == C.PROF_KEY_NONE: | 239 elif profile_key == C.PROF_KEY_NONE: |
240 raise exceptions.ProfileNotSetError | 240 raise exceptions.ProfileNotSetError |
241 elif return_profile_keys and profile_key in [C.PROF_KEY_ALL]: | 241 elif return_profile_keys and profile_key in [C.PROF_KEY_ALL]: |
242 return profile_key # this value must be managed by the caller | 242 return profile_key # this value must be managed by the caller |
243 if not self.storage.hasProfile(profile_key): | 243 if not self.storage.has_profile(profile_key): |
244 log.error(_("Trying to access an unknown profile (%s)") % profile_key) | 244 log.error(_("Trying to access an unknown profile (%s)") % profile_key) |
245 raise exceptions.ProfileUnknownError(profile_key) | 245 raise exceptions.ProfileUnknownError(profile_key) |
246 return profile_key | 246 return profile_key |
247 | 247 |
248 def __get_unique_node(self, parent, tag, name): | 248 def __get_unique_node(self, parent, tag, name): |
258 # the node already exists | 258 # the node already exists |
259 return node | 259 return node |
260 # the node is new | 260 # the node is new |
261 return None | 261 return None |
262 | 262 |
263 def updateParams(self, xml, security_limit=C.NO_SECURITY_LIMIT, app=""): | 263 def update_params(self, xml, security_limit=C.NO_SECURITY_LIMIT, app=""): |
264 """import xml in parameters, update if the param already exists | 264 """import xml in parameters, update if the param already exists |
265 | 265 |
266 If security_limit is specified and greater than -1, the parameters | 266 If security_limit is specified and greater than -1, the parameters |
267 that have a security level greater than security_limit are skipped. | 267 that have a security level greater than security_limit are skipped. |
268 @param xml: parameters in xml form | 268 @param xml: parameters in xml form |
285 continue | 285 continue |
286 to_remove_count = ( | 286 to_remove_count = ( |
287 0 | 287 0 |
288 ) # count the params to be removed from current category | 288 ) # count the params to be removed from current category |
289 for node in cat_node.childNodes: | 289 for node in cat_node.childNodes: |
290 if node.nodeName != "param" or not self.checkSecurityLimit( | 290 if node.nodeName != "param" or not self.check_security_limit( |
291 node, security_limit | 291 node, security_limit |
292 ): | 292 ): |
293 to_remove.append(node) | 293 to_remove.append(node) |
294 to_remove_count += 1 | 294 to_remove_count += 1 |
295 continue | 295 continue |
322 | 322 |
323 if app: | 323 if app: |
324 pre_process_app_node(src_parent, security_limit, app) | 324 pre_process_app_node(src_parent, security_limit, app) |
325 import_node(self.dom.documentElement, src_parent) | 325 import_node(self.dom.documentElement, src_parent) |
326 | 326 |
327 def paramsRegisterApp(self, xml, security_limit, app): | 327 def params_register_app(self, xml, security_limit, app): |
328 """Register frontend's specific parameters | 328 """Register frontend's specific parameters |
329 | 329 |
330 If security_limit is specified and greater than -1, the parameters | 330 If security_limit is specified and greater than -1, the parameters |
331 that have a security level greater than security_limit are skipped. | 331 that have a security level greater than security_limit are skipped. |
332 @param xml: XML definition of the parameters to be added | 332 @param xml: XML definition of the parameters to be added |
349 % {"app": app} | 349 % {"app": app} |
350 ) | 350 ) |
351 ) | 351 ) |
352 return | 352 return |
353 self.frontends_cache.append(app) | 353 self.frontends_cache.append(app) |
354 self.updateParams(xml, security_limit, app) | 354 self.update_params(xml, security_limit, app) |
355 log.debug("Frontends parameters registered for %(app)s" % {"app": app}) | 355 log.debug("Frontends parameters registered for %(app)s" % {"app": app}) |
356 | 356 |
357 def __default_ok(self, value, name, category): | 357 def __default_ok(self, value, name, category): |
358 # FIXME: will not work with individual parameters | 358 # FIXME: will not work with individual parameters |
359 self.setParam(name, value, category) | 359 self.param_set(name, value, category) |
360 | 360 |
361 def __default_ko(self, failure, name, category): | 361 def __default_ko(self, failure, name, category): |
362 log.error( | 362 log.error( |
363 _("Can't determine default value for [%(category)s/%(name)s]: %(reason)s") | 363 _("Can't determine default value for [%(category)s/%(name)s]: %(reason)s") |
364 % {"category": category, "name": name, "reason": str(failure.value)} | 364 % {"category": category, "name": name, "reason": str(failure.value)} |
365 ) | 365 ) |
366 | 366 |
367 def setDefault(self, name, category, callback, errback=None): | 367 def set_default(self, name, category, callback, errback=None): |
368 """Set default value of parameter | 368 """Set default value of parameter |
369 | 369 |
370 'default_cb' attibute of parameter must be set to 'yes' | 370 'default_cb' attibute of parameter must be set to 'yes' |
371 @param name: name of the parameter | 371 @param name: name of the parameter |
372 @param category: category of the parameter | 372 @param category: category of the parameter |
374 @param errback: must manage the error with args failure, name, category | 374 @param errback: must manage the error with args failure, name, category |
375 """ | 375 """ |
376 # TODO: send signal param update if value changed | 376 # TODO: send signal param update if value changed |
377 # TODO: manage individual paramaters | 377 # TODO: manage individual paramaters |
378 log.debug( | 378 log.debug( |
379 "setDefault called for %(category)s/%(name)s" | 379 "set_default called for %(category)s/%(name)s" |
380 % {"category": category, "name": name} | 380 % {"category": category, "name": name} |
381 ) | 381 ) |
382 node = self._getParamNode(name, category, "@ALL@") | 382 node = self._get_param_node(name, category, "@ALL@") |
383 if not node: | 383 if not node: |
384 log.error( | 384 log.error( |
385 _( | 385 _( |
386 "Requested param [%(name)s] in category [%(category)s] doesn't exist !" | 386 "Requested param [%(name)s] in category [%(category)s] doesn't exist !" |
387 ) | 387 ) |
388 % {"name": name, "category": category} | 388 % {"name": name, "category": category} |
389 ) | 389 ) |
390 return | 390 return |
391 if node[1].getAttribute("default_cb") == "yes": | 391 if node[1].getAttribute("default_cb") == "yes": |
392 # del node[1].attributes['default_cb'] # default_cb is not used anymore as a flag to know if we have to set the default value, | 392 # del node[1].attributes['default_cb'] # default_cb is not used anymore as a flag to know if we have to set the default value, |
393 # and we can still use it later e.g. to call a generic setDefault method | 393 # and we can still use it later e.g. to call a generic set_default method |
394 value = self._getParam(category, name, C.GENERAL) | 394 value = self._get_param(category, name, C.GENERAL) |
395 if value is None: # no value set by the user: we have the default value | 395 if value is None: # no value set by the user: we have the default value |
396 log.debug("Default value to set, using callback") | 396 log.debug("Default value to set, using callback") |
397 d = defer.maybeDeferred(callback) | 397 d = defer.maybeDeferred(callback) |
398 d.addCallback(self.__default_ok, name, category) | 398 d.addCallback(self.__default_ok, name, category) |
399 d.addErrback(errback or self.__default_ko, name, category) | 399 d.addErrback(errback or self.__default_ko, name, category) |
400 | 400 |
401 def _getAttr_internal(self, node, attr, value): | 401 def _get_attr_internal(self, node, attr, value): |
402 """Get attribute value. | 402 """Get attribute value. |
403 | 403 |
404 /!\ This method would return encrypted password values. | 404 /!\ This method would return encrypted password values. |
405 | 405 |
406 @param node: XML param node | 406 @param node: XML param node |
462 if value_to_use: | 462 if value_to_use: |
463 jids = value_to_use.split( | 463 jids = value_to_use.split( |
464 "\t" | 464 "\t" |
465 ) # FIXME: it's not good to use tabs as separator ! | 465 ) # FIXME: it's not good to use tabs as separator ! |
466 else: # no user defined value, take default value from the XML | 466 else: # no user defined value, take default value from the XML |
467 jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")] | 467 jids = [get_text(jid_) for jid_ in node.getElementsByTagName("jid")] |
468 to_delete = [] | 468 to_delete = [] |
469 for idx, value in enumerate(jids): | 469 for idx, value in enumerate(jids): |
470 try: | 470 try: |
471 jids[idx] = jid.JID(value) | 471 jids[idx] = jid.JID(value) |
472 except (RuntimeError, jid.InvalidFormat, AttributeError): | 472 except (RuntimeError, jid.InvalidFormat, AttributeError): |
478 jids.remove(value) | 478 jids.remove(value) |
479 return jids | 479 return jids |
480 return value_to_use | 480 return value_to_use |
481 return node.getAttribute(attr) | 481 return node.getAttribute(attr) |
482 | 482 |
483 def _getAttr(self, node, attr, value): | 483 def _get_attr(self, node, attr, value): |
484 """Get attribute value (synchronous). | 484 """Get attribute value (synchronous). |
485 | 485 |
486 /!\ This method can not be used to retrieve password values. | 486 /!\ This method can not be used to retrieve password values. |
487 @param node: XML param node | 487 @param node: XML param node |
488 @param attr: name of the attribute to get (e.g.: 'value' or 'type') | 488 @param attr: name of the attribute to get (e.g.: 'value' or 'type') |
489 @param value: user defined value | 489 @param value: user defined value |
490 @return (unicode, bool, int, list): value to retrieve | 490 @return (unicode, bool, int, list): value to retrieve |
491 """ | 491 """ |
492 if attr == "value" and node.getAttribute("type") == "password": | 492 if attr == "value" and node.getAttribute("type") == "password": |
493 raise exceptions.InternalError( | 493 raise exceptions.InternalError( |
494 "To retrieve password values, use _asyncGetAttr instead of _getAttr" | 494 "To retrieve password values, use _async_get_attr instead of _get_attr" |
495 ) | 495 ) |
496 return self._getAttr_internal(node, attr, value) | 496 return self._get_attr_internal(node, attr, value) |
497 | 497 |
498 def _asyncGetAttr(self, node, attr, value, profile=None): | 498 def _async_get_attr(self, node, attr, value, profile=None): |
499 """Get attribute value. | 499 """Get attribute value. |
500 | 500 |
501 Profile passwords are returned hashed (if not empty), | 501 Profile passwords are returned hashed (if not empty), |
502 other passwords are returned decrypted (if not empty). | 502 other passwords are returned decrypted (if not empty). |
503 @param node: XML param node | 503 @param node: XML param node |
504 @param attr: name of the attribute to get (e.g.: 'value' or 'type') | 504 @param attr: name of the attribute to get (e.g.: 'value' or 'type') |
505 @param value: user defined value | 505 @param value: user defined value |
506 @param profile: %(doc_profile)s | 506 @param profile: %(doc_profile)s |
507 @return (unicode, bool, int, list): Deferred value to retrieve | 507 @return (unicode, bool, int, list): Deferred value to retrieve |
508 """ | 508 """ |
509 value = self._getAttr_internal(node, attr, value) | 509 value = self._get_attr_internal(node, attr, value) |
510 if attr != "value" or node.getAttribute("type") != "password": | 510 if attr != "value" or node.getAttribute("type") != "password": |
511 return defer.succeed(value) | 511 return defer.succeed(value) |
512 param_cat = node.parentNode.getAttribute("name") | 512 param_cat = node.parentNode.getAttribute("name") |
513 param_name = node.getAttribute("name") | 513 param_name = node.getAttribute("name") |
514 if ((param_cat, param_name) == C.PROFILE_PASS_PATH) or not value: | 514 if ((param_cat, param_name) == C.PROFILE_PASS_PATH) or not value: |
517 ) # profile password and empty passwords are returned "as is" | 517 ) # profile password and empty passwords are returned "as is" |
518 if not profile: | 518 if not profile: |
519 raise exceptions.ProfileNotSetError( | 519 raise exceptions.ProfileNotSetError( |
520 "The profile is needed to decrypt a password" | 520 "The profile is needed to decrypt a password" |
521 ) | 521 ) |
522 password = self.host.memory.decryptValue(value, profile) | 522 password = self.host.memory.decrypt_value(value, profile) |
523 | 523 |
524 if password is None: | 524 if password is None: |
525 raise exceptions.InternalError("password should never be None") | 525 raise exceptions.InternalError("password should never be None") |
526 return defer.succeed(password) | 526 return defer.succeed(password) |
527 | 527 |
528 def _type_to_str(self, result): | 528 def _type_to_str(self, result): |
529 """Convert result to string, according to its type """ | 529 """Convert result to string, according to its type """ |
530 if isinstance(result, bool): | 530 if isinstance(result, bool): |
531 return C.boolConst(result) | 531 return C.bool_const(result) |
532 elif isinstance(result, (list, set, tuple)): | 532 elif isinstance(result, (list, set, tuple)): |
533 return ', '.join(self._type_to_str(r) for r in result) | 533 return ', '.join(self._type_to_str(r) for r in result) |
534 else: | 534 else: |
535 return str(result) | 535 return str(result) |
536 | 536 |
537 def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): | 537 def get_string_param_a(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): |
538 """ Same as getParamA but for bridge: convert non string value to string """ | 538 """ Same as param_get_a but for bridge: convert non string value to string """ |
539 return self._type_to_str( | 539 return self._type_to_str( |
540 self.getParamA(name, category, attr, profile_key=profile_key) | 540 self.param_get_a(name, category, attr, profile_key=profile_key) |
541 ) | 541 ) |
542 | 542 |
543 def getParamA( | 543 def param_get_a( |
544 self, name, category, attr="value", use_default=True, profile_key=C.PROF_KEY_NONE | 544 self, name, category, attr="value", use_default=True, profile_key=C.PROF_KEY_NONE |
545 ): | 545 ): |
546 """Helper method to get a specific attribute. | 546 """Helper method to get a specific attribute. |
547 | 547 |
548 /!\ This method would return encrypted password values, | 548 /!\ This method would return encrypted password values, |
549 to get the plain values you have to use asyncGetParamA. | 549 to get the plain values you have to use param_get_a_async. |
550 @param name: name of the parameter | 550 @param name: name of the parameter |
551 @param category: category of the parameter | 551 @param category: category of the parameter |
552 @param attr: name of the attribute (default: "value") | 552 @param attr: name of the attribute (default: "value") |
553 @parm use_default(bool): if True and attr=='value', return default value if not set | 553 @parm use_default(bool): if True and attr=='value', return default value if not set |
554 else return None if not set | 554 else return None if not set |
555 @param profile: owner of the param (@ALL@ for everyone) | 555 @param profile: owner of the param (@ALL@ for everyone) |
556 @return: attribute | 556 @return: attribute |
557 """ | 557 """ |
558 # FIXME: looks really dirty and buggy, need to be reviewed/refactored | 558 # FIXME: looks really dirty and buggy, need to be reviewed/refactored |
559 # FIXME: security_limit is not managed here ! | 559 # FIXME: security_limit is not managed here ! |
560 node = self._getParamNode(name, category) | 560 node = self._get_param_node(name, category) |
561 if not node: | 561 if not node: |
562 log.error( | 562 log.error( |
563 _( | 563 _( |
564 "Requested param [%(name)s] in category [%(category)s] doesn't exist !" | 564 "Requested param [%(name)s] in category [%(category)s] doesn't exist !" |
565 ) | 565 ) |
567 ) | 567 ) |
568 raise exceptions.NotFound | 568 raise exceptions.NotFound |
569 | 569 |
570 if attr == "value" and node[1].getAttribute("type") == "password": | 570 if attr == "value" and node[1].getAttribute("type") == "password": |
571 raise exceptions.InternalError( | 571 raise exceptions.InternalError( |
572 "To retrieve password values, use asyncGetParamA instead of getParamA" | 572 "To retrieve password values, use param_get_a_async instead of param_get_a" |
573 ) | 573 ) |
574 | 574 |
575 if node[0] == C.GENERAL: | 575 if node[0] == C.GENERAL: |
576 value = self._getParam(category, name, C.GENERAL) | 576 value = self._get_param(category, name, C.GENERAL) |
577 if value is None and attr == "value" and not use_default: | 577 if value is None and attr == "value" and not use_default: |
578 return value | 578 return value |
579 return self._getAttr(node[1], attr, value) | 579 return self._get_attr(node[1], attr, value) |
580 | 580 |
581 assert node[0] == C.INDIVIDUAL | 581 assert node[0] == C.INDIVIDUAL |
582 | 582 |
583 profile = self.getProfileName(profile_key) | 583 profile = self.get_profile_name(profile_key) |
584 if not profile: | 584 if not profile: |
585 log.error(_("Requesting a param for an non-existant profile")) | 585 log.error(_("Requesting a param for an non-existant profile")) |
586 raise exceptions.ProfileUnknownError(profile_key) | 586 raise exceptions.ProfileUnknownError(profile_key) |
587 | 587 |
588 if profile not in self.params: | 588 if profile not in self.params: |
589 log.error(_("Requesting synchronous param for not connected profile")) | 589 log.error(_("Requesting synchronous param for not connected profile")) |
590 raise exceptions.ProfileNotConnected(profile) | 590 raise exceptions.ProfileNotConnected(profile) |
591 | 591 |
592 if attr == "value": | 592 if attr == "value": |
593 value = self._getParam(category, name, profile=profile) | 593 value = self._get_param(category, name, profile=profile) |
594 if value is None and attr == "value" and not use_default: | 594 if value is None and attr == "value" and not use_default: |
595 return value | 595 return value |
596 return self._getAttr(node[1], attr, value) | 596 return self._get_attr(node[1], attr, value) |
597 | 597 |
598 async def asyncGetStringParamA( | 598 async def async_get_string_param_a( |
599 self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, | 599 self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, |
600 profile=C.PROF_KEY_NONE): | 600 profile=C.PROF_KEY_NONE): |
601 value = await self.asyncGetParamA( | 601 value = await self.param_get_a_async( |
602 name, category, attr, security_limit, profile_key=profile) | 602 name, category, attr, security_limit, profile_key=profile) |
603 return self._type_to_str(value) | 603 return self._type_to_str(value) |
604 | 604 |
605 def asyncGetParamA( | 605 def param_get_a_async( |
606 self, | 606 self, |
607 name, | 607 name, |
608 category, | 608 category, |
609 attr="value", | 609 attr="value", |
610 security_limit=C.NO_SECURITY_LIMIT, | 610 security_limit=C.NO_SECURITY_LIMIT, |
616 @param category: category of the parameter | 616 @param category: category of the parameter |
617 @param attr: name of the attribute (default: "value") | 617 @param attr: name of the attribute (default: "value") |
618 @param profile: owner of the param (@ALL@ for everyone) | 618 @param profile: owner of the param (@ALL@ for everyone) |
619 @return (defer.Deferred): parameter value, with corresponding type (bool, int, list, etc) | 619 @return (defer.Deferred): parameter value, with corresponding type (bool, int, list, etc) |
620 """ | 620 """ |
621 node = self._getParamNode(name, category) | 621 node = self._get_param_node(name, category) |
622 if not node: | 622 if not node: |
623 log.error( | 623 log.error( |
624 _( | 624 _( |
625 "Requested param [%(name)s] in category [%(category)s] doesn't exist !" | 625 "Requested param [%(name)s] in category [%(category)s] doesn't exist !" |
626 ) | 626 ) |
627 % {"name": name, "category": category} | 627 % {"name": name, "category": category} |
628 ) | 628 ) |
629 raise ValueError("Requested param doesn't exist") | 629 raise ValueError("Requested param doesn't exist") |
630 | 630 |
631 if not self.checkSecurityLimit(node[1], security_limit): | 631 if not self.check_security_limit(node[1], security_limit): |
632 log.warning( | 632 log.warning( |
633 _( | 633 _( |
634 "Trying to get parameter '%(param)s' in category '%(cat)s' without authorization!!!" | 634 "Trying to get parameter '%(param)s' in category '%(cat)s' without authorization!!!" |
635 % {"param": name, "cat": category} | 635 % {"param": name, "cat": category} |
636 ) | 636 ) |
637 ) | 637 ) |
638 raise exceptions.PermissionError | 638 raise exceptions.PermissionError |
639 | 639 |
640 if node[0] == C.GENERAL: | 640 if node[0] == C.GENERAL: |
641 value = self._getParam(category, name, C.GENERAL) | 641 value = self._get_param(category, name, C.GENERAL) |
642 return self._asyncGetAttr(node[1], attr, value) | 642 return self._async_get_attr(node[1], attr, value) |
643 | 643 |
644 assert node[0] == C.INDIVIDUAL | 644 assert node[0] == C.INDIVIDUAL |
645 | 645 |
646 profile = self.getProfileName(profile_key) | 646 profile = self.get_profile_name(profile_key) |
647 if not profile: | 647 if not profile: |
648 raise exceptions.InternalError( | 648 raise exceptions.InternalError( |
649 _("Requesting a param for a non-existant profile") | 649 _("Requesting a param for a non-existant profile") |
650 ) | 650 ) |
651 | 651 |
652 if attr != "value": | 652 if attr != "value": |
653 return defer.succeed(node[1].getAttribute(attr)) | 653 return defer.succeed(node[1].getAttribute(attr)) |
654 try: | 654 try: |
655 value = self._getParam(category, name, profile=profile) | 655 value = self._get_param(category, name, profile=profile) |
656 return self._asyncGetAttr(node[1], attr, value, profile) | 656 return self._async_get_attr(node[1], attr, value, profile) |
657 except exceptions.ProfileNotInCacheError: | 657 except exceptions.ProfileNotInCacheError: |
658 # We have to ask data to the storage manager | 658 # We have to ask data to the storage manager |
659 d = self.storage.getIndParam(category, name, profile) | 659 d = self.storage.get_ind_param(category, name, profile) |
660 return d.addCallback( | 660 return d.addCallback( |
661 lambda value: self._asyncGetAttr(node[1], attr, value, profile) | 661 lambda value: self._async_get_attr(node[1], attr, value, profile) |
662 ) | 662 ) |
663 | 663 |
664 def _getParamsValuesFromCategory( | 664 def _get_params_values_from_category( |
665 self, category, security_limit, app, extra_s, profile_key): | 665 self, category, security_limit, app, extra_s, profile_key): |
666 client = self.host.getClient(profile_key) | 666 client = self.host.get_client(profile_key) |
667 extra = data_format.deserialise(extra_s) | 667 extra = data_format.deserialise(extra_s) |
668 return defer.ensureDeferred(self.getParamsValuesFromCategory( | 668 return defer.ensureDeferred(self.get_params_values_from_category( |
669 client, category, security_limit, app, extra)) | 669 client, category, security_limit, app, extra)) |
670 | 670 |
671 async def getParamsValuesFromCategory( | 671 async def get_params_values_from_category( |
672 self, client, category, security_limit, app='', extra=None): | 672 self, client, category, security_limit, app='', extra=None): |
673 """Get all parameters "attribute" for a category | 673 """Get all parameters "attribute" for a category |
674 | 674 |
675 @param category(unicode): the desired category | 675 @param category(unicode): the desired category |
676 @param security_limit(int): NO_SECURITY_LIMIT (-1) to return all the params. | 676 @param security_limit(int): NO_SECURITY_LIMIT (-1) to return all the params. |
677 Otherwise sole the params which have a security level defined *and* | 677 Otherwise sole the params which have a security level defined *and* |
678 lower or equal to the specified value are returned. | 678 lower or equal to the specified value are returned. |
679 @param app(str): see [getParams] | 679 @param app(str): see [get_params] |
680 @param extra(dict): see [getParams] | 680 @param extra(dict): see [get_params] |
681 @return (dict): key: param name, value: param value (converted to string if needed) | 681 @return (dict): key: param name, value: param value (converted to string if needed) |
682 """ | 682 """ |
683 # TODO: manage category of general type (without existant profile) | 683 # TODO: manage category of general type (without existant profile) |
684 if extra is None: | 684 if extra is None: |
685 extra = {} | 685 extra = {} |
686 prof_xml = await self._constructProfileXml(client, security_limit, app, extra) | 686 prof_xml = await self._construct_profile_xml(client, security_limit, app, extra) |
687 ret = {} | 687 ret = {} |
688 for category_node in prof_xml.getElementsByTagName("category"): | 688 for category_node in prof_xml.getElementsByTagName("category"): |
689 if category_node.getAttribute("name") == category: | 689 if category_node.getAttribute("name") == category: |
690 for param_node in category_node.getElementsByTagName("param"): | 690 for param_node in category_node.getElementsByTagName("param"): |
691 name = param_node.getAttribute("name") | 691 name = param_node.getAttribute("name") |
694 "ignoring attribute without name: {}".format( | 694 "ignoring attribute without name: {}".format( |
695 param_node.toxml() | 695 param_node.toxml() |
696 ) | 696 ) |
697 ) | 697 ) |
698 continue | 698 continue |
699 value = await self.asyncGetStringParamA( | 699 value = await self.async_get_string_param_a( |
700 name, category, security_limit=security_limit, | 700 name, category, security_limit=security_limit, |
701 profile=client.profile) | 701 profile=client.profile) |
702 | 702 |
703 ret[name] = value | 703 ret[name] = value |
704 break | 704 break |
705 | 705 |
706 prof_xml.unlink() | 706 prof_xml.unlink() |
707 return ret | 707 return ret |
708 | 708 |
709 def _getParam( | 709 def _get_param( |
710 self, category, name, type_=C.INDIVIDUAL, cache=None, profile=C.PROF_KEY_NONE | 710 self, category, name, type_=C.INDIVIDUAL, cache=None, profile=C.PROF_KEY_NONE |
711 ): | 711 ): |
712 """Return the param, or None if it doesn't exist | 712 """Return the param, or None if it doesn't exist |
713 | 713 |
714 @param category: param category | 714 @param category: param category |
734 raise exceptions.ProfileNotInCacheError | 734 raise exceptions.ProfileNotInCacheError |
735 if (category, name) not in cache: | 735 if (category, name) not in cache: |
736 return None | 736 return None |
737 return cache[(category, name)] | 737 return cache[(category, name)] |
738 | 738 |
739 async def _constructProfileXml(self, client, security_limit, app, extra): | 739 async def _construct_profile_xml(self, client, security_limit, app, extra): |
740 """Construct xml for asked profile, filling values when needed | 740 """Construct xml for asked profile, filling values when needed |
741 | 741 |
742 /!\ as noticed in doc, don't forget to unlink the minidom.Document | 742 /!\ as noticed in doc, don't forget to unlink the minidom.Document |
743 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params. | 743 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params. |
744 Otherwise sole the params which have a security level defined *and* | 744 Otherwise sole the params which have a security level defined *and* |
747 @param profile: profile name (not key !) | 747 @param profile: profile name (not key !) |
748 @return: a deferred that fire a minidom.Document of the profile xml (cf warning above) | 748 @return: a deferred that fire a minidom.Document of the profile xml (cf warning above) |
749 """ | 749 """ |
750 profile = client.profile | 750 profile = client.profile |
751 | 751 |
752 def checkNode(node): | 752 def check_node(node): |
753 """Check the node against security_limit, app and extra""" | 753 """Check the node against security_limit, app and extra""" |
754 return (self.checkSecurityLimit(node, security_limit) | 754 return (self.check_security_limit(node, security_limit) |
755 and self.checkApp(node, app) | 755 and self.check_app(node, app) |
756 and self.checkExtra(node, extra)) | 756 and self.check_extra(node, extra)) |
757 | 757 |
758 if profile in self.params: | 758 if profile in self.params: |
759 profile_cache = self.params[profile] | 759 profile_cache = self.params[profile] |
760 else: | 760 else: |
761 # profile is not in cache, we load values in a short time cache | 761 # profile is not in cache, we load values in a short time cache |
762 profile_cache = {} | 762 profile_cache = {} |
763 await self.loadIndParams(profile, profile_cache) | 763 await self.load_ind_params(profile, profile_cache) |
764 | 764 |
765 # init the result document | 765 # init the result document |
766 prof_xml = minidom.parseString("<params/>") | 766 prof_xml = minidom.parseString("<params/>") |
767 cache = {} | 767 cache = {} |
768 | 768 |
780 cache[category] = dest_cat = cat_node.cloneNode(True) | 780 cache[category] = dest_cat = cat_node.cloneNode(True) |
781 to_remove = [] | 781 to_remove = [] |
782 for node in dest_cat.childNodes: | 782 for node in dest_cat.childNodes: |
783 if node.nodeName != "param": | 783 if node.nodeName != "param": |
784 continue | 784 continue |
785 if not checkNode(node): | 785 if not check_node(node): |
786 to_remove.append(node) | 786 to_remove.append(node) |
787 continue | 787 continue |
788 dest_params[node.getAttribute("name")] = node | 788 dest_params[node.getAttribute("name")] = node |
789 for node in to_remove: | 789 for node in to_remove: |
790 dest_cat.removeChild(node) | 790 dest_cat.removeChild(node) |
797 | 797 |
798 for param_node in params: | 798 for param_node in params: |
799 # we have to merge new params (we are parsing individual parameters, we have to add them | 799 # we have to merge new params (we are parsing individual parameters, we have to add them |
800 # to the previously parsed general ones) | 800 # to the previously parsed general ones) |
801 name = param_node.getAttribute("name") | 801 name = param_node.getAttribute("name") |
802 if not checkNode(param_node): | 802 if not check_node(param_node): |
803 continue | 803 continue |
804 if name not in dest_params: | 804 if name not in dest_params: |
805 # this is reached when a previous category exists | 805 # this is reached when a previous category exists |
806 dest_params[name] = param_node.cloneNode(True) | 806 dest_params[name] = param_node.cloneNode(True) |
807 dest_cat.appendChild(dest_params[name]) | 807 dest_cat.appendChild(dest_params[name]) |
808 | 808 |
809 profile_value = self._getParam( | 809 profile_value = self._get_param( |
810 category, | 810 category, |
811 name, | 811 name, |
812 type_node.nodeName, | 812 type_node.nodeName, |
813 cache=profile_cache, | 813 cache=profile_cache, |
814 profile=profile, | 814 profile=profile, |
865 prof_xml.documentElement.removeChild(node) | 865 prof_xml.documentElement.removeChild(node) |
866 | 866 |
867 return prof_xml | 867 return prof_xml |
868 | 868 |
869 | 869 |
870 def _getParamsUI(self, security_limit, app, extra_s, profile_key): | 870 def _get_params_ui(self, security_limit, app, extra_s, profile_key): |
871 client = self.host.getClient(profile_key) | 871 client = self.host.get_client(profile_key) |
872 extra = data_format.deserialise(extra_s) | 872 extra = data_format.deserialise(extra_s) |
873 return defer.ensureDeferred(self.getParamsUI(client, security_limit, app, extra)) | 873 return defer.ensureDeferred(self.param_ui_get(client, security_limit, app, extra)) |
874 | 874 |
875 async def getParamsUI(self, client, security_limit, app, extra=None): | 875 async def param_ui_get(self, client, security_limit, app, extra=None): |
876 """Get XMLUI to handle parameters | 876 """Get XMLUI to handle parameters |
877 | 877 |
878 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params. | 878 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params. |
879 Otherwise sole the params which have a security level defined *and* | 879 Otherwise sole the params which have a security level defined *and* |
880 lower or equal to the specified value are returned. | 880 lower or equal to the specified value are returned. |
881 @param app: name of the frontend requesting the parameters, or '' to get all parameters | 881 @param app: name of the frontend requesting the parameters, or '' to get all parameters |
882 @param extra (dict, None): extra options. Key can be: | 882 @param extra (dict, None): extra options. Key can be: |
883 - ignore: list of (category/name) values to remove from parameters | 883 - ignore: list of (category/name) values to remove from parameters |
884 @return(str): a SàT XMLUI for parameters | 884 @return(str): a SàT XMLUI for parameters |
885 """ | 885 """ |
886 param_xml = await self.getParams(client, security_limit, app, extra) | 886 param_xml = await self.get_params(client, security_limit, app, extra) |
887 return paramsXML2XMLUI(param_xml) | 887 return params_xml_2_xmlui(param_xml) |
888 | 888 |
889 async def getParams(self, client, security_limit, app, extra=None): | 889 async def get_params(self, client, security_limit, app, extra=None): |
890 """Construct xml for asked profile, take params xml as skeleton | 890 """Construct xml for asked profile, take params xml as skeleton |
891 | 891 |
892 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params. | 892 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params. |
893 Otherwise sole the params which have a security level defined *and* | 893 Otherwise sole the params which have a security level defined *and* |
894 lower or equal to the specified value are returned. | 894 lower or equal to the specified value are returned. |
898 @param profile_key: Profile key which can be either a magic (eg: @DEFAULT@) or the name of an existing profile. | 898 @param profile_key: Profile key which can be either a magic (eg: @DEFAULT@) or the name of an existing profile. |
899 @return: XML of parameters | 899 @return: XML of parameters |
900 """ | 900 """ |
901 if extra is None: | 901 if extra is None: |
902 extra = {} | 902 extra = {} |
903 prof_xml = await self._constructProfileXml(client, security_limit, app, extra) | 903 prof_xml = await self._construct_profile_xml(client, security_limit, app, extra) |
904 return_xml = prof_xml.toxml() | 904 return_xml = prof_xml.toxml() |
905 prof_xml.unlink() | 905 prof_xml.unlink() |
906 return "\n".join((line for line in return_xml.split("\n") if line)) | 906 return "\n".join((line for line in return_xml.split("\n") if line)) |
907 | 907 |
908 def _getParamNode(self, name, category, type_="@ALL@"): # FIXME: is type_ useful ? | 908 def _get_param_node(self, name, category, type_="@ALL@"): # FIXME: is type_ useful ? |
909 """Return a node from the param_xml | 909 """Return a node from the param_xml |
910 @param name: name of the node | 910 @param name: name of the node |
911 @param category: category of the node | 911 @param category: category of the node |
912 @param type_: keyword for search: | 912 @param type_: keyword for search: |
913 @ALL@ search everywhere | 913 @ALL@ search everywhere |
929 for param in params: | 929 for param in params: |
930 if param.getAttribute("name") == name: | 930 if param.getAttribute("name") == name: |
931 return (type_node.nodeName, param) | 931 return (type_node.nodeName, param) |
932 return None | 932 return None |
933 | 933 |
934 def getParamsCategories(self): | 934 def params_categories_get(self): |
935 """return the categories availables""" | 935 """return the categories availables""" |
936 categories = [] | 936 categories = [] |
937 for cat in self.dom.getElementsByTagName("category"): | 937 for cat in self.dom.getElementsByTagName("category"): |
938 name = cat.getAttribute("name") | 938 name = cat.getAttribute("name") |
939 if name not in categories: | 939 if name not in categories: |
940 categories.append(cat.getAttribute("name")) | 940 categories.append(cat.getAttribute("name")) |
941 return categories | 941 return categories |
942 | 942 |
943 def setParam(self, name, value, category, security_limit=C.NO_SECURITY_LIMIT, | 943 def param_set(self, name, value, category, security_limit=C.NO_SECURITY_LIMIT, |
944 profile_key=C.PROF_KEY_NONE): | 944 profile_key=C.PROF_KEY_NONE): |
945 """Set a parameter, return None if the parameter is not in param xml. | 945 """Set a parameter, return None if the parameter is not in param xml. |
946 | 946 |
947 Parameter of type 'password' that are not the SàT profile password are | 947 Parameter of type 'password' that are not the SàT profile password are |
948 stored encrypted (if not empty). The profile password is stored hashed | 948 stored encrypted (if not empty). The profile password is stored hashed |
953 @param category (str): the parameter category | 953 @param category (str): the parameter category |
954 @param security_limit (int) | 954 @param security_limit (int) |
955 @param profile_key (str): %(doc_profile_key)s | 955 @param profile_key (str): %(doc_profile_key)s |
956 @return: a deferred None value when everything is done | 956 @return: a deferred None value when everything is done |
957 """ | 957 """ |
958 # FIXME: setParam should accept the right type for value, not only str ! | 958 # FIXME: param_set should accept the right type for value, not only str ! |
959 if profile_key != C.PROF_KEY_NONE: | 959 if profile_key != C.PROF_KEY_NONE: |
960 profile = self.getProfileName(profile_key) | 960 profile = self.get_profile_name(profile_key) |
961 if not profile: | 961 if not profile: |
962 log.error(_("Trying to set parameter for an unknown profile")) | 962 log.error(_("Trying to set parameter for an unknown profile")) |
963 raise exceptions.ProfileUnknownError(profile_key) | 963 raise exceptions.ProfileUnknownError(profile_key) |
964 | 964 |
965 node = self._getParamNode(name, category, "@ALL@") | 965 node = self._get_param_node(name, category, "@ALL@") |
966 if not node: | 966 if not node: |
967 log.error( | 967 log.error( |
968 _("Requesting an unknown parameter (%(category)s/%(name)s)") | 968 _("Requesting an unknown parameter (%(category)s/%(name)s)") |
969 % {"category": category, "name": name} | 969 % {"category": category, "name": name} |
970 ) | 970 ) |
971 return defer.succeed(None) | 971 return defer.succeed(None) |
972 | 972 |
973 if not self.checkSecurityLimit(node[1], security_limit): | 973 if not self.check_security_limit(node[1], security_limit): |
974 msg = _( | 974 msg = _( |
975 "{profile!r} is trying to set parameter {name!r} in category " | 975 "{profile!r} is trying to set parameter {name!r} in category " |
976 "{category!r} without authorization!!!").format( | 976 "{category!r} without authorization!!!").format( |
977 profile=repr(profile), | 977 profile=repr(profile), |
978 name=repr(name), | 978 name=repr(name), |
1016 } | 1016 } |
1017 ) | 1017 ) |
1018 | 1018 |
1019 if node[0] == C.GENERAL: | 1019 if node[0] == C.GENERAL: |
1020 self.params_gen[(category, name)] = value | 1020 self.params_gen[(category, name)] = value |
1021 self.storage.setGenParam(category, name, value) | 1021 self.storage.set_gen_param(category, name, value) |
1022 for profile in self.storage.getProfilesList(): | 1022 for profile in self.storage.get_profiles_list(): |
1023 if self.host.memory.isSessionStarted(profile): | 1023 if self.host.memory.is_session_started(profile): |
1024 self.host.bridge.paramUpdate(name, value, category, profile) | 1024 self.host.bridge.param_update(name, value, category, profile) |
1025 self.host.trigger.point( | 1025 self.host.trigger.point( |
1026 "paramUpdateTrigger", name, value, category, node[0], profile | 1026 "param_update_trigger", name, value, category, node[0], profile |
1027 ) | 1027 ) |
1028 return defer.succeed(None) | 1028 return defer.succeed(None) |
1029 | 1029 |
1030 assert node[0] == C.INDIVIDUAL | 1030 assert node[0] == C.INDIVIDUAL |
1031 assert profile_key != C.PROF_KEY_NONE | 1031 assert profile_key != C.PROF_KEY_NONE |
1033 if type_ == "button": | 1033 if type_ == "button": |
1034 log.debug("Clicked param button %s" % node.toxml()) | 1034 log.debug("Clicked param button %s" % node.toxml()) |
1035 return defer.succeed(None) | 1035 return defer.succeed(None) |
1036 elif type_ == "password": | 1036 elif type_ == "password": |
1037 try: | 1037 try: |
1038 personal_key = self.host.memory.auth_sessions.profileGetUnique(profile)[ | 1038 personal_key = self.host.memory.auth_sessions.profile_get_unique(profile)[ |
1039 C.MEMORY_CRYPTO_KEY | 1039 C.MEMORY_CRYPTO_KEY |
1040 ] | 1040 ] |
1041 except TypeError: | 1041 except TypeError: |
1042 raise exceptions.InternalError( | 1042 raise exceptions.InternalError( |
1043 _("Trying to encrypt a password while the personal key is undefined!") | 1043 _("Trying to encrypt a password while the personal key is undefined!") |
1044 ) | 1044 ) |
1045 if (category, name) == C.PROFILE_PASS_PATH: | 1045 if (category, name) == C.PROFILE_PASS_PATH: |
1046 # using 'value' as the encryption key to encrypt another encryption key... could be confusing! | 1046 # using 'value' as the encryption key to encrypt another encryption key... could be confusing! |
1047 d = self.host.memory.encryptPersonalData( | 1047 d = self.host.memory.encrypt_personal_data( |
1048 data_key=C.MEMORY_CRYPTO_KEY, | 1048 data_key=C.MEMORY_CRYPTO_KEY, |
1049 data_value=personal_key, | 1049 data_value=personal_key, |
1050 crypto_key=value, | 1050 crypto_key=value, |
1051 profile=profile, | 1051 profile=profile, |
1052 ) | 1052 ) |
1058 else: | 1058 else: |
1059 d = defer.succeed(value) | 1059 d = defer.succeed(value) |
1060 else: | 1060 else: |
1061 d = defer.succeed(value) | 1061 d = defer.succeed(value) |
1062 | 1062 |
1063 def gotFinalValue(value): | 1063 def got_final_value(value): |
1064 if self.host.memory.isSessionStarted(profile): | 1064 if self.host.memory.is_session_started(profile): |
1065 self.params[profile][(category, name)] = value | 1065 self.params[profile][(category, name)] = value |
1066 self.host.bridge.paramUpdate(name, value, category, profile) | 1066 self.host.bridge.param_update(name, value, category, profile) |
1067 self.host.trigger.point( | 1067 self.host.trigger.point( |
1068 "paramUpdateTrigger", name, value, category, node[0], profile | 1068 "param_update_trigger", name, value, category, node[0], profile |
1069 ) | 1069 ) |
1070 return self.storage.setIndParam(category, name, value, profile) | 1070 return self.storage.set_ind_param(category, name, value, profile) |
1071 else: | 1071 else: |
1072 raise exceptions.ProfileNotConnected | 1072 raise exceptions.ProfileNotConnected |
1073 | 1073 |
1074 d.addCallback(gotFinalValue) | 1074 d.addCallback(got_final_value) |
1075 return d | 1075 return d |
1076 | 1076 |
1077 def _getNodesOfTypes(self, attr_type, node_type="@ALL@"): | 1077 def _get_nodes_of_types(self, attr_type, node_type="@ALL@"): |
1078 """Return all the nodes matching the given types. | 1078 """Return all the nodes matching the given types. |
1079 | 1079 |
1080 TODO: using during the dev but not anymore... remove if not needed | 1080 TODO: using during the dev but not anymore... remove if not needed |
1081 | 1081 |
1082 @param attr_type (str): the attribute type (string, text, password, bool, int, button, list) | 1082 @param attr_type (str): the attribute type (string, text, password, bool, int, button, list) |
1103 for param in params: | 1103 for param in params: |
1104 if param.getAttribute("type") == attr_type: | 1104 if param.getAttribute("type") == attr_type: |
1105 ret[(cat, param.getAttribute("name"))] = param | 1105 ret[(cat, param.getAttribute("name"))] = param |
1106 return ret | 1106 return ret |
1107 | 1107 |
1108 def checkSecurityLimit(self, node, security_limit): | 1108 def check_security_limit(self, node, security_limit): |
1109 """Check the given node against the given security limit. | 1109 """Check the given node against the given security limit. |
1110 The value NO_SECURITY_LIMIT (-1) means that everything is allowed. | 1110 The value NO_SECURITY_LIMIT (-1) means that everything is allowed. |
1111 @return: True if this node can be accessed with the given security limit. | 1111 @return: True if this node can be accessed with the given security limit. |
1112 """ | 1112 """ |
1113 if security_limit < 0: | 1113 if security_limit < 0: |
1115 if node.hasAttribute("security"): | 1115 if node.hasAttribute("security"): |
1116 if int(node.getAttribute("security")) <= security_limit: | 1116 if int(node.getAttribute("security")) <= security_limit: |
1117 return True | 1117 return True |
1118 return False | 1118 return False |
1119 | 1119 |
1120 def checkApp(self, node, app): | 1120 def check_app(self, node, app): |
1121 """Check the given node against the given app. | 1121 """Check the given node against the given app. |
1122 | 1122 |
1123 @param node: parameter node | 1123 @param node: parameter node |
1124 @param app: name of the frontend requesting the parameters, or '' to get all parameters | 1124 @param app: name of the frontend requesting the parameters, or '' to get all parameters |
1125 @return: True if this node concerns the given app. | 1125 @return: True if this node concerns the given app. |
1126 """ | 1126 """ |
1127 if not app or not node.hasAttribute("app"): | 1127 if not app or not node.hasAttribute("app"): |
1128 return True | 1128 return True |
1129 return node.getAttribute("app") == app | 1129 return node.getAttribute("app") == app |
1130 | 1130 |
1131 def checkExtra(self, node, extra): | 1131 def check_extra(self, node, extra): |
1132 """Check the given node against the extra filters. | 1132 """Check the given node against the extra filters. |
1133 | 1133 |
1134 @param node: parameter node | 1134 @param node: parameter node |
1135 @param app: name of the frontend requesting the parameters, or '' to get all parameters | 1135 @param app: name of the frontend requesting the parameters, or '' to get all parameters |
1136 @return: True if node doesn't match category/name of extra['ignore'] list | 1136 @return: True if node doesn't match category/name of extra['ignore'] list |
1145 log.debug(f"Ignoring parameter {category}/{name} as requested") | 1145 log.debug(f"Ignoring parameter {category}/{name} as requested") |
1146 return False | 1146 return False |
1147 return True | 1147 return True |
1148 | 1148 |
1149 | 1149 |
1150 def makeOptions(options, selected=None): | 1150 def make_options(options, selected=None): |
1151 """Create option XML form dictionary | 1151 """Create option XML form dictionary |
1152 | 1152 |
1153 @param options(dict): option's name => option's label map | 1153 @param options(dict): option's name => option's label map |
1154 @param selected(None, str): value of selected option | 1154 @param selected(None, str): value of selected option |
1155 None to use first value | 1155 None to use first value |