Skip to content
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

Resolve namespace package #264

Merged
merged 9 commits into from
Jul 16, 2021
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