comparison src/core/sat_main.py @ 2496:769e8d9d2438

core: new getLocalPath method to retrieve a path in SàT local dir, which may be specific to a profile or component
author Goffi <goffi@goffi.org>
date Wed, 28 Feb 2018 18:28:39 +0100
parents 0046283a285d
children d485e9416493
comparison
equal deleted inserted replaced
2495:537a4a8075f8 2496:769e8d9d2438
31 from sat.core.constants import Const as C 31 from sat.core.constants import Const as C
32 from sat.memory.memory import Memory 32 from sat.memory.memory import Memory
33 from sat.tools import trigger 33 from sat.tools import trigger
34 from sat.tools import utils 34 from sat.tools import utils
35 from sat.tools.common import dynamic_import 35 from sat.tools.common import dynamic_import
36 from sat.tools.common import regex
36 from sat.stdui import ui_contact_list, ui_profile_manager 37 from sat.stdui import ui_contact_list, ui_profile_manager
37 import sat.plugins 38 import sat.plugins
38 from glob import glob 39 from glob import glob
39 import sys 40 import sys
40 import os.path 41 import os.path
53 self._menus = OrderedDict() # dynamic menus. key: callback_id, value: menu data (dictionnary) 54 self._menus = OrderedDict() # dynamic menus. key: callback_id, value: menu data (dictionnary)
54 self._menus_paths = {} # path to id. key: (menu_type, lower case tuple of path), value: menu id 55 self._menus_paths = {} # path to id. key: (menu_type, lower case tuple of path), value: menu id
55 self.initialised = defer.Deferred() 56 self.initialised = defer.Deferred()
56 self.profiles = {} 57 self.profiles = {}
57 self.plugins = {} 58 self.plugins = {}
58 self._ns_map = {u'x-data': u'jabber:x:data'} # map for short name to whole namespace, 59 self.ns_map = {u'x-data': u'jabber:x:data'} # map for short name to whole namespace,
59 # extended by plugins with registerNamespace 60 # extended by plugins with registerNamespace
60 self.memory = Memory(self) 61 self.memory = Memory(self)
61 self.trigger = trigger.TriggerManager() # trigger are used to change SàT behaviour 62 self.trigger = trigger.TriggerManager() # trigger are used to change SàT behaviour
62 63
63 bridge_name = self.memory.getConfig('', 'bridge', 'dbus') 64 bridge_name = self.memory.getConfig('', 'bridge', 'dbus')
489 can be used as last errback to show unexpected error 490 can be used as last errback to show unexpected error
490 """ 491 """
491 log.error(_(u"Unexpected error: {}".format(failure_))) 492 log.error(_(u"Unexpected error: {}".format(failure_)))
492 return failure_ 493 return failure_
493 494
495 # namespaces
496
497 def registerNamespace(self, short_name, namespace):
498 """associate a namespace to a short name"""
499 if short_name in self.ns_map:
500 raise exceptions.ConflictError(u'this short name is already used')
501 self.ns_map[short_name] = namespace
502
503 def getNamespaces(self):
504 return self.ns_map
505
506 def getSessionInfos(self, profile_key):
507 """compile interesting data on current profile session"""
508 client = self.getClient(profile_key)
509 data = {
510 "jid": client.jid.full(),
511 "started": unicode(int(client.started)),
512 }
513 return defer.succeed(data)
514
515 # local dirs
516
517 def getLocalPath(self, client, dir_name, *extra_path, **kwargs):
518 """retrieve path for local data
519
520 if path doesn't exist, it will be created
521 @param client(SatXMPPClient, None): client instance
522 used when profile is set, can be None if profile is False
523 @param dir_name(unicode): name of the main path directory
524 @param component(bool): if True, path will be prefixed with C.COMPONENTS_DIR
525 @param profile(bool): if True, path will be suffixed by profile name
526 @param *extra_path: extra path element(s) to use
527 @return (unicode): path
528 """
529 # FIXME: component and profile are parsed with **kwargs because of python 2 limitations
530 # once moved to python 3, this can be fixed
531 component = kwargs.pop('component', False)
532 profile = kwargs.pop('profile', True)
533 assert not kwargs
534
535 path_elts = [self.memory.getConfig('', 'local_dir')]
536 if component:
537 path_elts.append(C.COMPONENTS_DIR)
538 path_elts.append(regex.pathEscape(dir_name))
539 if extra_path:
540 path_elts.extend([regex.pathEscape(p) for p in extra_path])
541 if profile:
542 regex.pathEscape(client.profile)
543 path = os.path.join(*path_elts)
544 if not os.path.exists(path):
545 os.makedirs(path)
546 return path
547
494 ## Client management ## 548 ## Client management ##
495 549
496 def setParam(self, name, value, category, security_limit, profile_key): 550 def setParam(self, name, value, category, security_limit, profile_key):
497 """set wanted paramater and notice observers""" 551 """set wanted paramater and notice observers"""
498 self.memory.setParam(name, value, category, security_limit, profile_key) 552 self.memory.setParam(name, value, category, security_limit, profile_key)
507 log.error(_('asking connection status for a non-existant profile')) 561 log.error(_('asking connection status for a non-existant profile'))
508 raise exceptions.ProfileUnknownError(profile_key) 562 raise exceptions.ProfileUnknownError(profile_key)
509 if profile not in self.profiles: 563 if profile not in self.profiles:
510 return False 564 return False
511 return self.profiles[profile].isConnected() 565 return self.profiles[profile].isConnected()
512
513 566
514 ## XMPP methods ## 567 ## XMPP methods ##
515 568
516 def _messageSend(self, to_jid_s, message, subject=None, mess_type='auto', extra=None, profile_key=C.PROF_KEY_NONE): 569 def _messageSend(self, to_jid_s, message, subject=None, mess_type='auto', extra=None, profile_key=C.PROF_KEY_NONE):
517 client = self.getClient(profile_key) 570 client = self.getClient(profile_key)
954 raise exceptions.DataError("Trying to access an unknown menu") 1007 raise exceptions.DataError("Trying to access an unknown menu")
955 languageSwitch(language) 1008 languageSwitch(language)
956 help_string = _(menu_data['help_string']) 1009 help_string = _(menu_data['help_string'])
957 languageSwitch() 1010 languageSwitch()
958 return help_string 1011 return help_string
959
960 # misc methods
961
962 def registerNamespace(self, short_name, namespace):
963 """associate a namespace to a short name"""
964 if short_name in self._ns_map:
965 raise exceptions.ConflictError(u'this short name is already used')
966 self._ns_map[short_name] = namespace
967
968 def getNamespaces(self):
969 return self._ns_map
970
971 def getSessionInfos(self, profile_key):
972 """compile interesting data on current profile session"""
973 client = self.getClient(profile_key)
974 data = {
975 "jid": client.jid.full(),
976 "started": unicode(int(client.started)),
977 }
978 return defer.succeed(data)