Skip to content

Commit 566d290

Browse files
committed
remove setuptools dependency
replace deprecated pkg_resources methods with importlib.resources and importlib.metadata
1 parent e84c6ee commit 566d290

10 files changed

+63
-32
lines changed

autobahn/xbr/_abi.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727
import os
2828
import json
2929
import binascii
30-
import pkg_resources
30+
import sys
31+
32+
if sys.version_info < (3, 10):
33+
import importlib_resources as resources
34+
else:
35+
from importlib import resources
36+
3137

3238
os.environ['ETH_HASH_BACKEND'] = 'pycryptodome'
3339

@@ -128,21 +134,21 @@
128134

129135

130136
def _load_json(contract_name):
131-
fn = pkg_resources.resource_filename('xbr', 'abi/{}.json'.format(contract_name))
132-
with open(fn) as f:
133-
data = json.loads(f.read())
137+
fn = resources.files('xbr.abi') / f'{contract_name}.json'
138+
text = fn.read_text()
139+
data = json.loads(text)
134140
return data
135141

136142

137143
#
138144
# XBR contract ABI file names
139145
#
140-
XBR_TOKEN_FN = pkg_resources.resource_filename('xbr', 'abi/XBRToken.json')
141-
XBR_NETWORK_FN = pkg_resources.resource_filename('xbr', 'abi/XBRNetwork.json')
142-
XBR_DOMAIN_FN = pkg_resources.resource_filename('xbr', 'abi/XBRDomain.json')
143-
XBR_CATALOG_FN = pkg_resources.resource_filename('xbr', 'abi/XBRCatalog.json')
144-
XBR_MARKET_FN = pkg_resources.resource_filename('xbr', 'abi/XBRMarket.json')
145-
XBR_CHANNEL_FN = pkg_resources.resource_filename('xbr', 'abi/XBRChannel.json')
146+
XBR_TOKEN_FN = str(resources.files('xbr.abi') / 'XBRToken.json')
147+
XBR_NETWORK_FN = str(resources.files('xbr.abi') / 'XBRNetwork.json')
148+
XBR_DOMAIN_FN = str(resources.files('xbr.abi') / 'XBRDomain.json')
149+
XBR_CATALOG_FN = str(resources.files('xbr.abi') / 'XBRCatalog.json')
150+
XBR_MARKET_FN = str(resources.files('xbr.abi') / 'XBRMarket.json')
151+
XBR_CHANNEL_FN = str(resources.files('xbr.abi') / 'XBRChannel.json')
146152

147153

148154
#

autobahn/xbr/_cli.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
import os
2828
import sys
29-
import pkg_resources
29+
if sys.version_info < (3, 10):
30+
import importlib_resources as resources
31+
else:
32+
from importlib import resources
3033

3134
from jinja2 import Environment, FileSystemLoader
3235

@@ -1026,7 +1029,7 @@ def _main():
10261029
print(repo.summary(keys=True))
10271030

10281031
# folder with jinja2 templates for python code sections
1029-
templates = pkg_resources.resource_filename('autobahn', 'xbr/templates')
1032+
templates = resources.files('autobahn.xbr') / 'templates'
10301033

10311034
# jinja2 template engine loader and environment
10321035
loader = FileSystemLoader(templates, encoding='utf-8', followlinks=False)

autobahn/xbr/_gui.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@
2929
import uuid
3030
import binascii
3131
import random
32-
import pkg_resources
3332
from pprint import pprint
3433
from time import time_ns
3534

35+
import sys
36+
if sys.version_info < (3, 10):
37+
import importlib_resources as resources
38+
else:
39+
from importlib import resources
40+
3641
import gi
3742

3843
gi.require_version("Gtk", "3.0")
@@ -65,7 +70,7 @@
6570
from autobahn.xbr._cli import Client
6671
from autobahn.xbr._config import UserConfig, Profile
6772

68-
LOGO_RESOURCE = pkg_resources.resource_filename('autobahn', 'asset/xbr_gray.svg')
73+
LOGO_RESOURCE = str(resources.files('autobahn.asset') / 'xbr_gray.svg')
6974
print(LOGO_RESOURCE, os.path.isfile(LOGO_RESOURCE))
7075

7176

autobahn/xbr/test/test_xbr_schema_demo.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import os
22
import copy
3-
import pkg_resources
3+
import sys
4+
if sys.version_info < (3, 10):
5+
import importlib_resources as resources
6+
else:
7+
from importlib import resources
8+
49
from random import randint, random
510
import txaio
611
from unittest import skipIf
@@ -31,7 +36,7 @@ def setUp(self):
3136
self.repo = FbsRepository('autobahn')
3237
self.archives = []
3338
for fbs_file in ['demo.bfbs', 'wamp-control.bfbs']:
34-
archive = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/{}'.format(fbs_file))
39+
archive = str(resources.files('autobahn.xbr.test.catalog.schema') / fbs_file)
3540
self.repo.load(archive)
3641
self.archives.append(archive)
3742

