Skip to content

Commit

Permalink
Update implement
Browse files Browse the repository at this point in the history
  • Loading branch information
mzr1996 committed Jun 29, 2023
1 parent eeb20e3 commit b726593
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions mmengine/config/new_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import copy
import importlib
import inspect
import platform
import sys
from importlib.machinery import NamespaceLoader
from functools import partial
from importlib.util import spec_from_loader
from pathlib import Path
from types import FunctionType, ModuleType
from types import BuiltinFunctionType, FunctionType, ModuleType
from typing import Any, Optional, Tuple, Union

from yapf.yapflib.yapf_api import FormatCode
Expand All @@ -17,6 +19,11 @@
DELETE_KEY = '_delete_'
RESERVED_KEYS = ['filename', 'text', 'pretty_text']

if platform.system() == 'Windows':
import regex as re
else:
import re # type: ignore


class Config:
"""A facility for config and config files.
Expand Down Expand Up @@ -148,9 +155,10 @@ def _parse_lazy_import(filename: Union[str, Path]) -> ConfigDict:
@staticmethod
def _get_config_module(filename: Union[str, Path], level=0):
file = Path(filename).absolute()
module_name = re.sub(r'\W|^(?=\d)', '_', file.stem)
parent_pkg = Config._parent_pkg + str(level)
fullname = '.'.join([parent_pkg] * Config._max_parent_depth +
[file.stem])
[module_name])

# import config file as a module
with LazyImportContext():
Expand Down Expand Up @@ -301,12 +309,15 @@ def _format_basic_types(input_):
return _format_list_tuple_set(input_)
elif isinstance(input_, LazyObject):
return repr(str(input_))
elif isinstance(input_, (type, FunctionType)):
elif isinstance(input_, (type, FunctionType, BuiltinFunctionType)):
if Config._parent_pkg in input_.__module__:
# which defined in the config file.
return repr(input_.__name__)
# defined in the config file.
module = input_.__module__.rpartition('.')[-1]
else:
return repr(input_.__module__ + input_.__name__)
module = input_.__module__
return repr(module + '.' + input_.__qualname__)
elif isinstance(input_, partial):
return repr(str(input_))
else:
return str(input_)

Expand Down Expand Up @@ -500,7 +511,7 @@ def lazy2string(cfg_dict):
return lazy2string(_cfg_dict)


class BaseLoader(importlib.abc.Loader):
class BaseConfigLoader(importlib.abc.Loader):

def __init__(self, filepath, level) -> None:
self.filepath = filepath
Expand All @@ -516,6 +527,17 @@ def exec_module(self, module):
getattr(module, k))


class ParentFolderLoader(importlib.abc.Loader):

@staticmethod
def create_module(spec):
return ModuleType(spec.name)

@staticmethod
def exec_module(module):
pass


class BaseImportContext(importlib.abc.MetaPathFinder):

def find_spec(self, fullname, path=None, target=None):
Expand All @@ -529,8 +551,8 @@ def find_spec(self, fullname, path=None, target=None):
if names[-1] == parent_pkg:
self.base_modules.append(fullname)
# Create parent package
return importlib.util.spec_from_loader(
fullname, NamespaceLoader(fullname, None, None))
return spec_from_loader(
fullname, loader=ParentFolderLoader, is_package=True)
elif names[0] == parent_pkg:
self.base_modules.append(fullname)
# relative imported base package
Expand All @@ -544,15 +566,15 @@ def find_spec(self, fullname, path=None, target=None):
# print(f"\033[92m'filepath'\033[0m: {filepath}")
if filepath.is_dir():
# If a dir, create a package.
return importlib.util.spec_from_loader(
fullname, NamespaceLoader(fullname, filepath, None))
return spec_from_loader(
fullname, loader=ParentFolderLoader, is_package=True)

pypath = filepath.with_suffix('.py')

if not pypath.exists():
raise ImportError(f'Not found base path {filepath.resolve()}')
return importlib.util.spec_from_loader(
fullname, BaseLoader(pypath, self.level + 1))
fullname, BaseConfigLoader(pypath, self.level + 1))

return None

Expand Down

0 comments on commit b726593

Please sign in to comment.