diff --git a/deluge/common.py b/deluge/common.py index ca6ccda17..35cab102d 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -207,15 +207,27 @@ def get_pixmap(fname): def resource_filename(module, path): - # While developing, if there's a second deluge package, installed globally - # and another in develop mode somewhere else, while pkg_resources.require('Deluge') - # returns the proper deluge instance, pkg_resources.resource_filename does - # not, it returns the first found on the python path, which is not good - # enough. - # This is a work-around that. - return pkg_resources.require('Deluge>=%s' % get_version())[0].get_resource_filename( - pkg_resources._manager, os.path.join(*(module.split('.') + [path])) - ) + """A workaround for pkg_resources.resource_filename finding wrong package when developing + + When developing and there is a second deluge package (installed globally) and + another in develop mode elsewhere. While pkg_resources.require('Deluge') + returns the proper deluge instance, resource_filename does not, it returns + the first found on the python path, which is not what is wanted. + + Also ensures the filename path is decoded from bytes. + + Args: + module (str): The module name in dotted notation. + path (str): The relative path to resource. + + Returns: + str: The absolute resource path. + + """ + filename = pkg_resources.require('Deluge>=%s' % get_version())[0].get_resource_filename( + pkg_resources._manager, os.path.join(*(module.split('.') + [path]))) + + return decode_bytes(filename) def open_file(path, timestamp=None): diff --git a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/common.py b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/common.py index 3e01f2f4c..43d2c34f2 100644 --- a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/common.py +++ b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/common.py @@ -14,10 +14,10 @@ from __future__ import unicode_literals -import os +from os.path import join -import pkg_resources +from deluge.common import resource_filename def get_resource(filename): - return pkg_resources.resource_filename('deluge.plugins.autoadd', os.path.join('data', filename)) + return resource_filename('deluge.plugins.autoadd', join('data', filename)) diff --git a/deluge/plugins/Blocklist/deluge/plugins/blocklist/common.py b/deluge/plugins/Blocklist/deluge/plugins/blocklist/common.py index 5e0a4989f..80fd67acc 100644 --- a/deluge/plugins/Blocklist/deluge/plugins/blocklist/common.py +++ b/deluge/plugins/Blocklist/deluge/plugins/blocklist/common.py @@ -9,16 +9,15 @@ from __future__ import unicode_literals -import os.path from functools import wraps +from os.path import join from sys import exc_info -import pkg_resources +from deluge.common import resource_filename def get_resource(filename): - return pkg_resources.resource_filename('deluge.plugins.blocklist', - os.path.join('data', filename)) + return resource_filename('deluge.plugins.blocklist', join('data', filename)) def raises_errors_as(error): diff --git a/deluge/plugins/Execute/deluge/plugins/execute/common.py b/deluge/plugins/Execute/deluge/plugins/execute/common.py index 489f31291..5adde6ef1 100644 --- a/deluge/plugins/Execute/deluge/plugins/execute/common.py +++ b/deluge/plugins/Execute/deluge/plugins/execute/common.py @@ -9,11 +9,10 @@ from __future__ import unicode_literals -import os.path +from os.path import join -import pkg_resources +from deluge.common import resource_filename def get_resource(filename): - return pkg_resources.resource_filename('deluge.plugins.execute', - os.path.join('data', filename)) + return resource_filename('deluge.plugins.execute', join('data', filename)) diff --git a/deluge/plugins/Extractor/deluge/plugins/extractor/common.py b/deluge/plugins/Extractor/deluge/plugins/extractor/common.py index 279893305..8353896d2 100644 --- a/deluge/plugins/Extractor/deluge/plugins/extractor/common.py +++ b/deluge/plugins/Extractor/deluge/plugins/extractor/common.py @@ -9,11 +9,10 @@ from __future__ import unicode_literals -import os +from os.path import join -import pkg_resources +from deluge.common import resource_filename def get_resource(filename): - return pkg_resources.resource_filename('deluge.plugins.extractor', - os.path.join('data', filename)) + return resource_filename('deluge.plugins.extractor', join('data', filename)) diff --git a/deluge/plugins/Label/deluge/plugins/label/common.py b/deluge/plugins/Label/deluge/plugins/label/common.py new file mode 100644 index 000000000..1507b8bad --- /dev/null +++ b/deluge/plugins/Label/deluge/plugins/label/common.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2009 Andrew Resch +# +# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with +# the additional special exception to link portions of this program with the OpenSSL library. +# See LICENSE for more details. +# + +from __future__ import unicode_literals + +from os.path import join + +from deluge.common import resource_filename + + +def get_resource(filename): + return resource_filename('deluge.plugins.label', join('data', filename)) diff --git a/deluge/plugins/Label/deluge/plugins/label/gtkui/label_config.py b/deluge/plugins/Label/deluge/plugins/label/gtkui/label_config.py index e93ac07a0..78c04d4f7 100644 --- a/deluge/plugins/Label/deluge/plugins/label/gtkui/label_config.py +++ b/deluge/plugins/Label/deluge/plugins/label/gtkui/label_config.py @@ -10,13 +10,13 @@ from __future__ import unicode_literals import logging -import os -import pkg_resources # access plugin egg from gtk import Builder from deluge.ui.client import client +from .common import get_resource + log = logging.getLogger(__name__) @@ -31,7 +31,7 @@ class LabelConfig(object): def load(self): log.debug('Adding Label Preferences page') builder = Builder() - builder.add_from_file(self.get_resource('label_pref.ui')) + builder.add_from_file(get_resource('label_pref.ui')) self.plugin.add_preferences_page(_('Label'), builder.get_object('label_prefs_box')) self.plugin.register_hook('on_show_prefs', self.load_settings) @@ -44,11 +44,6 @@ class LabelConfig(object): self.plugin.deregister_hook('on_apply_prefs', self.on_apply_prefs) self.plugin.deregister_hook('on_show_prefs', self.load_settings) - def get_resource(self, filename): - return pkg_resources.resource_filename( - 'deluge.plugins.label', os.path.join('data', filename) - ) - def load_settings(self, widget=None, data=None): client.label.get_config().addCallback(self.cb_global_options) diff --git a/deluge/plugins/Label/deluge/plugins/label/gtkui/sidebar_menu.py b/deluge/plugins/Label/deluge/plugins/label/gtkui/sidebar_menu.py index e3cf8bd70..30d33aa52 100644 --- a/deluge/plugins/Label/deluge/plugins/label/gtkui/sidebar_menu.py +++ b/deluge/plugins/Label/deluge/plugins/label/gtkui/sidebar_menu.py @@ -17,20 +17,13 @@ import gtk import deluge.component as component from deluge.ui.client import client +from .common import get_resource + log = logging.getLogger(__name__) NO_LABEL = 'No Label' -# helpers: -def get_resource(filename): - import pkg_resources - import os - return pkg_resources.resource_filename( - 'deluge.plugins.label', os.path.join('data', filename) - ) - - # menu class LabelSidebarMenu(object): def __init__(self): diff --git a/deluge/plugins/Label/deluge/plugins/label/webui.py b/deluge/plugins/Label/deluge/plugins/label/webui.py index dec9ab428..58c38e941 100644 --- a/deluge/plugins/Label/deluge/plugins/label/webui.py +++ b/deluge/plugins/Label/deluge/plugins/label/webui.py @@ -14,21 +14,14 @@ from __future__ import unicode_literals import logging -import os - -import pkg_resources from deluge.plugins.pluginbase import WebPluginBase +from .common import get_resource + log = logging.getLogger(__name__) -def get_resource(filename): - return pkg_resources.resource_filename('deluge.plugins.label', - os.path.join('data', filename)) - - class WebUI(WebPluginBase): - scripts = [get_resource('label.js')] debug_scripts = scripts diff --git a/deluge/plugins/Notifications/deluge/plugins/notifications/common.py b/deluge/plugins/Notifications/deluge/plugins/notifications/common.py index 9e2c19235..64e3be290 100644 --- a/deluge/plugins/Notifications/deluge/plugins/notifications/common.py +++ b/deluge/plugins/Notifications/deluge/plugins/notifications/common.py @@ -15,19 +15,19 @@ from __future__ import unicode_literals import logging +from os.path import join from twisted.internet import defer from deluge import component +from deluge.common import resource_filename from deluge.event import known_events log = logging.getLogger(__name__) def get_resource(filename): - import os - import pkg_resources - return pkg_resources.resource_filename('deluge.plugins.notifications', os.path.join('data', filename)) + return resource_filename('deluge.plugins.notifications', join('data', filename)) class CustomNotifications(object): diff --git a/deluge/plugins/Scheduler/deluge/plugins/scheduler/common.py b/deluge/plugins/Scheduler/deluge/plugins/scheduler/common.py index decf23a99..f0d9200a1 100644 --- a/deluge/plugins/Scheduler/deluge/plugins/scheduler/common.py +++ b/deluge/plugins/Scheduler/deluge/plugins/scheduler/common.py @@ -11,11 +11,12 @@ # See LICENSE for more details. # - from __future__ import unicode_literals +from os.path import join + +from deluge.common import resource_filename + def get_resource(filename): - import os - import pkg_resources - return pkg_resources.resource_filename('deluge.plugins.scheduler', os.path.join('data', filename)) + return resource_filename('deluge.plugins.scheduler', join('data', filename)) diff --git a/deluge/plugins/Stats/deluge/plugins/stats/common.py b/deluge/plugins/Stats/deluge/plugins/stats/common.py index 33c1348fb..112731363 100644 --- a/deluge/plugins/Stats/deluge/plugins/stats/common.py +++ b/deluge/plugins/Stats/deluge/plugins/stats/common.py @@ -9,10 +9,10 @@ from __future__ import unicode_literals -import os.path +from os.path import join -import pkg_resources +from deluge.common import resource_filename def get_resource(filename): - return pkg_resources.resource_filename('deluge.plugins.stats', os.path.join('data', filename)) + return resource_filename('deluge.plugins.stats', join('data', filename)) diff --git a/deluge/plugins/Toggle/deluge/plugins/toggle/common.py b/deluge/plugins/Toggle/deluge/plugins/toggle/common.py index c3e66edbf..a59472b36 100644 --- a/deluge/plugins/Toggle/deluge/plugins/toggle/common.py +++ b/deluge/plugins/Toggle/deluge/plugins/toggle/common.py @@ -12,12 +12,12 @@ # See LICENSE for more details. # - from __future__ import unicode_literals +from os.path import join + +from deluge.common import resource_filename + def get_resource(filename): - import os.path - import pkg_resources - return pkg_resources.resource_filename('deluge.plugins.toggle', - os.path.join('data', filename)) + return resource_filename('deluge.plugins.toggle', join('data', filename)) diff --git a/deluge/plugins/WebUi/deluge/plugins/webui/common.py b/deluge/plugins/WebUi/deluge/plugins/webui/common.py index 97cbe651e..f6a408559 100644 --- a/deluge/plugins/WebUi/deluge/plugins/webui/common.py +++ b/deluge/plugins/WebUi/deluge/plugins/webui/common.py @@ -11,12 +11,12 @@ # See LICENSE for more details. # - from __future__ import unicode_literals +from os.path import join + +from deluge.common import resource_filename + def get_resource(filename): - import os.path - import pkg_resources - return pkg_resources.resource_filename('deluge.plugins.webui', - os.path.join('data', filename)) + return resource_filename('deluge.plugins.webui', join('data', filename)) diff --git a/deluge/scripts/create_plugin.py b/deluge/scripts/create_plugin.py index ed49c3dff..5ef6c2a8b 100644 --- a/deluge/scripts/create_plugin.py +++ b/deluge/scripts/create_plugin.py @@ -208,11 +208,14 @@ setup( COMMON = """ +from __future__ import unicode_literals + +from os.path import join + +from deluge.common import resource_filename + def get_resource(filename): - import pkg_resources - import os - return pkg_resources.resource_filename('deluge.plugins.%(safe_name)s', - os.path.join('data', filename)) + return resource_filename('deluge.plugins.%(safe_name)s', join('data', filename)) """ GTKUI = """ @@ -223,7 +226,7 @@ from deluge.ui.client import client from deluge.plugins.pluginbase import GtkPluginBase import deluge.component as component -from common import get_resource +from .common import get_resource log = logging.getLogger(__name__) @@ -290,7 +293,7 @@ import logging from deluge.ui.client import client from deluge.plugins.pluginbase import WebPluginBase -from common import get_resource +from .common import get_resource log = logging.getLogger(__name__)