changeset 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 537a4a8075f8
children 38f472dbfcf0
files src/core/sat_main.py
diffstat 1 files changed, 55 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/sat_main.py	Wed Feb 28 18:28:39 2018 +0100
+++ b/src/core/sat_main.py	Wed Feb 28 18:28:39 2018 +0100
@@ -33,6 +33,7 @@
 from sat.tools import trigger
 from sat.tools import utils
 from sat.tools.common import dynamic_import
+from sat.tools.common import regex
 from sat.stdui import ui_contact_list, ui_profile_manager
 import sat.plugins
 from glob import glob
@@ -55,7 +56,7 @@
         self.initialised = defer.Deferred()
         self.profiles = {}
         self.plugins = {}
-        self._ns_map = {u'x-data': u'jabber:x:data'}  # map for short name to whole namespace,
+        self.ns_map = {u'x-data': u'jabber:x:data'}  # map for short name to whole namespace,
                                                       # extended by plugins with registerNamespace
         self.memory = Memory(self)
         self.trigger = trigger.TriggerManager()  # trigger are used to change SàT behaviour
@@ -491,6 +492,59 @@
         log.error(_(u"Unexpected error: {}".format(failure_)))
         return failure_
 
+    # namespaces
+
+    def registerNamespace(self, short_name, namespace):
+        """associate a namespace to a short name"""
+        if short_name in self.ns_map:
+            raise exceptions.ConflictError(u'this short name is already used')
+        self.ns_map[short_name] = namespace
+
+    def getNamespaces(self):
+        return self.ns_map
+
+    def getSessionInfos(self, profile_key):
+        """compile interesting data on current profile session"""
+        client = self.getClient(profile_key)
+        data = {
+            "jid": client.jid.full(),
+            "started": unicode(int(client.started)),
+            }
+        return defer.succeed(data)
+
+    # local dirs
+
+    def getLocalPath(self, client, dir_name, *extra_path, **kwargs):
+        """retrieve path for local data
+
+        if path doesn't exist, it will be created
+        @param client(SatXMPPClient, None): client instance
+            used when profile is set, can be None if profile is False
+        @param dir_name(unicode): name of the main path directory
+        @param component(bool): if True, path will be prefixed with C.COMPONENTS_DIR
+        @param profile(bool): if True, path will be suffixed by profile name
+        @param *extra_path: extra path element(s) to use
+        @return (unicode): path
+        """
+        # FIXME: component and profile are parsed with **kwargs because of python 2 limitations
+        #        once moved to python 3, this can be fixed
+        component = kwargs.pop('component', False)
+        profile = kwargs.pop('profile', True)
+        assert not kwargs
+
+        path_elts = [self.memory.getConfig('', 'local_dir')]
+        if component:
+            path_elts.append(C.COMPONENTS_DIR)
+        path_elts.append(regex.pathEscape(dir_name))
+        if extra_path:
+            path_elts.extend([regex.pathEscape(p) for p in extra_path])
+        if profile:
+            regex.pathEscape(client.profile)
+        path = os.path.join(*path_elts)
+        if not os.path.exists(path):
+            os.makedirs(path)
+        return path
+
     ## Client management ##
 
     def setParam(self, name, value, category, security_limit, profile_key):
@@ -510,7 +564,6 @@
             return False
         return self.profiles[profile].isConnected()
 
-
     ## XMPP methods ##
 
     def _messageSend(self, to_jid_s, message, subject=None, mess_type='auto', extra=None, profile_key=C.PROF_KEY_NONE):
@@ -956,23 +1009,3 @@
         help_string = _(menu_data['help_string'])
         languageSwitch()
         return help_string
-
-    # misc methods
-
-    def registerNamespace(self, short_name, namespace):
-        """associate a namespace to a short name"""
-        if short_name in self._ns_map:
-            raise exceptions.ConflictError(u'this short name is already used')
-        self._ns_map[short_name] = namespace
-
-    def getNamespaces(self):
-        return self._ns_map
-
-    def getSessionInfos(self, profile_key):
-        """compile interesting data on current profile session"""
-        client = self.getClient(profile_key)
-        data = {
-            "jid": client.jid.full(),
-            "started": unicode(int(client.started)),
-            }
-        return defer.succeed(data)