Mercurial > libervia-backend
comparison tools/memory.py @ 69:86f1f7f6d332
i18n first draft
- gettext support added in SàT
- first draft of french translation
- added README with a HOWTO for translators
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 03 Mar 2010 17:12:23 +1100 |
parents | 9b842086d915 |
children | db0a0f000e37 |
comparison
equal
deleted
inserted
replaced
68:9b842086d915 | 69:86f1f7f6d332 |
---|---|
72 | 72 |
73 if os.path.exists(file_gen): | 73 if os.path.exists(file_gen): |
74 try: | 74 try: |
75 with open(file_gen, 'r') as file_gen_pickle: | 75 with open(file_gen, 'r') as file_gen_pickle: |
76 self.params_gen=pickle.load(file_gen_pickle) | 76 self.params_gen=pickle.load(file_gen_pickle) |
77 debug("general params data loaded") | 77 debug(_("general params data loaded")) |
78 except: | 78 except: |
79 error ("Can't load general params data !") | 79 error (_("Can't load general params data !")) |
80 | 80 |
81 if os.path.exists(file_ind): | 81 if os.path.exists(file_ind): |
82 try: | 82 try: |
83 with open(file_ind, 'r') as file_ind_pickle: | 83 with open(file_ind, 'r') as file_ind_pickle: |
84 self.params=pickle.load(file_ind_pickle) | 84 self.params=pickle.load(file_ind_pickle) |
85 debug("individual params data loaded") | 85 debug(_("individual params data loaded")) |
86 except: | 86 except: |
87 error ("Can't load individual params data !") | 87 error (_("Can't load individual params data !")) |
88 | 88 |
89 def save_xml(self, file): | 89 def save_xml(self, file): |
90 """Save parameters template to xml file""" | 90 """Save parameters template to xml file""" |
91 with open(file, 'wb') as xml_file: | 91 with open(file, 'wb') as xml_file: |
92 self.dom.writexml(xml_file) | 92 self.dom.writexml(xml_file) |
119 | 119 |
120 def createProfile(self, name): | 120 def createProfile(self, name): |
121 """Create a new profile | 121 """Create a new profile |
122 @param name: Name of the profile""" | 122 @param name: Name of the profile""" |
123 if self.params.has_key(name): | 123 if self.params.has_key(name): |
124 info ('The profile name already exists') | 124 info (_('The profile name already exists')) |
125 return 1 | 125 return 1 |
126 self.params[name]={} | 126 self.params[name]={} |
127 return 0 | 127 return 0 |
128 | 128 |
129 def deleteProfile(self, name): | 129 def deleteProfile(self, name): |
130 """Delete an existing profile | 130 """Delete an existing profile |
131 @param name: Name of the profile""" | 131 @param name: Name of the profile""" |
132 if not self.params.has_key(name): | 132 if not self.params.has_key(name): |
133 error ('Trying to delete an unknown profile') | 133 error (_('Trying to delete an unknown profile')) |
134 return 1 | 134 return 1 |
135 del self.params[name] | 135 del self.params[name] |
136 return 0 | 136 return 0 |
137 | 137 |
138 def getProfileName(self, profile_key): | 138 def getProfileName(self, profile_key): |
144 if profile_key=='@DEFAULT@': | 144 if profile_key=='@DEFAULT@': |
145 if not self.params: | 145 if not self.params: |
146 return "" | 146 return "" |
147 default = self.host.memory.getPrivate('Profile_default') | 147 default = self.host.memory.getPrivate('Profile_default') |
148 if not default or not default in self.params: | 148 if not default or not default in self.params: |
149 info('No default profile, returning first one') #TODO: manage real default profile | 149 info(_('No default profile, returning first one')) #TODO: manage real default profile |
150 default = self.params.keys()[0] | 150 default = self.params.keys()[0] |
151 self.host.memory.setPrivate('Profile_default', default) | 151 self.host.memory.setPrivate('Profile_default', default) |
152 return default #FIXME: gof: temporary, must use real default value, and fallback to first one if it doesn't exists | 152 return default #FIXME: gof: temporary, must use real default value, and fallback to first one if it doesn't exists |
153 if not self.params.has_key(profile_key): | 153 if not self.params.has_key(profile_key): |
154 info ('Trying to access an unknown profile') | 154 info (_('Trying to access an unknown profile')) |
155 return "" | 155 return "" |
156 return profile_key | 156 return profile_key |
157 | 157 |
158 def __get_unique_node(self, parent, tag, name): | 158 def __get_unique_node(self, parent, tag, name): |
159 """return node with given tag | 159 """return node with given tag |
189 def __default_ok(self, value, name, category): | 189 def __default_ok(self, value, name, category): |
190 #FIXME: gof: will not work with individual parameters | 190 #FIXME: gof: will not work with individual parameters |
191 self.setParam(name, value, category) #FIXME: better to set param xml value ??? | 191 self.setParam(name, value, category) #FIXME: better to set param xml value ??? |
192 | 192 |
193 def __default_ko(self, failure, name, category): | 193 def __default_ko(self, failure, name, category): |
194 error ("Can't determine default value for [%s/%s]: %s" % (category, name, str(failure.value))) | 194 error (_("Can't determine default value for [%(category)s/%(name)s]: %(reason)s") % {'category':category, 'name':name, 'reason':str(failure.value)}) |
195 | 195 |
196 def setDefault(self, name, category, callback, errback=None): | 196 def setDefault(self, name, category, callback, errback=None): |
197 """Set default value of parameter | 197 """Set default value of parameter |
198 'default_cb' attibute of parameter must be set to 'yes' | 198 'default_cb' attibute of parameter must be set to 'yes' |
199 @param name: name of the parameter | 199 @param name: name of the parameter |
202 @param errback: must manage the error with args failure, name, category | 202 @param errback: must manage the error with args failure, name, category |
203 """ | 203 """ |
204 #TODO: send signal param update if value changed | 204 #TODO: send signal param update if value changed |
205 node = self.__getParamNode(name, category, '@ALL@') | 205 node = self.__getParamNode(name, category, '@ALL@') |
206 if not node: | 206 if not node: |
207 error("Requested param [%s] in category [%s] doesn't exist !" , name, category) | 207 error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category}) |
208 return | 208 return |
209 if node[1].getAttribute('default_cb') == 'yes': | 209 if node[1].getAttribute('default_cb') == 'yes': |
210 del node[1].attributes['default_cb'] | 210 del node[1].attributes['default_cb'] |
211 d = defer.maybeDeferred(callback) | 211 d = defer.maybeDeferred(callback) |
212 d.addCallback(self.__default_ok, name, category) | 212 d.addCallback(self.__default_ok, name, category) |
220 @param profile: owner of the param (@ALL@ for everyone) | 220 @param profile: owner of the param (@ALL@ for everyone) |
221 | 221 |
222 @return: attribute""" | 222 @return: attribute""" |
223 node = self.__getParamNode(name, category) | 223 node = self.__getParamNode(name, category) |
224 if not node: | 224 if not node: |
225 error("Requested param [%s] in category [%s] doesn't exist !" , name, category) | 225 error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category}) |
226 return None | 226 return None |
227 | 227 |
228 if node[0] == 'general': | 228 if node[0] == 'general': |
229 value = self.__getParam(None, category, name, 'general') | 229 value = self.__getParam(None, category, name, 'general') |
230 return value or node[1].getAttribute(attr) | 230 return value or node[1].getAttribute(attr) |
231 | 231 |
232 assert(node[0] == 'individual') | 232 assert(node[0] == 'individual') |
233 | 233 |
234 profile = self.getProfileName(profile_key) | 234 profile = self.getProfileName(profile_key) |
235 if not profile: | 235 if not profile: |
236 error('Requesting a param for an non-existant profile') | 236 error(_('Requesting a param for an non-existant profile')) |
237 return None | 237 return None |
238 | 238 |
239 if attr == "value": | 239 if attr == "value": |
240 return self.__getParam(profile, category, name) or node[1].getAttribute(attr) | 240 return self.__getParam(profile, category, name) or node[1].getAttribute(attr) |
241 else: | 241 else: |
285 def getParams(self, profile_key='@DEFAULT@'): | 285 def getParams(self, profile_key='@DEFAULT@'): |
286 """Construct xml for asked profile | 286 """Construct xml for asked profile |
287 Take params xml as skeleton""" | 287 Take params xml as skeleton""" |
288 profile = self.getProfileName(profile_key) | 288 profile = self.getProfileName(profile_key) |
289 if not profile: | 289 if not profile: |
290 error("Asking params for inexistant profile") | 290 error(_("Asking params for inexistant profile")) |
291 return "" | 291 return "" |
292 prof_xml = self.__constructProfileXml(profile) | 292 prof_xml = self.__constructProfileXml(profile) |
293 return_xml = prof_xml.toxml() | 293 return_xml = prof_xml.toxml() |
294 prof_xml.unlink() | 294 prof_xml.unlink() |
295 | 295 |
298 def getParamsForCategory(self, category, profile_key='@DEFAULT@'): | 298 def getParamsForCategory(self, category, profile_key='@DEFAULT@'): |
299 """Return node's xml for selected category""" | 299 """Return node's xml for selected category""" |
300 #TODO: manage category of general type (without existant profile) | 300 #TODO: manage category of general type (without existant profile) |
301 profile = self.getProfileName(profile_key) | 301 profile = self.getProfileName(profile_key) |
302 if not profile: | 302 if not profile: |
303 error("Asking params for inexistant profile") | 303 error(_("Asking params for inexistant profile")) |
304 return "" | 304 return "" |
305 prof_xml = self.__constructProfileXml(profile) | 305 prof_xml = self.__constructProfileXml(profile) |
306 | 306 |
307 for node in prof_xml.getElementsByTagName("category"): | 307 for node in prof_xml.getElementsByTagName("category"): |
308 if node.nodeName == "category" and node.getAttribute("name") == category: | 308 if node.nodeName == "category" and node.getAttribute("name") == category: |
344 def setParam(self, name, value, category, profile_key='@DEFAULT@'): | 344 def setParam(self, name, value, category, profile_key='@DEFAULT@'): |
345 """Set a parameter, return None if the parameter is not in param xml""" | 345 """Set a parameter, return None if the parameter is not in param xml""" |
346 | 346 |
347 node = self.__getParamNode(name, category, '@ALL@') | 347 node = self.__getParamNode(name, category, '@ALL@') |
348 if not node: | 348 if not node: |
349 error('Requesting an unknown parameter (%s/%s)' % (category, name)) | 349 error(_('Requesting an unknown parameter (%(category)s/%(name)s)') % {'category':category, 'name':name}) |
350 return | 350 return |
351 | 351 |
352 if node[0] == 'general': | 352 if node[0] == 'general': |
353 self.params_gen[(category, name)] = value | 353 self.params_gen[(category, name)] = value |
354 self.host.bridge.paramUpdate(name, value, category) #TODO: add profile in signal | 354 self.host.bridge.paramUpdate(name, value, category) #TODO: add profile in signal |
356 | 356 |
357 assert (node[0] == 'individual') | 357 assert (node[0] == 'individual') |
358 | 358 |
359 profile = self.getProfileName(profile_key) | 359 profile = self.getProfileName(profile_key) |
360 if not profile: | 360 if not profile: |
361 error('Trying to set parameter for an unknown profile') | 361 error(_('Trying to set parameter for an unknown profile')) |
362 return #TODO: throw an error | 362 return #TODO: throw an error |
363 | 363 |
364 type = node[1].getAttribute("type") | 364 type = node[1].getAttribute("type") |
365 if type=="button": | 365 if type=="button": |
366 print "clique",node.toxml() | 366 print "clique",node.toxml() |
370 | 370 |
371 class Memory: | 371 class Memory: |
372 """This class manage all persistent informations""" | 372 """This class manage all persistent informations""" |
373 | 373 |
374 def __init__(self, host): | 374 def __init__(self, host): |
375 info ("Memory manager init") | 375 info (_("Memory manager init")) |
376 self.host = host | 376 self.host = host |
377 self.contacts={} | 377 self.contacts={} |
378 self.presenceStatus={} | 378 self.presenceStatus={} |
379 self.subscriptions={} | 379 self.subscriptions={} |
380 self.params=Param(host) | 380 self.params=Param(host) |
397 | 397 |
398 #parameters | 398 #parameters |
399 if os.path.exists(param_file_xml): | 399 if os.path.exists(param_file_xml): |
400 try: | 400 try: |
401 self.params.load_xml(param_file_xml) | 401 self.params.load_xml(param_file_xml) |
402 debug("params template loaded") | 402 debug(_("params template loaded")) |
403 except: | 403 except: |
404 error ("Can't load params template !") | 404 error (_("Can't load params template !")) |
405 self.params.load_default_params() | 405 self.params.load_default_params() |
406 else: | 406 else: |
407 info ("No params template, using default template") | 407 info (_("No params template, using default template")) |
408 self.params.load_default_params() | 408 self.params.load_default_params() |
409 | 409 |
410 try: | 410 try: |
411 self.params.load_data(param_file_data) | 411 self.params.load_data(param_file_data) |
412 debug("params loaded") | 412 debug(_("params loaded")) |
413 except: | 413 except: |
414 error ("Can't load params !") | 414 error (_("Can't load params !")) |
415 | 415 |
416 #history | 416 #history |
417 if os.path.exists(history_file): | 417 if os.path.exists(history_file): |
418 try: | 418 try: |
419 with open(history_file, 'r') as history_pickle: | 419 with open(history_file, 'r') as history_pickle: |
420 self.history=pickle.load(history_pickle) | 420 self.history=pickle.load(history_pickle) |
421 debug("history loaded") | 421 debug(_("history loaded")) |
422 except: | 422 except: |
423 error ("Can't load history !") | 423 error (_("Can't load history !")) |
424 | 424 |
425 #private | 425 #private |
426 if os.path.exists(private_file): | 426 if os.path.exists(private_file): |
427 try: | 427 try: |
428 with open(private_file, 'r') as private_pickle: | 428 with open(private_file, 'r') as private_pickle: |
429 self.private=pickle.load(private_pickle) | 429 self.private=pickle.load(private_pickle) |
430 debug("private values loaded") | 430 debug(_("private values loaded")) |
431 except: | 431 except: |
432 error ("Can't load private values !") | 432 error (_("Can't load private values !")) |
433 | 433 |
434 def save(self): | 434 def save(self): |
435 """Save parameters and all memory things to file/db""" | 435 """Save parameters and all memory things to file/db""" |
436 #TODO: need to encrypt files (at least passwords !) and set permissions | 436 #TODO: need to encrypt files (at least passwords !) and set permissions |
437 param_file_xml = os.path.expanduser(self.host.get_const('local_dir')+ | 437 param_file_xml = os.path.expanduser(self.host.get_const('local_dir')+ |
443 private_file = os.path.expanduser(self.host.get_const('local_dir')+ | 443 private_file = os.path.expanduser(self.host.get_const('local_dir')+ |
444 self.host.get_const('savefile_private')) | 444 self.host.get_const('savefile_private')) |
445 | 445 |
446 self.params.save_xml(param_file_xml) | 446 self.params.save_xml(param_file_xml) |
447 self.params.save_data(param_file_data) | 447 self.params.save_data(param_file_data) |
448 debug("params saved") | 448 debug(_("params saved")) |
449 with open(history_file, 'w') as history_pickle: | 449 with open(history_file, 'w') as history_pickle: |
450 pickle.dump(self.history, history_pickle) | 450 pickle.dump(self.history, history_pickle) |
451 debug("history saved") | 451 debug(_("history saved")) |
452 with open(private_file, 'w') as private_pickle: | 452 with open(private_file, 'w') as private_pickle: |
453 pickle.dump(self.private, private_pickle) | 453 pickle.dump(self.private, private_pickle) |
454 debug("private values saved") | 454 debug(_("private values saved")) |
455 | 455 |
456 def getProfilesList(self): | 456 def getProfilesList(self): |
457 return self.params.getProfilesList() | 457 return self.params.getProfilesList() |
458 | 458 |
459 | 459 |
492 self.history[me_short][key][int(time.time())] = (from_short, message) | 492 self.history[me_short][key][int(time.time())] = (from_short, message) |
493 | 493 |
494 def getHistory(self, from_jid, to_jid, size): | 494 def getHistory(self, from_jid, to_jid, size): |
495 ret={} | 495 ret={} |
496 if not self.history.has_key(from_jid): | 496 if not self.history.has_key(from_jid): |
497 error("source JID not found !") | 497 error(_("source JID not found !")) |
498 #TODO: throw an error here | 498 #TODO: throw an error here |
499 return {} | 499 return {} |
500 if not self.history[from_jid].has_key(to_jid): | 500 if not self.history[from_jid].has_key(to_jid): |
501 error("dest JID not found !") | 501 error(_("dest JID not found !")) |
502 #TODO: throw an error here | 502 #TODO: throw an error here |
503 return {} | 503 return {} |
504 stamps=self.history[from_jid][to_jid].keys() | 504 stamps=self.history[from_jid][to_jid].keys() |
505 stamps.sort() | 505 stamps.sort() |
506 for stamp in stamps[-size:]: | 506 for stamp in stamps[-size:]: |
523 | 523 |
524 def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'): | 524 def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'): |
525 debug("Memory addContact: %s",contact_jid.userhost()) | 525 debug("Memory addContact: %s",contact_jid.userhost()) |
526 profile = self.getProfileName(profile_key) | 526 profile = self.getProfileName(profile_key) |
527 if not profile: | 527 if not profile: |
528 error ('Trying to add a contact to a non-existant profile') | 528 error (_('Trying to add a contact to a non-existant profile')) |
529 return | 529 return |
530 assert(isinstance(attributes,dict)) | 530 assert(isinstance(attributes,dict)) |
531 assert(isinstance(groups,set)) | 531 assert(isinstance(groups,set)) |
532 if not self.contacts.has_key(profile): | 532 if not self.contacts.has_key(profile): |
533 self.contacts[profile] = {} | 533 self.contacts[profile] = {} |
535 | 535 |
536 def delContact(self, contact_jid, profile_key='@DEFAULT@'): | 536 def delContact(self, contact_jid, profile_key='@DEFAULT@'): |
537 debug("Memory delContact: %s",contact_jid.userhost()) | 537 debug("Memory delContact: %s",contact_jid.userhost()) |
538 profile = self.getProfileName(profile_key) | 538 profile = self.getProfileName(profile_key) |
539 if not profile: | 539 if not profile: |
540 error ('Trying to delete a contact for a non-existant profile') | 540 error (_('Trying to delete a contact for a non-existant profile')) |
541 return | 541 return |
542 if self.contacts.has_key(profile) and self.contacts[profile].has_key(contact_jid.userhost()): | 542 if self.contacts.has_key(profile) and self.contacts[profile].has_key(contact_jid.userhost()): |
543 del self.contacts[profile][contact_jid.userhost()] | 543 del self.contacts[profile][contact_jid.userhost()] |
544 | 544 |
545 def getContact(self, contact_jid, profile_key='@DEFAULT@'): | 545 def getContact(self, contact_jid, profile_key='@DEFAULT@'): |
546 profile = self.getProfileName(profile_key) | 546 profile = self.getProfileName(profile_key) |
547 if not profile: | 547 if not profile: |
548 error('Asking a contact for a non-existant profile') | 548 error(_('Asking a contact for a non-existant profile')) |
549 return None | 549 return None |
550 if self.contacts.has_key(profile) and self.contacts[profile].has_key(contact_jid.userhost()): | 550 if self.contacts.has_key(profile) and self.contacts[profile].has_key(contact_jid.userhost()): |
551 self.contacts[profile][contact_jid.userhost()] | 551 self.contacts[profile][contact_jid.userhost()] |
552 else: | 552 else: |
553 return None | 553 return None |
557 @param profile_key: profile key | 557 @param profile_key: profile key |
558 @return list of [contact, attr, groups]""" | 558 @return list of [contact, attr, groups]""" |
559 debug ("Memory getContact OK (%s)", self.contacts) | 559 debug ("Memory getContact OK (%s)", self.contacts) |
560 profile = self.getProfileName(profile_key) | 560 profile = self.getProfileName(profile_key) |
561 if not profile: | 561 if not profile: |
562 error('Asking contacts for a non-existant profile') | 562 error(_('Asking contacts for a non-existant profile')) |
563 return [] | 563 return [] |
564 ret=[] | 564 ret=[] |
565 for contact in self.contacts[profile]: | 565 for contact in self.contacts[profile]: |
566 attr, groups = self.contacts[profile][contact] | 566 attr, groups = self.contacts[profile][contact] |
567 ret.append([contact, attr, groups ]) | 567 ret.append([contact, attr, groups ]) |
568 return ret | 568 return ret |
569 | 569 |
570 def addPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'): | 570 def addPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'): |
571 profile = self.getProfileName(profile_key) | 571 profile = self.getProfileName(profile_key) |
572 if not profile: | 572 if not profile: |
573 error('Trying to add presence status to a non-existant profile') | 573 error(_('Trying to add presence status to a non-existant profile')) |
574 return | 574 return |
575 if not self.presenceStatus.has_key(profile): | 575 if not self.presenceStatus.has_key(profile): |
576 self.presenceStatus[profile] = {} | 576 self.presenceStatus[profile] = {} |
577 if not self.presenceStatus[profile].has_key(contact_jid.userhost()): | 577 if not self.presenceStatus[profile].has_key(contact_jid.userhost()): |
578 self.presenceStatus[profile][contact_jid.userhost()] = {} | 578 self.presenceStatus[profile][contact_jid.userhost()] = {} |
596 | 596 |
597 def getWaitingSub(self, profile_key='@DEFAULT@'): | 597 def getWaitingSub(self, profile_key='@DEFAULT@'): |
598 """Called to get a list of currently waiting subscription requests""" | 598 """Called to get a list of currently waiting subscription requests""" |
599 profile = self.getProfileName(profile_key) | 599 profile = self.getProfileName(profile_key) |
600 if not profile: | 600 if not profile: |
601 error('Asking waiting subscriptions for a non-existant profile') | 601 error(_('Asking waiting subscriptions for a non-existant profile')) |
602 return {} | 602 return {} |
603 if not self.subscriptions.has_key(profile): | 603 if not self.subscriptions.has_key(profile): |
604 return {} | 604 return {} |
605 | 605 |
606 return self.subscriptions[profile] | 606 return self.subscriptions[profile] |
607 | 607 |
608 def getPresenceStatus(self, profile_key='@DEFAULT@'): | 608 def getPresenceStatus(self, profile_key='@DEFAULT@'): |
609 profile = self.getProfileName(profile_key) | 609 profile = self.getProfileName(profile_key) |
610 if not profile: | 610 if not profile: |
611 error('Asking contacts for a non-existant profile') | 611 error(_('Asking contacts for a non-existant profile')) |
612 return {} | 612 return {} |
613 if not self.presenceStatus.has_key(profile): | 613 if not self.presenceStatus.has_key(profile): |
614 self.presenceStatus[profile] = {} | 614 self.presenceStatus[profile] = {} |
615 debug ("Memory getPresenceStatus (%s)", self.presenceStatus[profile]) | 615 debug ("Memory getPresenceStatus (%s)", self.presenceStatus[profile]) |
616 return self.presenceStatus[profile] | 616 return self.presenceStatus[profile] |