Skip to content

Commit

Permalink
Switched to a hopefully more robust approach based on util.find_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jooste committed Dec 20, 2022
1 parent d26105c commit cb1debe
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions bluesky/pathfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
import itertools
from pathlib import Path
from importlib import import_module
from importlib.abc import MetaPathFinder

try:
from importlib.resources import files
from importlib.readers import MultiplexedPath
except ImportError:
# Python < 3.9 only provides deprecated resources API
from importlib.resources import path
# Previous alternatives to find resources seem to be difficult
# to get right on all platforms and python versions, so instead
# this approach based on the search locations from the package spec
from importlib.util import find_spec
def files(package):
res = '.'
res = ''
if '.' in package:
package, res = package.split('.', 1)
s = find_spec(package)
if s.submodule_search_locations:
p = Path(s.submodule_search_locations[0])
else:
p = Path(package)
return p / res.replace('.', '/')

p = path(package, res)
return Path(p.args[0].path).parent / p.args[1]

class MultiplexedPath:
def __init__(self, *paths) -> None:
Expand Down

0 comments on commit cb1debe

Please sign in to comment.