[#2341] Use common.resource_filename in plugins

This commit is contained in:
Calum Lind 2017-03-17 11:57:48 +00:00
parent 8326206f87
commit bdb3b509ad
15 changed files with 88 additions and 76 deletions

View File

@ -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):

View File

@ -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))

View File

@ -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):

View File

@ -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))

View File

@ -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))

View File

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com>
#
# 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))

View File

@ -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)

View File

@ -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):

View File

@ -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

View File

@ -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):

View File

@ -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))

View File

@ -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))

View File

@ -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))

View File

@ -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))

View File

@ -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__)