diff sat/memory/memory.py @ 3371:e8d74ac7c479

core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
author Goffi <goffi@goffi.org>
date Mon, 28 Sep 2020 17:00:14 +0200
parents d24252df91ee
children be6d91572633
line wrap: on
line diff
--- a/sat/memory/memory.py	Sun Sep 20 14:05:40 2020 +0200
+++ b/sat/memory/memory.py	Mon Sep 28 17:00:14 2020 +0200
@@ -41,6 +41,7 @@
 from sat.memory.crypto import PasswordHasher
 from sat.tools import config as tools_config
 from sat.tools.common import data_format
+from sat.tools.common import regex
 
 
 log = getLogger(__name__)
@@ -238,6 +239,7 @@
         self.auth_sessions = PasswordSessions()  # remember the authenticated profiles
         self.disco = Discovery(host)
         self.config = tools_config.parseMainConf(log_filenames=True)
+        self._cache_path = Path(self.getConfig("", "local_dir"), C.CACHE_DIR)
         database_file = os.path.expanduser(
             os.path.join(self.getConfig("", "local_dir"), C.SAVEFILE_DATABASE)
         )
@@ -1765,6 +1767,23 @@
         files_path = self.host.getLocalPath(None, C.FILES_DIR, profile=False)
         yield self._deleteFile(client, peer_jid, recursive, files_path, file_data)
 
+    ## Cache ##
+
+    def getCachePath(self, namespace: str, *args: str) -> Path:
+        """Get path to use to get a common path for a namespace
+
+        This can be used by plugins to manage permanent data. It's the responsability
+        of plugins to clean this directory from unused data.
+        @param namespace: unique namespace to use
+        @param args: extra identifier which will be added to the path
+        """
+        namespace = namespace.strip().lower()
+        return Path(
+            self._cache_path,
+            regex.pathEscape(namespace),
+            *(regex.pathEscape(a) for a in args)
+        )
+
     ## Misc ##
 
     def isEntityAvailable(self, client, entity_jid):