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


Commits:
1f081612 by Philipp Hörist at 2022-11-05T23:27:57+01:00
refactor: Make open_file() only accept Path objects

- - - - -


3 changed files:

- gajim/common/helpers.py
- gajim/gtk/conversation/rows/file_transfer_jingle.py
- gajim/gtk/filetransfer.py


Changes:

=====================================
gajim/common/helpers.py
=====================================
@@ -1111,16 +1111,12 @@ def open_file_uri(uri: str) -> None:
 
 
 @catch_exceptions
-def open_file(path: Union[str, Path]) -> None:
-    if sys.platform == 'win32':
-        os.startfile(path)
-    else:
-        # Call str() to make it work with pathlib.Path
-        path = str(path)
-        if path.startswith('/'):
-            open_file_uri('file://' + escape_iri_path(path))
-        else:
-            open_file_uri(path)
+def open_file(path: Path) -> None:
+    if not path.exists():
+        log.warning('Unable to open file, path %s does not exist', path)
+        return
+
+    open_file_uri(path.as_uri())
 
 
 def filesystem_path_from_uri(uri: str) -> Optional[Path]:


=====================================
gajim/gtk/conversation/rows/file_transfer_jingle.py
=====================================
@@ -18,9 +18,9 @@
 from typing import Optional
 
 import logging
-import os
 import time
 from datetime import datetime
+from pathlib import Path
 
 from gi.repository import GdkPixbuf
 from gi.repository import GLib
@@ -347,15 +347,14 @@ def _on_reject_file_request(self, _button: Gtk.Button) -> 
None:
     def _on_open_file(self, _button: Gtk.Button) -> None:
         assert self._file_props is not None
         assert self._file_props.file_name is not None
-        if os.path.exists(self._file_props.file_name):
-            open_file(self._file_props.file_name)
+        open_file(Path(self._file_props.file_name))
 
     def _on_open_folder(self, _button: Gtk.Button) -> None:
         assert self._file_props is not None
         assert self._file_props.file_name is not None
-        path = os.path.split(self._file_props.file_name)[0]
-        if os.path.exists(path) and os.path.isdir(path):
-            open_file(path)
+
+        folder = Path(self._file_props.file_name).parent
+        open_file(folder)
 
     def _on_bad_hash_retry(self, _button: Gtk.Button) -> None:
         app.interface.instances['file_transfers'].show_hash_error(


=====================================
gajim/gtk/filetransfer.py
=====================================
@@ -29,6 +29,7 @@
 from enum import unique
 from datetime import datetime
 from datetime import timezone
+from pathlib import Path
 
 from gi.repository import Gtk
 from gi.repository import Gdk
@@ -1030,9 +1031,9 @@ def _on_open_folder_menuitem_activate(self, widget):
         file_props = FilesProp.getFilePropByType(sid[0], sid[1:])
         if not file_props.file_name:
             return
-        path = os.path.split(file_props.file_name)[0]
-        if os.path.exists(path) and os.path.isdir(path):
-            open_file(path)
+
+        folder = Path(file_props.file_name).parent
+        open_file(folder)
 
     def _on_cancel_menuitem_activate(self, widget):
         self._on_cancel_button_clicked(widget)



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

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


_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to