autobahn/xbr/test/test_xbr_schema_wamp.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import os
2-
import pkg_resources
2+
import sys
3+
if sys.version_info < (3, 10):
4+
import importlib_resources as resources
5+
else:
6+
from importlib import resources
37
from binascii import a2b_hex
48
import txaio
59
from unittest import skipIf
@@ -78,7 +82,7 @@ def setUp(self):
7882
self.repo = FbsRepository('autobahn')
7983
self.archives = []
8084
for fbs_file in ['wamp.bfbs', 'testsvc1.bfbs']:
81-
archive = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/{}'.format(fbs_file))
85+
archive = str(resources.files('autobahn.xbr.test.catalog.schema') / fbs_file)
8286
self.repo.load(archive)
8387
self.archives.append(archive)
8488

@@ -99,7 +103,7 @@ def test_create_from_archive(self):
99103
self.assertIsInstance(self.repo.services['testsvc1.ITestService1'], FbsService)
100104

101105
def test_loaded_schema(self):
102-
schema_fn = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/testsvc1.bfbs')
106+
schema_fn = str(resources.files('autobahn.xbr.test.catalog.schema') / 'testsvc1.bfbs')
103107

104108
# get reflection schema loaded
105109
schema: FbsSchema = self.repo.schemas[schema_fn]

autobahn/xbr/test/test_xbr_schema_wamp_control.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import os
22
import copy
3-
import pkg_resources
3+
import sys
4+
if sys.version_info < (3, 10):
5+
import importlib_resources as resources
6+
else:
7+
from importlib import resources
48
import txaio
59
from unittest import skipIf
610

@@ -30,7 +34,7 @@ def setUp(self):
3034
self.repo = FbsRepository('autobahn')
3135
self.archives = []
3236
for fbs_file in ['wamp-control.bfbs']:
33-
archive = pkg_resources.resource_filename('autobahn', 'xbr/test/catalog/schema/{}'.format(fbs_file))
37+
archive = str(resources.files('autobahn.xbr.test.catalog.schema') / fbs_file)
3438
self.repo.load(archive)
3539
self.archives.append(archive)
3640

autobahn/xbr/test/test_xbr_secmod.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
import os
2828
import sys
29-
import pkg_resources
29+
if sys.version_info < (3, 10):
30+
import importlib_resources as resources
31+
else:
32+
from importlib import resources
3033
from random import randint, random
3134
from binascii import a2b_hex
3235
from typing import List
@@ -418,7 +421,7 @@ def test_secmod_from_seedphrase(self):
418421

419422
@inlineCallbacks
420423
def test_secmod_from_config(self):
421-
config = pkg_resources.resource_filename('autobahn', 'xbr/test/profile/config.ini')
424+
config = str(resources.files('autobahn.xbr.test.profile') / 'config.ini')
422425

423426
sm = SecurityModuleMemory.from_config(config)
424427
yield sm.open()
@@ -439,7 +442,7 @@ def test_secmod_from_config(self):
439442

440443
@inlineCallbacks
441444
def test_secmod_from_keyfile(self):
442-
keyfile = pkg_resources.resource_filename('autobahn', 'xbr/test/profile/default.priv')
445+
keyfile = str(resources.files('autobahn.xbr.test.profile') / 'default.priv')
443446

444447
sm = SecurityModuleMemory.from_keyfile(keyfile)
445448
yield sm.open()

docker/print-versions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import platform
44
import importlib
5-
import pkg_resources
5+
import importlib.metadata
66

77
import txaio
88
txaio.use_twisted() # noqa
@@ -28,7 +28,7 @@ def _get_version(name_or_module):
2828
v = name_or_module.version
2929
else:
3030
try:
31-
v = pkg_resources.get_distribution(name_or_module.__name__).version
31+
v = importlib.metadata.version(name_or_module.__name__)
3232
except:
3333
# eg flatbuffers when run from single file EXE (pyinstaller): https://github.com/google/flatbuffers/issues/5299
3434
v = '?.?.?'

docs/xbr.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ To directly use the embedded ABI files:
9595
.. code-block:: python
9696
9797
import json
98-
import pkg_resources
98+
import importlib.resources
9999
from pprint import pprint
100100
101-
with open(pkg_resources.resource_filename('xbr', 'contracts/XBRToken.json')) as f:
102-
data = json.loads(f.read())
103-
abi = data['abi']
104-
pprint(abi)
101+
text = (importlib.resources.files('xbr.abi') / 'XBRToken.json').read_text()
102+
data = json.loads(text)
103+
abi = data['abi']
104+
pprint(abi)
105105
106106
107107
Data stored on-chain

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@
235235
'txaio>=21.2.1', # MIT license (https://github.com/crossbario/txaio)
236236
'cryptography>=3.4.6', # BSD *or* Apache license (https://github.com/pyca/cryptography)
237237
'hyperlink>=21.0.0', # MIT license (https://github.com/python-hyper/hyperlink)
238-
'setuptools', # MIT license (https://github.com/pypa/setuptools)
238+
'importlib.resources>=5.0.0 ; python_version < "3.10"', # Apache license (https://github.com/python/importlib_resources/blob/main/LICENSE)
239+
239240
],
240241
extras_require={
241242
'all': extras_require_all,

0 commit comments

Comments
 (0)