Philipp Hörist pushed to branch master at gajim / gajim


Commits:
f488fe1f by Philipp Hörist at 2024-09-29T19:06:06+02:00
refactor: Create temp dir on demand

- - - - -


4 changed files:

- gajim/common/configpaths.py
- gajim/common/modules/httpupload.py
- gajim/gtk/message_actions_box.py
- gajim/gtk/voice_message_recorder.py


Changes:

=====================================
gajim/common/configpaths.py
=====================================
@@ -35,6 +35,10 @@ def get(key: str) -> Path:
     return _paths[key]
 
 
+def get_temp_dir() -> Path:
+    return _paths.get_temp_dir()
+
+
 def get_plugin_dirs() -> list[Path]:
     if gajim.IS_FLATPAK:
         return [Path(_paths['PLUGINS_BASE']),
@@ -88,7 +92,7 @@ def create_paths() -> None:
 
 
 def cleanup_temp() -> None:
-    tmpdir = _paths['TMP'].parent
+    tmpdir = _paths.get_temp_dir().parent
     for path in tmpdir.glob('gajim-*'):
         if not path.is_dir():
             continue
@@ -101,6 +105,8 @@ def cleanup_temp() -> None:
 class ConfigPaths:
     def __init__(self) -> None:
         self._paths: dict[str, PathTupleT] = {}
+        self._temp_dir: Path | None = None
+
         self.profile = ''
         self.profile_separation = False
         self.custom_config_root: Path | None = None
@@ -173,7 +179,6 @@ def init(self):
             self.cache_root = self.data_root = self.custom_config_root
 
         user_dir_paths = [
-            ('TMP', Path(tempfile.mkdtemp(prefix='gajim-')), None, None),
             ('MY_CONFIG', Path(), PathLocation.CONFIG, PathType.FOLDER),
             ('MY_CACHE', Path(), PathLocation.CACHE, PathType.FOLDER),
             ('MY_DATA', Path(), PathLocation.DATA, PathType.FOLDER),
@@ -231,5 +236,10 @@ def init(self):
         for path in paths:
             self.add(*path)
 
+    def get_temp_dir(self) -> Path:
+        if self._temp_dir is None or not self._temp_dir.exists():
+            self._temp_dir = Path(tempfile.mkdtemp(prefix='gajim-'))
+        return self._temp_dir
+
 
 _paths = ConfigPaths()


=====================================
gajim/common/modules/httpupload.py
=====================================
@@ -420,7 +420,7 @@ def filename(self) -> str:
 
     @staticmethod
     def _get_temp_path() -> Path:
-        tempdir = configpaths.get('TMP')
+        tempdir = configpaths.get_temp_dir()
         return tempdir / get_random_string(16)
 
     def set_error(self, domain: str, text: str = '') -> None:


=====================================
gajim/gtk/message_actions_box.py
=====================================
@@ -768,7 +768,7 @@ def _on_paste_clipboard(self,
             log.info('No image pasted')
             return
 
-        temp_dir = configpaths.get('TMP')
+        temp_dir = configpaths.get_temp_dir()
         image_path = temp_dir / f'{uuid.uuid4()}.png'
 
         try:


=====================================
gajim/gtk/voice_message_recorder.py
=====================================
@@ -116,10 +116,9 @@ def __init__(self, error_callback: Callable[[int, str], 
None]) -> None:
 
         # Voice message storage location
         self._filetype = 'm4a'
-        self._output_dir = configpaths.get('TMP')
         timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
         self._file_name = f'voice-message-{timestamp}.{self._filetype}'
-        self._file_path: Path = self._output_dir / self._file_name
+        self._file_path: Path = configpaths.get_temp_dir() / self._file_name
 
         if self._audio_input_device == 'wasapisrc':
             self._audiosrc.set_property('role', 'comms')
@@ -185,7 +184,7 @@ def start_recording(self) -> None:
             self._new_recording = False
             timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
             self._file_name = f'voice-message-{timestamp}.{self._filetype}'
-            self._file_path = self._output_dir / self._file_name
+            self._file_path = configpaths.get_temp_dir() / self._file_name
 
         self._output_file_counter += 1
         self._filesink.set_property(



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/f488fe1f93a5fbbe299f94f1f3699ca0aea95afc

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/f488fe1f93a5fbbe299f94f1f3699ca0aea95afc
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to