diff --git a/autobahn/xbr/_abi.py b/autobahn/xbr/_abi.py index 513a40f61..215fb5575 100644 --- a/autobahn/xbr/_abi.py +++ b/autobahn/xbr/_abi.py @@ -27,7 +27,13 @@ import os import json import binascii -import pkg_resources +import sys + +if sys.version_info < (3, 10): + import importlib_resources as resources +else: + from importlib import resources + os.environ['ETH_HASH_BACKEND'] = 'pycryptodome' @@ -128,21 +134,21 @@ def _load_json(contract_name): - fn = pkg_resources.resource_filename('xbr', 'abi/{}.json'.format(contract_name)) - with open(fn) as f: - data = json.loads(f.read()) + fn = resources.files('xbr.abi') / f'{contract_name}.json' + text = fn.read_text() + data = json.loads(text) return data # # XBR contract ABI file names # -XBR_TOKEN_FN = pkg_resources.resource_filename('xbr', 'abi/XBRToken.json') -XBR_NETWORK_FN = pkg_resources.resource_filename('xbr', 'abi/XBRNetwork.json') -XBR_DOMAIN_FN = pkg_resources.resource_filename('xbr', 'abi/XBRDomain.json') -XBR_CATALOG_FN = pkg_resources.resource_filename('xbr', 'abi/XBRCatalog.json') -XBR_MARKET_FN = pkg_resources.resource_filename('xbr', 'abi/XBRMarket.json') -XBR_CHANNEL_FN = pkg_resources.resource_filename('xbr', 'abi/XBRChannel.json') +XBR_TOKEN_FN = str(resources.files('xbr.abi') / 'XBRToken.json') +XBR_NETWORK_FN = str(resources.files('xbr.abi') / 'XBRNetwork.json') +XBR_DOMAIN_FN = str(resources.files('xbr.abi') / 'XBRDomain.json') +XBR_CATALOG_FN = str(resources.files('xbr.abi') / 'XBRCatalog.json') +XBR_MARKET_FN = str(resources.files('xbr.abi') / 'XBRMarket.json') +XBR_CHANNEL_FN = str(resources.files('xbr.abi') / 'XBRChannel.json') # diff --git a/autobahn/xbr/_cli.py b/autobahn/xbr/_cli.py index 0fcb86930..495044bad 100644 --- a/autobahn/xbr/_cli.py +++ b/autobahn/xbr/_cli.py @@ -26,7 +26,10 @@ import os import sys -import pkg_resources +if sys.version_info < (3, 10): + import importlib_resources as resources +else: + from importlib import resources from jinja2 import Environment, FileSystemLoader @@ -1026,7 +1029,7 @@ def _main(): print(repo.summary(keys=True)) # folder with jinja2 templates for python code sections - templates = pkg_resources.resource_filename('autobahn', 'xbr/templates') + templates = resources.files('autobahn.xbr') / 'templates' # jinja2 template engine loader and environment loader = FileSystemLoader(templates, encoding='utf-8', followlinks=False) diff --git a/autobahn/xbr/_gui.py b/autobahn/xbr/_gui.py index 6ca565bda..10fd0dd42 100644 --- a/autobahn/xbr/_gui.py +++ b/autobahn/xbr/_gui.py @@ -29,10 +29,15 @@ import uuid import binascii import random -import pkg_resources from pprint import pprint from time import time_ns +import sys +if sys.version_info < (3, 10): + import importlib_resources as resources +else: + from importlib import resources + import gi gi.require_version("Gtk", "3.0") @@ -65,7 +70,7 @@ from autobahn.xbr._cli import Client from autobahn.xbr._config import UserConfig, Profile -LOGO_RESOURCE = pkg_resources.resource_filename('autobahn', 'asset/xbr_gray.svg') +LOGO_RESOURCE = str(resources.files('autobahn.asset') / 'xbr_gray.svg') print(LOGO_RESOURCE, os.path.isfile(LOGO_RESOURCE)) diff --git a/autobahn/xbr/test/test_xbr_schema_demo.py b/autobahn/xbr/test/test_xbr_schema_demo.py index 13a9419f3..17f5a311c 100644 --- a/autobahn/xbr/test/test_xbr_schema_demo.py +++ b/autobahn/xbr/test/test_xbr_schema_demo.py @@ -1,6 +1,11 @@ import os import copy -import pkg_resources +import sys +if sys.version_info < (3, 10): + import importlib_resources as resources +else: + from importlib import resources + from random import randint, random import txaio from unittest import skipIf @@ -31,7 +36,7 @@ def setUp(self): self.repo = FbsRepository('autobahn') self.archives = [] for fbs_file in ['demo.bfbs', 'wamp-control.bfbs']: - archive = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/{}'.format(fbs_file)) + archive = str(resources.files('autobahn.xbr.test.catalog.schema') / fbs_file) self.repo.load(archive) self.archives.append(archive) diff --git a/autobahn/xbr/test/test_xbr_schema_wamp.py b/autobahn/xbr/test/test_xbr_schema_wamp.py index cf15b5040..8d69a65fb 100644 --- a/autobahn/xbr/test/test_xbr_schema_wamp.py +++ b/autobahn/xbr/test/test_xbr_schema_wamp.py @@ -1,5 +1,9 @@ import os -import pkg_resources +import sys +if sys.version_info < (3, 10): + import importlib_resources as resources +else: + from importlib import resources from binascii import a2b_hex import txaio from unittest import skipIf @@ -78,7 +82,7 @@ def setUp(self): self.repo = FbsRepository('autobahn') self.archives = [] for fbs_file in ['wamp.bfbs', 'testsvc1.bfbs']: - archive = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/{}'.format(fbs_file)) + archive = str(resources.files('autobahn.xbr.test.catalog.schema') / fbs_file) self.repo.load(archive) self.archives.append(archive) @@ -99,7 +103,7 @@ def test_create_from_archive(self): self.assertIsInstance(self.repo.services['testsvc1.ITestService1'], FbsService) def test_loaded_schema(self): - schema_fn = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/testsvc1.bfbs') + schema_fn = str(resources.files('autobahn.xbr.test.catalog.schema') / 'testsvc1.bfbs') # get reflection schema loaded schema: FbsSchema = self.repo.schemas[schema_fn] diff --git a/autobahn/xbr/test/test_xbr_schema_wamp_control.py b/autobahn/xbr/test/test_xbr_schema_wamp_control.py index 948b503bb..d42cb9c5a 100644 --- a/autobahn/xbr/test/test_xbr_schema_wamp_control.py +++ b/autobahn/xbr/test/test_xbr_schema_wamp_control.py @@ -1,6 +1,10 @@ import os import copy -import pkg_resources +import sys +if sys.version_info < (3, 10): + import importlib_resources as resources +else: + from importlib import resources import txaio from unittest import skipIf @@ -30,7 +34,7 @@ def setUp(self): self.repo = FbsRepository('autobahn') self.archives = [] for fbs_file in ['wamp-control.bfbs']: - archive = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/{}'.format(fbs_file)) + archive = str(resources.files('autobahn.xbr.test.catalog.schema') / fbs_file) self.repo.load(archive) self.archives.append(archive) diff --git a/autobahn/xbr/test/test_xbr_secmod.py b/autobahn/xbr/test/test_xbr_secmod.py index 0676e3804..00de2cb71 100644 --- a/autobahn/xbr/test/test_xbr_secmod.py +++ b/autobahn/xbr/test/test_xbr_secmod.py @@ -26,7 +26,10 @@ import os import sys -import pkg_resources +if sys.version_info < (3, 10): + import importlib_resources as resources +else: + from importlib import resources from random import randint, random from binascii import a2b_hex from typing import List @@ -418,7 +421,7 @@ def test_secmod_from_seedphrase(self): @inlineCallbacks def test_secmod_from_config(self): - config = pkg_resources.resource_filename('autobahn', 'xbr/test/profile/config.ini') + config = str(resources.files('autobahn.xbr.test.profile') / 'config.ini') sm = SecurityModuleMemory.from_config(config) yield sm.open() @@ -439,7 +442,7 @@ def test_secmod_from_config(self): @inlineCallbacks def test_secmod_from_keyfile(self): - keyfile = pkg_resources.resource_filename('autobahn', 'xbr/test/profile/default.priv') + keyfile = str(resources.files('autobahn.xbr.test.profile') / 'default.priv') sm = SecurityModuleMemory.from_keyfile(keyfile) yield sm.open() diff --git a/docker/print-versions.py b/docker/print-versions.py index 9ae0262b3..d0d5c10ff 100644 --- a/docker/print-versions.py +++ b/docker/print-versions.py @@ -2,7 +2,7 @@ import sys import platform import importlib -import pkg_resources +import importlib.metadata import txaio txaio.use_twisted() # noqa @@ -28,7 +28,7 @@ def _get_version(name_or_module): v = name_or_module.version else: try: - v = pkg_resources.get_distribution(name_or_module.__name__).version + v = importlib.metadata.version(name_or_module.__name__) except: # eg flatbuffers when run from single file EXE (pyinstaller): https://github.com/google/flatbuffers/issues/5299 v = '?.?.?' diff --git a/docs/xbr.rst b/docs/xbr.rst index 72a4d4849..9564bf09f 100644 --- a/docs/xbr.rst +++ b/docs/xbr.rst @@ -95,13 +95,13 @@ To directly use the embedded ABI files: .. code-block:: python import json - import pkg_resources + import importlib.resources from pprint import pprint - with open(pkg_resources.resource_filename('xbr', 'contracts/XBRToken.json')) as f: - data = json.loads(f.read()) - abi = data['abi'] - pprint(abi) + text = (importlib.resources.files('xbr.abi') / 'XBRToken.json').read_text() + data = json.loads(text) + abi = data['abi'] + pprint(abi) Data stored on-chain diff --git a/setup.py b/setup.py index c2a463295..dd74f9a56 100644 --- a/setup.py +++ b/setup.py @@ -235,7 +235,8 @@ 'txaio>=21.2.1', # MIT license (https://github.com/crossbario/txaio) 'cryptography>=3.4.6', # BSD *or* Apache license (https://github.com/pyca/cryptography) 'hyperlink>=21.0.0', # MIT license (https://github.com/python-hyper/hyperlink) - 'setuptools', # MIT license (https://github.com/pypa/setuptools) + 'importlib.resources>=5.0.0 ; python_version < "3.10"', # Apache license (https://github.com/python/importlib_resources/blob/main/LICENSE) + ], extras_require={ 'all': extras_require_all,