Philipp Hörist pushed to branch master at gajim / gajim
Commits: e8cf6a27 by lovetox at 2020-08-22T22:34:14+02:00 Port mutiple modules to pathlib
- - - - -
7 changed files:
- gajim/common/helpers.py - gajim/gtk/account_wizard.py - gajim/gtk/emoji_chooser.py - gajim/gtk/util.py - gajim/gui_interface.py - gajim/history_manager.py - gajim/plugins/plugins_i18n.py
Changes:
===================================== gajim/common/helpers.py ===================================== @@ -913,7 +913,7 @@ def get_sync_threshold(jid, archive_info):
def load_json(path, key=None, default=None): try: - with open(path, 'r') as file: + with path.open('r') as file: json_dict = json.loads(file.read()) except Exception: log.exception('Parsing error')
===================================== gajim/gtk/account_wizard.py ===================================== @@ -12,8 +12,8 @@ # 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 pathlib import Path
from gi.repository import Gdk from gi.repository import GLib @@ -553,9 +553,8 @@ class Login(Page):
def _create_server_completion(self): # Parse servers.json - server_file_path = os.path.join( - configpaths.get('DATA'), 'other', 'servers.json') - self._servers = helpers.load_json(server_file_path, 'servers', []) + file_path = 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 # be updated with our localpart@ @@ -631,11 +630,6 @@ class Signup(Page): self.complete = False self.title = _('Create New Account')
- # Parse servers.json - server_file_path = os.path.join( - configpaths.get('DATA'), 'other', 'servers.json') - self._servers = helpers.load_json(server_file_path, 'servers', []) - self._ui = get_builder('account_wizard.ui') self._ui.server_comboboxtext_sign_up_entry.set_activates_default(True) self._create_server_completion() @@ -658,13 +652,12 @@ class Signup(Page):
def _create_server_completion(self): # Parse servers.json - server_file_path = os.path.join( - configpaths.get('DATA'), 'other', 'servers.json') - self._servers = helpers.load_json(server_file_path, 'servers', []) + file_path = Path(configpaths.get('DATA')) / 'other' / 'servers.json' + servers = helpers.load_json(file_path, 'servers', [])
# Create servers_model for comboboxes and entries servers_model = Gtk.ListStore(str) - for server in self._servers: + for server in servers: servers_model.append((server,))
# Sign up combobox and entry
===================================== gajim/gtk/emoji_chooser.py ===================================== @@ -12,10 +12,10 @@ # 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 sys import logging import weakref +from pathlib import Path from collections import OrderedDict
from gi.repository import Gtk @@ -317,15 +317,14 @@ class EmojiChooser(Gtk.Popover): def _get_emoji_theme_path(theme): if theme == 'font': return 'font' - emoticons_data_path = os.path.join(configpaths.get('EMOTICONS'), - theme, - '%s.png' % theme) - if os.path.exists(emoticons_data_path): + + base_path = 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 = os.path.join(configpaths.get('MY_EMOTS'), - '%s.png' % theme) - if os.path.exists(emoticons_user_path): + emoticons_user_path = Path(configpaths.get('MY_EMOTS')) / f'{theme}.png' + if emoticons_user_path.exists(): return emoticons_user_path
log.warning('Could not find emoji theme: %s', theme) @@ -385,7 +384,7 @@ class EmojiChooser(Gtk.Popover): @staticmethod def _get_next_pixbuf(path): src_x = src_y = cur_column = 0 - atlas = GdkPixbuf.Pixbuf.new_from_file(path) + atlas = GdkPixbuf.Pixbuf.new_from_file(str(path))
while True: src_x = cur_column * Emoji.PARSE_WIDTH
===================================== gajim/gtk/util.py ===================================== @@ -20,7 +20,6 @@ from typing import List from typing import Tuple from typing import Optional
-import os import sys import logging import textwrap @@ -60,7 +59,7 @@ from gajim.gtk.const import WINDOW_MODULES
_icon_theme = Gtk.IconTheme.get_default() if _icon_theme is not None: - _icon_theme.append_search_path(configpaths.get('ICONS')) + _icon_theme.append_search_path(str(configpaths.get('ICONS')))
log = logging.getLogger('gajim.gtk.util')
@@ -164,7 +163,7 @@ class Builder: if gettext_ is None: gettext_ = _
- file_path = os.path.join(configpaths.get('GUI'), filename) + file_path = str(Path(configpaths.get('GUI')) / filename)
if sys.platform == "win32": # This is a workaround for non working translation on Windows
===================================== gajim/gui_interface.py ===================================== @@ -115,7 +115,7 @@ from gajim.gtk.util import get_color_for_account from gajim.gtk.const import ControlType
-parser = optparser.OptionsParser(configpaths.get('CONFIG_FILE')) +parser = optparser.OptionsParser(str(configpaths.get('CONFIG_FILE'))) log = logging.getLogger('gajim.interface')
class Interface:
===================================== gajim/history_manager.py ===================================== @@ -30,6 +30,7 @@ import sys import time import getopt import sqlite3 +from pathlib import Path from enum import IntEnum, unique
import gi @@ -108,8 +109,8 @@ class Column(IntEnum):
class HistoryManager: def __init__(self): - log_db_path = configpaths.get('LOG_DB') - if not os.path.exists(log_db_path): + log_db_path = Path(configpaths.get('LOG_DB')) + if not log_db_path.exists(): ErrorDialog(_('Cannot find history logs database'), _('%s does not exist.') % log_db_path) sys.exit()
===================================== gajim/plugins/plugins_i18n.py ===================================== @@ -14,21 +14,23 @@ # 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 locale import gettext +from pathlib import Path
from gajim.common import configpaths
DOMAIN = 'gajim_plugins' try: - plugin_user_dir = configpaths.get('PLUGINS_USER') + plugin_user_dir = Path(configpaths.get('PLUGINS_USER')) except KeyError: # This allows to import the module for tests print('No plugin translation path available') - plugin_user_dir = '' + plugin_user_dir = Path.cwd()
-plugins_locale_dir = os.path.join(plugin_user_dir, 'locale') + +# python 3.7 gettext module does not support Path objects +plugins_locale_dir = str(plugin_user_dir / 'locale')
try: t = gettext.translation(DOMAIN, plugins_locale_dir)
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/e8cf6a27e80fc9260b38638cbff16d70e...