#3252: Replace deprecated pkg_resources
-------------------+---------------------------
 Reporter:  Cas    |      Owner:
     Type:  bug    |     Status:  new
 Priority:  minor  |  Milestone:  2.x
Component:  Core   |    Version:  develop (git)
 Keywords:         |
-------------------+---------------------------
 Going forward in Python 3 the pkg_resources api is rather slow and has
 been marked deprecated.

 To find and load resources using pkgutil or new importlib.resources is the
 solution. There are however a few issues that don't make them direct drop
 in replacements.

 Firstly to get the resource filename pkgutil can only get_data, file
 contents, so to get the file path, a custom method will need to be
 implemented like this:

 {{{
 def get_filename(package, resource):
     """Rewrite of pkgutil.get_data() that return the file path.

     From: https://github.com/DLR-RY/pando-core/blob/master/pando/pkg.py
     """
     loader = get_loader(package)
     if loader is None or not hasattr(loader, 'get_data'):
         return None
     mod = sys.modules.get(package) or loader.load_module(package)
     if mod is None or not hasattr(mod, '__file__'):
         return None

     # Modify the resource name to be compatible with the loader.get_data
     # signature - an os.path format "filename" starting with the dirname
 of
     # the package's __file__
     parts = resource.split('/')
     parts.insert(0, os.path.dirname(mod.__file__))
     resource_name = os.path.normpath(os.path.join(*parts))

     return resource_name
 }}}

 And for importlib.resources there is an equivalent on the [https
 ://importlib-resources.readthedocs.io/en/latest/migration.html#pkg-
 resources-resource-filename migration guide].

 The only issue is that neither work perfectly with zipped eggs, like
 pkg_resources, which uses a python egg cache. So will need to workaround
 this issue by trying not to pass around resource filenames in the plugins.

 I have checked the code of the plugins and most GTK methods will accept
 the resource data but the WebUI requires a filepath for scripts so somehow
 need to pass either the data or package details for the web server to load
 the data without needing the absolute path.

 A few notes about importlib.resources, it is only built-in to Python 3.7
 so harder to package and it requires all data directories to be packages
 which requires adding __init__.py files to these locations.

--
Ticket URL: <https://dev.deluge-torrent.org/ticket/3252>
Deluge <http://deluge-torrent.org/>
Deluge Project

-- 
You received this message because you are subscribed to the Google Groups 
"Deluge Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/deluge-dev.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/deluge-dev/041.d4e040c3ad4a025cd95305ad946716ac%40deluge-torrent.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to