Skip to content

Add importlib.resources an as alternative for deprecated pkg_resources #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions stdnum/numdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,20 @@

import re

from pkg_resources import resource_stream

try:
from importlib.resources import files

def get_resource_stream(name):
"""Return resource stream for given name"""
return files('stdnum').joinpath(name).open('rb')

except ImportError:
from functools import partial

from pkg_resources import resource_stream

get_resource_stream = partial(resource_stream, __name__)


_line_re = re.compile(
Expand Down Expand Up @@ -165,6 +178,6 @@ def get(name):
if name not in _open_databases:
import codecs
reader = codecs.getreader('utf-8')
with reader(resource_stream(__name__, name + '.dat')) as fp:
with reader(get_resource_stream(name + '.dat')) as fp:
_open_databases[name] = read(fp)
return _open_databases[name]