Skip to content

Commit

Permalink
Resolve namespace package (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
ifeitao committed Jul 16, 2021
1 parent 5d08816 commit 0918300
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions pyhocon/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ def glob(pathname, recursive=False):
if sys.version_info >= (3, 4):
import importlib.util

def find_package_dir(name):
def find_package_dirs(name):
spec = importlib.util.find_spec(name)
# When `imp.find_module()` cannot find a package it raises ImportError.
# Here we should simulate it to keep the compatibility with older
# versions.
if not spec:
raise ImportError('No module named {!r}'.format(name))
return os.path.dirname(spec.origin)
return spec.submodule_search_locations
else:
import imp
import importlib

def find_package_dir(name):
return imp.find_module(name)[1]
def find_package_dirs(name):
return [imp.find_module(name)[1]]


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -758,9 +759,14 @@ def resolve_package_path(cls, package_path):
if ':' not in package_path:
raise ValueError("Expected format is 'PACKAGE:PATH'")
package_name, path_relative = package_path.split(':', 1)
package_dir = find_package_dir(package_name)
path_abs = os.path.join(package_dir, path_relative)
return path_abs
package_dirs = find_package_dirs(package_name)
for package_dir in package_dirs:
path_abs = os.path.join(package_dir, path_relative)
if os.path.exists(path_abs):
return path_abs
raise ImportError("Can't find {path_relative} in package:{package_name}".format(
path_relative=path_relative,
package_name=package_name))


class ListParser(TokenConverter):
Expand Down

0 comments on commit 0918300

Please sign in to comment.