From 64c1a09023298e75eed0b2a0e736d1422f7f46b7 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 10 Aug 2013 00:24:27 +0200 Subject: Fix #2337 (search the themes the same way than plugins) - Load the themes from: 1 - The sources found in the directory ../data/themes/ (if it exists) 2 - The user-defined dir (or ~/.local/blah) 3 - The poezio_themes package if found - Also fix some potential issues with the plugins importer --- setup.py | 4 +-- src/core.py | 1 + src/plugin_manager.py | 10 +++---- src/theming.py | 82 +++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 76 insertions(+), 21 deletions(-) diff --git a/setup.py b/setup.py index e57f9a70..a07b74c9 100644 --- a/setup.py +++ b/setup.py @@ -40,8 +40,8 @@ setup(name="poezio", 'Programming Language :: Python :: 3', ], keywords = ['jabber', 'xmpp', 'client', 'chat', 'im', 'console'], - packages = ['poezio', 'poezio_plugins', 'poezio_plugins.gpg'], - package_dir = {'poezio': 'src', 'poezio_plugins': 'plugins'}, + packages = ['poezio', 'poezio_plugins', 'poezio_plugins.gpg', 'poezio_themes'], + package_dir = {'poezio': 'src', 'poezio_plugins': 'plugins', 'poezio_themes': 'data/themes'}, package_data = {'poezio': ['default_config.cfg']}, scripts = ['scripts/poezio'], data_files = [('share/poezio/themes/', ['data/themes/dark.py']), diff --git a/src/core.py b/src/core.py index 4c1dd032..20e09b39 100644 --- a/src/core.py +++ b/src/core.py @@ -301,6 +301,7 @@ class Core(object): self.add_configuration_handler("plugins_conf_dir", self.on_plugins_conf_dir_config_change) self.add_configuration_handler("connection_timeout_delay", self.xmpp.set_keepalive_values) self.add_configuration_handler("connection_check_interval", self.xmpp.set_keepalive_values) + self.add_configuration_handler("themes_dir", theming.update_themes_dir) self.add_configuration_handler("", self.on_any_config_change) def on_any_config_change(self, option, value): diff --git a/src/plugin_manager.py b/src/plugin_manager.py index 4f728938..bb139dad 100644 --- a/src/plugin_manager.py +++ b/src/plugin_manager.py @@ -62,8 +62,6 @@ else: if poezio_plugins.__path__: load_path.append(poezio_plugins.__path__[0]) -sys.path.extend(load_path) - if version_info[1] >= 3: # 3.3 & > from importlib import machinery finder = machinery.PathFinder() @@ -111,7 +109,7 @@ class PluginManager(object): imp.acquire_lock() module = imp.load_module(name, file, filename, info) else: # 3.3 & > - loader = finder.find_module(name) + loader = finder.find_module(name, load_path) if not loader: self.core.information('Could not find plugin') return @@ -324,9 +322,9 @@ class PluginManager(object): def on_plugins_dir_change(self, new_value): global plugins_dir - if plugins_dir in sys.path: - sys.path.remove(plugins_dir) - sys.path.append(new_value) + if plugins_dir in load_path: + load_path.remove(plugins_dir) + load_path.insert(0, new_value) plugins_dir = new_value def on_plugins_conf_dir_change(self, new_value): diff --git a/src/theming.py b/src/theming.py index 975da92a..dd42342c 100644 --- a/src/theming.py +++ b/src/theming.py @@ -67,6 +67,12 @@ from config import config import curses import imp import os +from os import path +from sys import version_info + +if version_info[1] >= 3: + from importlib import machinery + finder = machinery.PathFinder() class Theme(object): """ @@ -319,6 +325,8 @@ table_256_to_16 = [ 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15 ] +load_path = [] + def color_256_to_16(color): if color == -1: return color @@ -384,29 +392,77 @@ def get_theme(): """ return theme -def reload_theme(): - themes_dir = config.get('themes_dir', '') - themes_dir = themes_dir or\ - os.path.join(os.environ.get('XDG_DATA_HOME') or\ - os.path.join(os.environ.get('HOME'), '.local', 'share'), - 'poezio', 'themes') - themes_dir = os.path.expanduser(themes_dir) +def update_themes_dir(option=None, value=None): + global load_path + load_path = [] + + # import from the git sources + default_dir = path.join( + path.dirname(path.dirname(__file__)), + 'data/themes') + if path.exists(default_dir): + load_path.append(default_dir) + + # import from the user-defined prefs + themes_dir = path.expanduser( + value or + config.get('themes_dir', '') or + path.join(os.environ.get('XDG_DATA_HOME') or + path.join(os.environ.get('HOME'), '.local', 'share'), + 'poezio', 'themes') + ) try: os.makedirs(themes_dir) - except OSError: + except OSError as e: + if e.errno != 17: + log.error('Unable to create the themes dir (%s)', themes_dir) + else: + load_path.append(themes_dir) + else: + load_path.append(themes_dir) + + # system-wide import + try: + import poezio_themes + except: pass + else: + load_path.append(poezio_themes.__path__[0]) + +def reload_theme(): theme_name = config.get('theme', 'default') global theme if theme_name == 'default' or not theme_name.strip(): theme = Theme() return + new_theme = None + exc = None try: - file_path = os.path.join(themes_dir, theme_name)+'.py' - log.debug('Theme file to load: %s' %(file_path,)) - new_theme = imp.load_source('theme', os.path.join(themes_dir, theme_name)+'.py') + if version_info[1] < 3: + file, filename, info = imp.find_module(theme_name, load_path) + imp.acquire_lock() + new_theme = imp.load_module(them_name, file, filename, info) + else: + loader = finder.find_module(theme_name, load_path) + if not loader: + return + new_theme = loader.load_module() except Exception as e: - return 'Failed to load theme: %s' % (e,) - theme = new_theme.theme + log.error('Failed to load the theme %s', theme_name, exc_info=True) + exc = e + finally: + if version_info[1] < 3: + imp.release_lock() + + if not new_theme: + return 'Failed to load theme: %s' % exc + + if hasattr(new_theme, 'theme'): + theme = new_theme.theme + else: + return 'No theme present in the theme file' + +update_themes_dir() if __name__ == '__main__': """ -- cgit v1.2.3