Skip to content

Commit

Permalink
remove setuptools dependency
Browse files Browse the repository at this point in the history
replace deprecated pkg_resources methods with importlib.resources and
importlib.metadata
  • Loading branch information
dimbleby committed Jan 5, 2025
1 parent e84c6ee commit 566d290
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 32 deletions.
26 changes: 16 additions & 10 deletions autobahn/xbr/_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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')


#
Expand Down
7 changes: 5 additions & 2 deletions autobahn/xbr/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions autobahn/xbr/_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))


Expand Down
9 changes: 7 additions & 2 deletions autobahn/xbr/test/test_xbr_schema_demo.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 7 additions & 3 deletions autobahn/xbr/test/test_xbr_schema_wamp.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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]
Expand Down
8 changes: 6 additions & 2 deletions autobahn/xbr/test/test_xbr_schema_wamp_control.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)

Expand Down
9 changes: 6 additions & 3 deletions autobahn/xbr/test/test_xbr_secmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions docker/print-versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import platform
import importlib
import pkg_resources
import importlib.metadata

import txaio
txaio.use_twisted() # noqa
Expand All @@ -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 = '?.?.?'
Expand Down
10 changes: 5 additions & 5 deletions docs/xbr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 566d290

Please sign in to comment.