Philipp Hörist pushed to branch master at gajim / gajim
Commits: b749795f by André at 2020-10-22T22:28:24+02:00 configpaths: Port to pathlib
- - - - -
26 changed files:
- gajim/application.py - gajim/common/app.py - gajim/common/cert_store.py - gajim/common/configpaths.py - gajim/common/helpers.py - gajim/common/jingle_content.py - gajim/common/jingle_ft.py - gajim/common/jingle_xtls.py - gajim/common/modules/bits_of_binary.py - gajim/common/settings.py - gajim/common/storage/archive.py - gajim/common/storage/cache.py - gajim/common/types.py - gajim/gtk/account_wizard.py - gajim/gtk/avatar.py - gajim/gtk/css_config.py - gajim/gtk/emoji_chooser.py - gajim/gtk/exception.py - gajim/gtk/manage_sounds.py - gajim/gtk/util.py - gajim/history_manager.py - gajim/plugins/gajimplugin.py - gajim/plugins/helpers.py - gajim/plugins/pluginmanager.py - gajim/plugins/plugins_i18n.py - gajim/vcard.py
Changes:
===================================== gajim/application.py ===================================== @@ -36,7 +36,6 @@ import time import sys from datetime import datetime -from pathlib import Path from urllib.parse import unquote
from nbxmpp.namespaces import Namespace @@ -384,7 +383,7 @@ def warn_with_traceback(message, category, filename, lineno,
@staticmethod def _redirect_output(): - debug_folder = Path(configpaths.get('DEBUG')) + debug_folder = configpaths.get('DEBUG') date = datetime.today().strftime('%d%m%Y-%H%M%S') filename = '%s-debug.log' % date fd = open(debug_folder / filename, 'a') @@ -392,7 +391,7 @@ def _redirect_output():
@staticmethod def _cleanup_debug_logs(): - debug_folder = Path(configpaths.get('DEBUG')) + debug_folder = configpaths.get('DEBUG') debug_files = list(debug_folder.glob('*-debug.log*')) now = time.time() for file in debug_files:
===================================== gajim/common/app.py ===================================== @@ -34,7 +34,6 @@ import sys import logging import uuid -from pathlib import Path from collections import namedtuple from collections import defaultdict
@@ -631,7 +630,7 @@ def load_css_config(): css_config = CSSConfig()
def set_debug_mode(enable: bool) -> None: - debug_folder = Path(configpaths.get('DEBUG')) + debug_folder = configpaths.get('DEBUG') debug_enabled = debug_folder / 'debug-enabled' if enable: debug_enabled.touch() @@ -640,7 +639,7 @@ def set_debug_mode(enable: bool) -> None: debug_enabled.unlink()
def get_debug_mode() -> bool: - debug_folder = Path(configpaths.get('DEBUG')) + debug_folder = configpaths.get('DEBUG') debug_enabled = debug_folder / 'debug-enabled' return debug_enabled.exists()
@@ -648,7 +647,7 @@ def get_stored_bob_data(algo_hash: str) -> Optional[bytes]: try: return bob_cache[algo_hash] except KeyError: - filepath = Path(configpaths.get('BOB')) / algo_hash + filepath = configpaths.get('BOB') / algo_hash if filepath.exists(): with open(str(filepath), 'r+b') as file: data = file.read()
===================================== gajim/common/cert_store.py ===================================== @@ -14,7 +14,6 @@
import logging -from pathlib import Path
from gi.repository import GLib from gi.repository import Gio @@ -30,7 +29,7 @@
class CertificateStore: def __init__(self): - self._path = Path(configpaths.get('CERT_STORE')) + self._path = configpaths.get('CERT_STORE') self._certs = []
self._load_certificates()
===================================== gajim/common/configpaths.py ===================================== @@ -38,7 +38,7 @@ from gajim.common.types import PathTuple
-def get(key: str) -> str: +def get(key: str) -> Path: return _paths[key]
@@ -50,7 +50,7 @@ def get_plugin_dirs() -> List[Path]: Path(_paths['PLUGINS_USER'])]
-def get_paths(type_: PathType) -> Generator[str, None, None]: +def get_paths(type_: PathType) -> Generator[Path, None, None]: for key, value in _paths.items(): path_type = value[2] if type_ != path_type: @@ -71,7 +71,7 @@ def set_profile(profile: str) -> None:
def set_config_root(config_root: str) -> None: - _paths.custom_config_root = str(Path(config_root).resolve()) + _paths.custom_config_root = Path(config_root).resolve()
def init() -> None: @@ -80,24 +80,21 @@ def init() -> None:
def create_paths() -> None: for path in get_paths(PathType.FOLDER): - if not isinstance(path, Path): - path_ = Path(path) - - if path_.is_file(): - print(_('%s is a file but it should be a directory') % path_) + if path.is_file(): + print(_('%s is a file but it should be a directory') % path) print(_('Gajim will now exit')) sys.exit()
- if not path_.exists(): - for parent_path in reversed(path_.parents): + if not path.exists(): + for parent_path in reversed(path.parents): # Create all parent folders # don't use mkdir(parent=True), as it ignores `mode` # when creating the parents if not parent_path.exists(): print(('creating %s directory') % parent_path) parent_path.mkdir(mode=0o700) - print(('creating %s directory') % path_) - path_.mkdir(mode=0o700) + print(('creating %s directory') % path) + path.mkdir(mode=0o700)
class ConfigPaths: @@ -105,67 +102,66 @@ def __init__(self) -> None: self._paths = {} # type: Dict[str, PathTuple] self.profile = '' self.profile_separation = False - self.custom_config_root = None # type: Optional[str] + self.custom_config_root = None # type: Optional[Path]
if os.name == 'nt': try: # Documents and Settings[User Name]\Application Data\Gajim self.config_root = self.cache_root = self.data_root = \ - os.path.join(os.environ['appdata'], 'Gajim') + Path(os.environ['appdata']) / 'Gajim' except KeyError: # win9x, in cwd - self.config_root = self.cache_root = self.data_root = '.' + self.config_root = self.cache_root = self.data_root = Path('.') else: - self.config_root = os.path.join(GLib.get_user_config_dir(), - 'gajim') - self.cache_root = os.path.join(GLib.get_user_cache_dir(), 'gajim') - self.data_root = os.path.join(GLib.get_user_data_dir(), 'gajim') + self.config_root = Path(GLib.get_user_config_dir()) / 'gajim' + self.cache_root = Path(GLib.get_user_cache_dir()) / 'gajim' + self.data_root = Path(GLib.get_user_data_dir()) / 'gajim'
import pkg_resources - basedir = pkg_resources.resource_filename("gajim", ".") + basedir = Path(pkg_resources.resource_filename("gajim", "."))
source_paths = [ - ('DATA', os.path.join(basedir, 'data')), - ('STYLE', os.path.join(basedir, 'data', 'style')), - ('EMOTICONS', os.path.join(basedir, 'data', 'emoticons')), - ('GUI', os.path.join(basedir, 'data', 'gui')), - ('ICONS', os.path.join(basedir, 'data', 'icons')), - ('HOME', os.path.expanduser('~')), - ('PLUGINS_BASE', os.path.join(basedir, 'data', 'plugins')), + ('DATA', basedir / 'data'), + ('STYLE', basedir / 'data' / 'style'), + ('EMOTICONS', basedir / 'data' / 'emoticons'), + ('GUI', basedir / 'data' / 'gui'), + ('ICONS', basedir / 'data' / 'icons'), + ('HOME', Path.home()), + ('PLUGINS_BASE', basedir / 'data' / 'plugins'), ]
for path in source_paths: self.add(*path)
- def __getitem__(self, key: str) -> str: + def __getitem__(self, key: str) -> Path: location, path, _ = self._paths[key] if location == PathLocation.CONFIG: - return os.path.join(self.config_root, path) + return self.config_root / path if location == PathLocation.CACHE: - return os.path.join(self.cache_root, path) + return self.cache_root / path if location == PathLocation.DATA: - return os.path.join(self.data_root, path) + return self.data_root / path return path
def items(self) -> Generator[Tuple[str, PathTuple], None, None]: for key, value in self._paths.items(): yield (key, value)
- def _prepare(self, path: str, unique: bool) -> str: + def _prepare(self, path: Path, unique: bool) -> Path: if os.name == 'nt': - path = path.capitalize() + path = Path(str(path).capitalize()) if self.profile: if unique or self.profile_separation: - return '%s.%s' % (path, self.profile) + return Path(f'{path}.{self.profile}') return path
def add(self, name: str, - path: str, + path: Path, location: PathLocation = None, path_type: PathType = None, unique: bool = False) -> None: - if path and location is not None: + if location is not None: path = self._prepare(path, unique) self._paths[name] = (location, path, path_type)
@@ -175,10 +171,10 @@ def init(self): self.cache_root = self.data_root = self.custom_config_root
user_dir_paths = [ - ('TMP', tempfile.gettempdir()), - ('MY_CONFIG', '', PathLocation.CONFIG, PathType.FOLDER), - ('MY_CACHE', '', PathLocation.CACHE, PathType.FOLDER), - ('MY_DATA', '', PathLocation.DATA, PathType.FOLDER), + ('TMP', Path(tempfile.gettempdir())), + ('MY_CONFIG', Path(), PathLocation.CONFIG, PathType.FOLDER), + ('MY_CACHE', Path(), PathLocation.CACHE, PathType.FOLDER), + ('MY_DATA', Path(), PathLocation.DATA, PathType.FOLDER), ]
for path in user_dir_paths:
===================================== gajim/common/helpers.py ===================================== @@ -51,6 +51,7 @@ from urllib.parse import unquote from encodings.punycode import punycode_encode from functools import wraps +from pathlib import Path from packaging.version import Version as V
from nbxmpp.namespaces import Namespace @@ -447,18 +448,18 @@ def check_soundfile_path(file_, dirs=None): (eg: ~/.gajim/sounds/, DATADIR/sounds...). :return the path to file or None if it doesn't exists. """ + if not file_: + return None + if Path(file_).exists(): + return Path(file_) + if dirs is None: dirs = [configpaths.get('MY_DATA'), configpaths.get('DATA')]
- if not file_: - return None - if os.path.exists(file_): - return file_ - for dir_ in dirs: - dir_ = os.path.join(dir_, 'sounds', file_) - if os.path.exists(dir_): + dir_ = dir_ / 'sounds' / file_ + if dir_.exists(): return dir_ return None
@@ -482,11 +483,12 @@ def strip_soundfile_path(file_, dirs=None, abs_=True): dirs = [configpaths.get('MY_DATA'), configpaths.get('DATA')]
- name = os.path.basename(file_) + file_ = Path(file_) + name = file_.name for dir_ in dirs: - dir_ = os.path.join(dir_, 'sounds', name) + dir_ = dir_ / 'sounds' / name if abs_: - dir_ = os.path.abspath(dir_) + dir_ = dir_.absolute() if file_ == dir_: return name return file_ @@ -496,6 +498,7 @@ def play_sound_file(path_to_soundfile): if path_to_soundfile is None: return
+ path_to_soundfile = str(path_to_soundfile) if sys.platform == 'win32': import winsound try: @@ -854,25 +857,19 @@ def version_condition(current_version, required_version): return True
def get_available_emoticon_themes(): - emoticons_themes = ['font'] files = [] - dir_iterator = os.scandir(configpaths.get('EMOTICONS')) - for folder in dir_iterator: + for folder in configpaths.get('EMOTICONS').iterdir(): if not folder.is_dir(): continue - file_iterator = os.scandir(folder.path) - for theme in file_iterator: - if theme.is_file(): - files.append(theme.name) - - if os.path.isdir(configpaths.get('MY_EMOTS')): - files += os.listdir(configpaths.get('MY_EMOTS')) - - for file in files: - if file.endswith('.png'): - emoticons_themes.append(file[:-4]) - emoticons_themes.sort() - return emoticons_themes + files += [theme for theme in folder.iterdir() if theme.is_file()] + + my_emots = configpaths.get('MY_EMOTS') + if my_emots.is_dir(): + files += list(my_emots.iterdir()) + + emoticons_themes = ['font'] + emoticons_themes += [file.stem for file in files if file.suffix == '.png'] + return sorted(emoticons_themes)
def call_counter(func): def helper(self, restart=False):
===================================== gajim/common/jingle_content.py ===================================== @@ -19,8 +19,6 @@ from typing import Any # pylint: disable=unused-import from typing import Dict # pylint: disable=unused-import
-import os - import nbxmpp from nbxmpp.namespaces import Namespace
@@ -227,8 +225,8 @@ def _fill_content(self, content): if self.use_security: security = nbxmpp.simplexml.Node( tag=Namespace.JINGLE_XTLS + ' security') - certpath = os.path.join( - configpaths.get('MY_CERT'), SELF_SIGNED_CERTIFICATE) + '.cert' + certpath = configpaths.get('MY_CERT') / (SELF_SIGNED_CERTIFICATE + + '.cert') cert = load_cert_file(certpath) if cert: digest_algo = (cert.get_signature_algorithm()
===================================== gajim/common/jingle_ft.py ===================================== @@ -18,7 +18,6 @@
import hashlib import logging -import os import threading from enum import IntEnum, unique
@@ -119,10 +118,10 @@ def __init__(self, session, transport=None, file_props=None, State.CAND_SENT_AND_RECEIVED : StateCandSentAndRecv(self) }
- cert_name = os.path.join(configpaths.get('MY_CERT'), - jingle_xtls.SELF_SIGNED_CERTIFICATE) - if not (os.path.exists(cert_name + '.cert') - and os.path.exists(cert_name + '.pkey')): + cert_name = (configpaths.get('MY_CERT') / + jingle_xtls.SELF_SIGNED_CERTIFICATE) + if not (cert_name.with_suffix('.cert').exists() + and cert_name.with_suffix('.pkey').exists()): jingle_xtls.make_certs(cert_name, 'gajim')
def __state_changed(self, nextstate, args=None):
===================================== gajim/common/jingle_xtls.py ===================================== @@ -13,7 +13,7 @@ # along with Gajim. If not, see http://www.gnu.org/licenses/.
import logging -import os +from pathlib import Path
from OpenSSL import SSL, crypto
@@ -52,7 +52,7 @@ def load_cert_file(cert_path, cert_store=None): """ This is almost identical to the one in nbxmpp.tls_nb """ - if not os.path.isfile(cert_path): + if not cert_path.is_file(): return None try: f = open(cert_path) @@ -100,22 +100,22 @@ def get_context(fingerprint, verify_cb=None, remote_jid=None): elif fingerprint == 'client': ctx.set_verify(SSL.VERIFY_PEER, verify_cb or default_callback)
- cert_name = os.path.join(configpaths.get('MY_CERT'), SELF_SIGNED_CERTIFICATE) - ctx.use_privatekey_file((cert_name + '.pkey').encode('utf-8')) - ctx.use_certificate_file((cert_name + '.cert').encode('utf-8')) + cert_name = configpaths.get('MY_CERT') / SELF_SIGNED_CERTIFICATE + ctx.use_privatekey_file(str(cert_name.with_suffix('.pkey')).encode('utf-8')) + ctx.use_certificate_file(str(cert_name.with_suffix('.cert')).encode('utf-8'))
# Try to load Diffie-Hellman parameters. # First try user DH parameters, if this fails load the default DH parameters - dh_params_name = os.path.join(configpaths.get('MY_CERT'), DH_PARAMS) + dh_params_name = configpaths.get('MY_CERT') / DH_PARAMS try: with open(dh_params_name, "r"): ctx.load_tmp_dh(dh_params_name.encode('utf-8')) except FileNotFoundError as err: - default_dh_params_name = os.path.join(configpaths.get('DATA'), - 'other', DEFAULT_DH_PARAMS) + default_dh_params_name = (configpaths.get('DATA') / 'other' / + DEFAULT_DH_PARAMS) try: with open(default_dh_params_name, "r"): - ctx.load_tmp_dh(default_dh_params_name.encode('utf-8')) + ctx.load_tmp_dh(str(default_dh_params_name).encode('utf-8')) except FileNotFoundError as err: log.error('Unable to load default DH parameter file: %s, %s', default_dh_params_name, err) @@ -123,9 +123,9 @@ def get_context(fingerprint, verify_cb=None, remote_jid=None):
if remote_jid: store = ctx.get_cert_store() - path = os.path.join(os.path.expanduser(configpaths.get('MY_PEER_CERTS')), - remote_jid) + '.cert' - if os.path.exists(path): + path = configpaths.get('MY_PEER_CERTS').expanduser() / (remote_jid + + '.cert') + if path.exists(): load_cert_file(path, cert_store=store) log.debug('certificate file %s loaded fingerprint %s', path, fingerprint) @@ -140,8 +140,8 @@ def read_cert(certpath): return certificate
def send_cert(con, jid_from, sid): - certpath = os.path.join(configpaths.get('MY_CERT'), SELF_SIGNED_CERTIFICATE) + \ - '.cert' + certpath = configpaths.get('MY_CERT') / (SELF_SIGNED_CERTIFICATE + + '.cert') certificate = read_cert(certpath) iq = nbxmpp.Iq('result', to=jid_from) iq.setAttr('id', sid) @@ -159,8 +159,7 @@ def send_cert(con, jid_from, sid):
def handle_new_cert(con, obj, jid_from): jid = app.get_jid_without_resource(jid_from) - certpath = os.path.join(os.path.expanduser(configpaths.get('MY_PEER_CERTS')), jid) - certpath += '.cert' + certpath = configpaths.get('MY_PEER_CERTS').expanduser() / (jid + '.cert')
id_ = obj.getAttr('id')
@@ -177,9 +176,8 @@ def handle_new_cert(con, obj, jid_from): approve_pending_content(id_)
def check_cert(jid, fingerprint): - certpath = os.path.join(os.path.expanduser(configpaths.get('MY_PEER_CERTS')), jid) - certpath += '.cert' - if os.path.exists(certpath): + certpath = configpaths.get('MY_PEER_CERTS').expanduser() / (jid + '.cert') + if certpath.exists(): cert = load_cert_file(certpath) if cert: digest_algo = cert.get_signature_algorithm().decode('utf-8')\ @@ -273,12 +271,12 @@ def make_certs(filepath, CN): key = createKeyPair(TYPE_RSA, 4096) req = createCertRequest(key, CN=CN) cert = createCertificate(req, req, key, 0, 0, 60*60*24*365*5) # five years - with open(filepath + '.pkey', 'wb') as f: - os.chmod(filepath + '.pkey', 0o600) + with open(filepath.with_suffix('.pkey'), 'wb') as f: + filepath.with_suffix('.pkey').chmod(0o600) f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key)) - with open(filepath + '.cert', 'wb') as f: + with open(filepath.with_suffix('.cert'), 'wb') as f: f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
if __name__ == '__main__': - make_certs('./selfcert', 'gajim') + make_certs(Path('selfcert'), 'gajim')
===================================== gajim/common/modules/bits_of_binary.py ===================================== @@ -17,7 +17,6 @@ import logging import hashlib from base64 import b64decode -from pathlib import Path
import nbxmpp from nbxmpp.namespaces import Namespace @@ -133,7 +132,7 @@ def parse_bob_data(stanza): log.warning('No data found: %s', stanza) return None
- filepath = Path(configpaths.get('BOB')) / algo_hash + filepath = configpaths.get('BOB') / algo_hash if algo_hash in app.bob_cache or filepath.exists(): log.info('BoB data already cached') return None @@ -182,7 +181,7 @@ def store_bob_data(bob_data):
algo_hash = '%s+%s' % (bob_data.algo, bob_data.hash_)
- filepath = Path(configpaths.get('BOB')) / algo_hash + filepath = configpaths.get('BOB') / algo_hash if algo_hash in app.bob_cache or filepath.exists(): log.info('BoB data already cached') return None
===================================== gajim/common/settings.py ===================================== @@ -170,7 +170,7 @@ def _namedtuple_factory(cursor: Any, row: Any) -> Any: return namedtuple("Row", fields)(*row)
def _connect_database(self) -> None: - path = Path(configpaths.get('SETTINGS')) + path = configpaths.get('SETTINGS') if path.is_dir(): log.error('%s is a directory but should be a file', path) sys.exit() @@ -237,7 +237,7 @@ def _migrate(self) -> None: pass
def _migrate_old_config(self) -> None: - config_file = Path(configpaths.get('CONFIG_FILE')) + config_file = configpaths.get('CONFIG_FILE') if not config_file.exists(): return
===================================== gajim/common/storage/archive.py ===================================== @@ -27,7 +27,6 @@ import json import logging import sqlite3 as sqlite -from pathlib import Path from collections import namedtuple
from gajim.common import app @@ -93,7 +92,7 @@ class MessageArchiveStorage(SqliteStorage): def __init__(self): SqliteStorage.__init__(self, log, - Path(configpaths.get('LOG_DB')), + configpaths.get('LOG_DB'), ARCHIVE_SQL_STATEMENT)
self._jid_ids = {}
===================================== gajim/common/storage/cache.py ===================================== @@ -16,7 +16,6 @@ import time import sqlite3 import logging -from pathlib import Path from collections import namedtuple
from gajim.common import configpaths @@ -56,7 +55,7 @@ class CacheStorage(SqliteStorage): def __init__(self): SqliteStorage.__init__(self, log, - Path(configpaths.get('CACHE_DB')), + configpaths.get('CACHE_DB'), CACHE_SQL_STATEMENT)
self._entity_caps_cache = {}
===================================== gajim/common/types.py ===================================== @@ -22,6 +22,7 @@ from typing import Union from typing import TYPE_CHECKING
+from pathlib import Path import nbxmpp
from gajim.common.const import PathType, PathLocation @@ -53,7 +54,7 @@ PEPHandlersDict = Dict[str, List[PEPNotifyCallback]]
# Configpaths -PathTuple = Tuple[Optional[PathLocation], str, Optional[PathType]] +PathTuple = Tuple[Optional[PathLocation], Path, Optional[PathType]]
# Plugins PluginExtensionPoints = Dict[str, Tuple[Optional[Callable[..., None]],
===================================== gajim/gtk/account_wizard.py ===================================== @@ -13,7 +13,6 @@ # along with Gajim. If not, see http://www.gnu.org/licenses/.
import logging -from pathlib import Path
from gi.repository import Gdk from gi.repository import GLib @@ -559,7 +558,7 @@ def _on_signup(self, *args):
def _create_server_completion(self): # Parse servers.json - file_path = Path(configpaths.get('DATA')) / 'other' / 'servers.json' + file_path = configpaths.get('DATA') / 'other' / 'servers.json' self._servers = helpers.load_json(file_path, 'servers', [])
# Create a separate model for the address entry, because it will @@ -658,7 +657,7 @@ def focus(self):
def _create_server_completion(self): # Parse servers.json - file_path = Path(configpaths.get('DATA')) / 'other' / 'servers.json' + file_path = configpaths.get('DATA') / 'other' / 'servers.json' servers = helpers.load_json(file_path, 'servers', [])
# Create servers_model for comboboxes and entries
===================================== gajim/gtk/avatar.py ===================================== @@ -12,7 +12,6 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
-import os import logging import hashlib from math import pi @@ -295,7 +294,7 @@ def save_avatar(data): return None
sha = hashlib.sha1(data).hexdigest() - path = os.path.join(configpaths.get('AVATAR'), sha) + path = configpaths.get('AVATAR') / sha try: with open(path, 'wb') as output_file: output_file.write(data) @@ -306,8 +305,8 @@ def save_avatar(data):
@staticmethod def get_avatar_path(filename): - path = os.path.join(configpaths.get('AVATAR'), filename) - if not os.path.isfile(path): + path = configpaths.get('AVATAR') / filename + if not path.is_file(): return None return path
===================================== gajim/gtk/css_config.py ===================================== @@ -17,7 +17,6 @@
import math import logging -from pathlib import Path
from gi.repository import Gtk from gi.repository import Gdk @@ -148,7 +147,7 @@ def _load_css(self): CSSPriority.DEFAULT_THEME_DARK)
def _load_css_from_file(self, filename, priority): - path = Path(configpaths.get('STYLE')) / filename + path = configpaths.get('STYLE') / filename try: with open(path, "r") as file_: css = file_.read() @@ -180,7 +179,7 @@ def _pango_to_css_weight(number): return int(math.ceil(number / 100.0)) * 100
def _gather_available_themes(self): - files = Path(configpaths.get('MY_THEME')).iterdir() + files = configpaths.get('MY_THEME').iterdir() self.themes = [file.stem for file in files if file.suffix == '.css'] if 'default' in self.themes: # Ignore user created themes that are named 'default' @@ -191,8 +190,8 @@ def get_theme_path(self, theme, user=True): theme = 'default-dark'
if user: - return Path(configpaths.get('MY_THEME')) / f'{theme}.css' - return Path(configpaths.get('STYLE')) / f'{theme}.css' + return configpaths.get('MY_THEME') / f'{theme}.css' + return configpaths.get('STYLE') / f'{theme}.css'
def _determine_theme_path(self): # Gets the path of the currently active theme.
===================================== gajim/gtk/emoji_chooser.py ===================================== @@ -14,7 +14,6 @@
import logging import weakref -from pathlib import Path from collections import OrderedDict
from gi.repository import Gtk @@ -312,12 +311,12 @@ def _get_emoji_theme_path(theme): if theme == 'font': return 'font'
- base_path = Path(configpaths.get('EMOTICONS')) + base_path = configpaths.get('EMOTICONS') emoticons_data_path = base_path / theme / f'{theme}.png' if emoticons_data_path.exists(): return emoticons_data_path
- emoticons_user_path = Path(configpaths.get('MY_EMOTS')) / f'{theme}.png' + emoticons_user_path = configpaths.get('MY_EMOTS') / f'{theme}.png' if emoticons_user_path.exists(): return emoticons_user_path
===================================== gajim/gtk/exception.py ===================================== @@ -23,7 +23,6 @@ import threading import webbrowser import platform -from pathlib import Path from io import StringIO from urllib.parse import urlencode
@@ -69,7 +68,7 @@ def _hook(type_, value, tb):
class ExceptionDialog(): def __init__(self, type_, value, tb): - path = Path(configpaths.get('GUI')) / 'exception_dialog.ui' + path = configpaths.get('GUI') / 'exception_dialog.ui' self._ui = get_builder(path.resolve()) self._ui.connect_signals(self)
===================================== gajim/gtk/manage_sounds.py ===================================== @@ -107,7 +107,7 @@ def _on_cursor_changed(self, treeview): if path_to_snd_file is None: self._ui.filechooser.unselect_all() else: - self._ui.filechooser.set_filename(path_to_snd_file) + self._ui.filechooser.set_filename(str(path_to_snd_file))
def _on_file_set(self, button): model, iter_ = self._ui.sounds_treeview.get_selection().get_selected() @@ -118,7 +118,7 @@ def _on_file_set(self, button): path_to_snd_file = strip_soundfile_path(filename)
# set new path to sounds_model - model[iter_][2] = path_to_snd_file + model[iter_][2] = str(path_to_snd_file) # set the sound to enabled model[iter_][0] = True
===================================== gajim/gtk/util.py ===================================== @@ -28,7 +28,6 @@ import functools from importlib import import_module import xml.etree.ElementTree as ET -from pathlib import Path from functools import wraps from functools import lru_cache
@@ -177,7 +176,7 @@ def __init__(self, @staticmethod @functools.lru_cache(maxsize=None) def _load_string_from_filename(filename, gettext_): - file_path = str(Path(configpaths.get('GUI')) / filename) + file_path = str(configpaths.get('GUI') / filename)
if sys.platform == "win32": # This is a workaround for non working translation on Windows @@ -266,7 +265,7 @@ def get_icon_name(name: str,
def load_user_iconsets(): - iconsets_path = Path(configpaths.get('MY_ICONSETS')) + iconsets_path = configpaths.get('MY_ICONSETS') if not iconsets_path.exists(): return
@@ -282,7 +281,7 @@ def get_available_iconsets(): for iconset in GajimIconSet: iconsets.append(iconset.value)
- iconsets_path = Path(configpaths.get('MY_ICONSETS')) + iconsets_path = configpaths.get('MY_ICONSETS') if not iconsets_path.exists(): return iconsets
===================================== gajim/history_manager.py ===================================== @@ -30,7 +30,6 @@ import time import getopt import sqlite3 -from pathlib import Path from enum import IntEnum, unique
import gi @@ -109,7 +108,7 @@ class Column(IntEnum):
class HistoryManager: def __init__(self): - log_db_path = Path(configpaths.get('LOG_DB')) + log_db_path = configpaths.get('LOG_DB') if not log_db_path.exists(): ErrorDialog(_('Cannot find history logs database'), _('%s does not exist.') % log_db_path)
===================================== gajim/plugins/gajimplugin.py ===================================== @@ -210,8 +210,8 @@ def deactivate(self): class GajimPluginConfig(): def __init__(self, plugin): self.plugin = plugin - self.FILE_PATH = os.path.join( - configpaths.get('PLUGINS_CONFIG_DIR'), self.plugin.short_name) + self.FILE_PATH = (configpaths.get('PLUGINS_CONFIG_DIR') / + self.plugin.short_name) self.data = {}
def __getitem__(self, key): @@ -243,18 +243,18 @@ def items(self): return self.data.items()
def save(self): - fd = open(self.FILE_PATH, 'wb') - pickle.dump(self.data, fd) - fd.close() + with open(self.FILE_PATH, 'wb') as fd: + pickle.dump(self.data, fd)
def load(self): - if os.path.isfile(self.FILE_PATH): - fd = open(self.FILE_PATH, 'rb') + if not self.FILE_PATH.is_file(): + self.data = {} + self.save() + return + with open(self.FILE_PATH, 'rb') as fd: try: self.data = pickle.load(fd) - fd.close() except Exception: - fd.close() try: import shelve s = shelve.open(self.FILE_PATH) @@ -276,9 +276,6 @@ def load(self): os.rename(self.FILE_PATH, self.FILE_PATH + '.bak') self.data = {} self.save() - else: - self.data = {} - self.save()
class GajimPluginException(Exception):
===================================== gajim/plugins/helpers.py ===================================== @@ -27,7 +27,6 @@
import logging import functools -from pathlib import Path
from gajim.common import configpaths from gajim.plugins import plugins_i18n @@ -126,8 +125,8 @@ def get_builder(file_name: str, widgets: List[str] = None) -> Builder:
def is_shipped_plugin(path): - base = Path(configpaths.get('PLUGINS_BASE')) + base = configpaths.get('PLUGINS_BASE') if not base.exists(): return False - plugin_parent = Path(path).parent + plugin_parent = path.parent return base.samefile(plugin_parent)
===================================== gajim/plugins/pluginmanager.py ===================================== @@ -241,10 +241,10 @@ def update_plugins(self, replace=True, activate=False, plugin_name=None): dl_dir = configpaths.get('PLUGINS_DOWNLOAD') to_update = [plugin_name] if plugin_name else next(os.walk(dl_dir))[1] for directory in to_update: - src_dir = os.path.join(dl_dir, directory) - dst_dir = os.path.join(user_dir, directory) + src_dir = dl_dir / directory + dst_dir = user_dir / directory try: - if os.path.exists(dst_dir): + if dst_dir.exists(): if not replace: continue self.delete_plugin_files(dst_dir) @@ -701,7 +701,7 @@ def install_from_zip(self, zip_filename, overwrite=None): raise PluginsystemError(_('Archive is malformed'))
plugin_name = dirs[0] - user_dir = Path(configpaths.get('PLUGINS_USER')) + user_dir = configpaths.get('PLUGINS_USER') plugin_path = user_dir / plugin_name
if plugin_path.exists(): @@ -742,7 +742,7 @@ def uninstall_plugin(self, plugin): self.remove_plugin(plugin) self.delete_plugin_files(plugin.__path__) if not is_shipped_plugin(Path(plugin.__path__)): - path = Path(configpaths.get('PLUGINS_BASE')) / plugin.short_name + path = configpaths.get('PLUGINS_BASE') / plugin.short_name if path.exists(): self.delete_plugin_files(str(path))
===================================== gajim/plugins/plugins_i18n.py ===================================== @@ -22,7 +22,7 @@
DOMAIN = 'gajim_plugins' try: - plugin_user_dir = Path(configpaths.get('PLUGINS_USER')) + plugin_user_dir = configpaths.get('PLUGINS_USER') except KeyError: # This allows to import the module for tests print('No plugin translation path available')
===================================== gajim/vcard.py ===================================== @@ -25,7 +25,6 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
-import os import logging
from gi.repository import Gtk @@ -89,11 +88,11 @@ def __init__(self, contact, account, gc_contact=None): self.real_resource = contact.resource
puny_jid = helpers.sanitize_filename(contact.jid) - local_avatar_basepath = os.path.join(configpaths.get('AVATAR'), puny_jid) + \ - '_local' + local_avatar_basepath = configpaths.get('AVATAR') / (puny_jid + + '_local') for extension in ('.png', '.jpeg'): - local_avatar_path = local_avatar_basepath + extension - if os.path.isfile(local_avatar_path): + local_avatar_path = local_avatar_basepath.with_suffix(extension) + if local_avatar_path.is_file(): image = self.xml.get_object('custom_avatar_image') image.set_from_file(local_avatar_path) image.show()
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/b749795fdba9501172493c984b62b0c7c...