def resolve(name, package=None): """ Turn a dotted name into a module or class reference """ if isinstance(package, str): package = resolve_exposing(package) if package: name = resolve_name('.{}'.format(name), package.__name__) try: # Try to get a module return resolve_exposing(name) except ImportError as err: if '.' not in name: raise NotFoundError( '{n} is not a valid module name'.format(n=name)) from err try: # Try to get an attribute of a module mod, attr = name.rsplit('.', maxsplit=1) package = resolve_exposing(mod) cls = getattr(package, attr) assert (isinstance(cls, type)) return cls except ImportError as err: raise NotFoundError( '{n} is not a valid class or module name'.format(n=name)) from err except AttributeError as err: raise NotFoundError('{a} does not exist within {m}'.format( a=attr, m=mod)) from err except AssertionError as err: raise ResolveError('{a} in {m} is not a valid class'.format( a=attr, m=mod)) from err
def extract_function_args_and_arg_imports_from_import( function_name: str, imp: Optional[AnyImportStatement], import_section_path_str: Optional[str] = None) -> ArgumentsAndImports: from pyfileconf.io.func.load.extractimp import extract_import_statements_from_function_args_imports_and_assigns if imp is None: raise NoImportStatementException( 'no import passed to extract function and imports') filepath = get_module_filepath_from_import(imp, import_section_path_str) if function_name in imp.renames.new_names: # If was renamed in the import, # Get original name from rename statement function_name = imp.renames.reverse_name_map[function_name] loader = ImportAssignmentLazyLoader(filepath) # TODO [#44]: handle relative nested imports such as from .this import that # # This code keeps importing and checking whether the name is defined in the file. If not, # it follows the next import. This doesn't properly work if one of those followed imports # is a relative import such as `from .this import that`. Add test to show this and # also fix it. ast_function_def = extract_function_definition_or_class_init_from_ast_by_name( loader.ast, function_name) if ast_function_def is not None: # Found function definition in this import # TODO [#12]: handle other class methods - also update extract_function_definition function if ast_function_def.name == '__init__': # Got class init del ast_function_def.args.args[0] # delete first arg (self) function_arg_imports = extract_import_statements_from_function_args_imports_and_assigns( ast_function_def.args, loader.imports, loader.assigns, imp.module) return ast_function_def.args, function_arg_imports # Else, this function must have been imported into this file as well. # Must find the matching import, the extract args from that import next_level_import = loader.imports.get_import_for_module_or_obj_name( function_name) if next_level_import is None: raise ValueError( f'not able to find function definition for {function_name}. Could not ' f'find in definitions or in imports') if import_section_path_str is not None: next_module = next_level_import.module new_section_path = resolve_name( next_module, import_section_path_str) if next_module.startswith( '.') else next_module else: new_section_path = None return extract_function_args_and_arg_imports_from_import( function_name, imp=next_level_import, import_section_path_str=new_section_path)
def load(self, name: str, package: Optional[str] = None) -> None: """ "Loads" an extension off of the current client by adding a new class which is imported from the library. :param name: The name of the extension. :type name: str :param package?: The package of the extension. :type package: Optional[str] """ _name: str = resolve_name(name, package) if _name in self._extensions: log.error(f"Extension {name} has already been loaded. Skipping.") module = import_module(name, package) try: setup = getattr(module, "setup") setup(self) except Exception as error: del sys.modules[name] log.error(f"Could not load {name}: {error}. Skipping.") else: log.debug(f"Loaded extension {name}.") self._extensions[_name] = module
def run(self): self.delayobj.wait() # now import module properly modname = resolve_name(self.name, self.package) if isinstance(sys.modules[modname], BackgroundModuleProxy): del sys.modules[modname] mod = importlib.import_module(self.name, package=self.package) for targname, varname in self.replacements.items(): if targname in sys.modules: targmod = sys.modules[targname] setattr(targmod, varname, mod)
def __init__(self, config, database): super().__init__(command_prefix='!') self.config = config self.database = database for extension in _BOT_EXTENSIONS: # The load_extension method expects an "absolute" package name, but we # want to be relative because why not? So we resolve the name relative # to our package name. self.load_extension(resolve_name(extension, __package__))
def resolve_name(name, package): level = 0 if name.startswith('.'): for character in name: if character != '.': break level += 1 try: # py3k compat. name = importlib._resolve_name(name[level:], package, level) except AttributeError: from importlib import util name = util.resolve_name(name, package) return name
def load_module_in_background(name, package=None, debug='DEBUG', env=None, replacements=None, delayobj=BackgroundModuleDelayDefault()): """Entry point for loading modules in background thread. Parameters ---------- name : str Module name to load in background thread. package : str or None, optional Package name, has the same meaning as in importlib.import_module(). debug : str, optional Debugging symbol name to look up in the environment. env : Mapping or None, optional Environment this will default to __xonsh_env__, if available, and os.environ otherwise. replacements : Mapping or None, optional Dictionary mapping fully qualified module names (eg foo.bar.baz) that import the lazily loaded moudle, with the variable name in that module. For example, suppose that foo.bar imports module a as b, this dict is then {'foo.bar': 'b'}. delayobj: initial delay before loading module default(None): until sys.modules stops growing for 5ms. Other options include Noop (do not wait), ExpBackoff (exponential backoff), and Const (constant delay). Returns ------- module : ModuleType This is either the original module that is found in sys.modules or a proxy module that will block until delay attribute access until the module is fully loaded. """ modname = resolve_name(name, package) if modname in sys.modules: return sys.modules[modname] if env is None: try: import builtins env = getattr(builtins, '__xonsh_env__', os.environ) except: return os.environ if env.get(debug, None): mod = importlib.import_module(name, package=package) return mod proxy = sys.modules[modname] = BackgroundModuleProxy(modname) BackgroundModuleLoader(name, package, replacements or {}, delayobj) return proxy
def import_module_from_path(name, path): """ Imports the contents of a module from a relative or absolute path and makes its content available for usage. Simulates `from module import *`. Examples: ```python from flashback.importing import import_module_from_path import_module_from_path('logging', 'flashback') print(DEFAULT_CONSOLE_CONFIGURATION) ``` Params: - `name (str)` the name of the module to import - `path (str)` the relative path in which to find the module to import Returns: - `None` Raises: - `ImportError` if a relative import beyond the top-level package is attempted - `ImportError` if the request module is not found """ if path.startswith('.'): caller_module = inspect.getmodule(get_frameinfo(1).frame) caller_package = caller_module.__package__ module_path = util.resolve_name(path, caller_package) else: module_path = path imported_module = import_module(module_path + '.' + name) if hasattr(imported_module, '__all__'): to_globalize = { name: getattr(imported_module, name) for name in imported_module.__all__ } else: to_globalize = { name: attr for name, attr in imported_module.__dict__.items() if not name.startswith('_') } globals().update(to_globalize)
def load_module(cls, mod_path: str, package: str = None) -> ModuleType: """ Load a module by its absolute path. Args: mod_path: the absolute or relative module path package: the parent package to search for the module Returns: The resolved module or `None` if the module cannot be found Raises: ModuleLoadError: If there was an error loading the module """ if package: # preload parent package if not cls.load_module(package): return None # must treat as a relative import if not mod_path.startswith("."): mod_path = f".{mod_path}" full_path = resolve_name(mod_path, package) if full_path in sys.modules: return sys.modules[full_path] if "." in mod_path: parent_mod_path, mod_name = mod_path.rsplit(".", 1) if parent_mod_path and parent_mod_path[-1] != ".": parent_mod = cls.load_module(parent_mod_path, package) if not parent_mod: return None package = parent_mod.__name__ mod_path = f".{mod_name}" # Load the module spec first # this means that a later ModuleNotFoundError indicates a code issue spec = find_spec(mod_path, package) if not spec: return None try: return import_module(mod_path, package) except ModuleNotFoundError as e: raise ModuleLoadError( f"Unable to import module {full_path}: {str(e)}") from e
def import_class_from_path(name, path): """ Imports a class from a relative or absolute path, and returns it. Similar to `from module import Class` if this statement returned the imported class. Examples: ```python from flashback.importing import import_class_from_path borg_class = import_class_from_path('borg', 'flashback') borg_class() ``` Paramss: - `name (str)` the name of the class to import - `path (str)` the relative path in which to find the class to import Returns: - `Callable` the class from the imported module Raises: - `ImportError` if the requested module is not found - `ImportError` if a relative import beyond the top-level package is attempted - `AttributeError` if the class is not found in the imported module """ if path.startswith('.'): caller_module = inspect.getmodule(get_frameinfo(1).frame) caller_package = caller_module.__package__ module_path = util.resolve_name(path, caller_package) else: module_path = path # Loads the module, will raise ImportError if module cannot be loaded # The module import is called with the complete absolute class path # (`import_module(absolute_path)`) rather than the relative class path for an absolute package # path (`import_module(relative_path, package=absolute_path)`) because it can happen that the # package is not yet loaded when it tries to import. imported_module = import_module(module_path + '.' + name) # Gets the class, will raise AttributeError if class cannot be found return getattr(imported_module, pascalize(name))
def remove(self, name: str, package: Optional[str] = None) -> None: """ Removes an extension out of the current client from an import resolve. :param name: The name of the extension. :type name: str :param package?: The package of the extension. :type package: Optional[str] """ _name: str = resolve_name(name, package) module = self._extensions.get(_name) if module not in self._extensions: log.error( f"Extension {name} has not been loaded before. Skipping.") log.debug(f"Removed extension {name}.") del sys.modules[_name] del self._extensions[_name]
def load_module_in_background(name, package=None, debug='DEBUG', env=None, replacements=None): """Entry point for loading modules in background thread. Parameters ---------- name : str Module name to load in background thread. package : str or None, optional Package name, has the same meaning as in importlib.import_module(). debug : str, optional Debugging symbol name to look up in the environment. env : Mapping or None, optional Environment this will default to __xonsh_env__, if available, and os.environ otherwise. replacements : Mapping or None, optional Dictionary mapping fully qualified module names (eg foo.bar.baz) that import the lazily loaded moudle, with the variable name in that module. For example, suppose that foo.bar imports module a as b, this dict is then {'foo.bar': 'b'}. Returns ------- module : ModuleType This is either the original module that is found in sys.modules or a proxy module that will block until delay attribute access until the module is fully loaded. """ modname = resolve_name(name, package) if modname in sys.modules: return sys.modules[modname] if env is None: try: import builtins env = getattr(builtins, '__xonsh_env__', os.environ) except: return os.environ if env.get(debug, None): mod = importlib.import_module(name, package=package) return mod proxy = sys.modules[modname] = BackgroundModuleProxy(modname) BackgroundModuleLoader(name, package, replacements or {}) return proxy
def reload(self, name: str, package: Optional[str] = None) -> None: """ "Reloads" an extension off of current client from an import resolve. :param name: The name of the extension. :type name: str :param package?: The package of the extension. :type package: Optional[str] """ _name: str = resolve_name(name, package) module = self._extensions.get(_name) if module is None: log.warning( f"Extension {name} could not be reloaded because it was never loaded." ) self.extend(name, package) self.remove(name, package) self.load(name, package)
def find_spec(name, package=None) -> ModuleSpec: """Return the spec for the specified module. Note: this is a modification of importlib.util.find_spec which does not import parent packages. It instead recursively searches through the parents. First, sys.modules is checked to see if the module was already imported. If so, then sys.modules[name].__spec__ is returned. If that happens to be set to None, then ValueError is raised. If the module is not in sys.modules, then sys.meta_path is searched for a suitable spec with the value of 'path' given to the finders. None is returned if no spec could be found. If the name is for submodule (contains a dot), find_spec is called on the parent module to determine the submodule_search_locations. The name and package arguments work the same as importlib.import_module(). In other words, relative module names (with leading dots) work. """ fullname = resolve_name(name, package) if name.startswith('.') else name if fullname not in sys.modules: parent_name = fullname.rpartition('.')[0] if parent_name: # Use builtins.__import__() in case someone replaced it. parent = find_spec(parent_name, package=package) return _find_spec(fullname, parent.submodule_search_locations) else: return _find_spec(fullname, None) else: module = sys.modules[fullname] if module is None: return None try: spec = module.__spec__ except AttributeError: raise ValueError('{}.__spec__ is not set'.format(name)) from None else: if spec is None: raise ValueError('{}.__spec__ is None'.format(name)) return spec
def find_imports(file, name, resolve_relative=True): from importlib.util import resolve_name results = set() with open(file, mode="r", encoding="UTF-8") as f: data = f.read() data = data.splitlines(False) possible_imports = tuple( i.lstrip(" ") for i in data if i.lstrip(" ").startswith(startswith_hints)) for i in possible_imports: i = i.replace("cimport", "import") if i.startswith("import"): val = i.split(" ", 1)[1] val = tuple(j.strip() for j in val.split(",")) val = tuple(j.split(" ")[0].strip() for j in val) results.update(val) continue elif i.startswith("from"): val = i.split(" ", 3) source = val[1] val = tuple(source + "." + j.strip().split(" ", 2)[0] for j in val[3].split(", ")) results.update(val) for i in tuple(results): if i.count(".") == 1: continue lst = i.split(".") while True: del lst[-1] if len(lst) == 0: break text = '.'.join(lst) if len(text) == 0: break results.add(text) if resolve_relative == True: package_name = name.split(".")[:-1] package_name = '.'.join(package_name) # print(package_name, results) results = {resolve_name(i, package_name) for i in results} return results
def run(self): # wait for other modules to stop being imported # We assume that module loading is finished when sys.modules doesn't # get longer in 5 consecutive 1ms waiting steps counter = 0 last = -1 while counter < 5: new = len(sys.modules) if new == last: counter += 1 else: last = new counter = 0 time.sleep(0.001) # now import module properly modname = resolve_name(self.name, self.package) if isinstance(sys.modules[modname], BackgroundModuleProxy): del sys.modules[modname] mod = importlib.import_module(self.name, package=self.package) for targname, varname in self.replacements.items(): if targname in sys.modules: targmod = sys.modules[targname] setattr(targmod, varname, mod)
def test_escape(self): # ..bacon in spam with self.assertRaises(ValueError): util.resolve_name('..bacon', 'spam')
def test_other_package(self): # ..bacon in spam.bacon self.assertEqual('spam.bacon', util.resolve_name('..bacon', 'spam.eggs'))
def test_in_package(self): # .bacon in spam self.assertEqual('spam.eggs.bacon', util.resolve_name('.bacon', 'spam.eggs'))
def test_no_package(self): # .bacon in '' with self.assertRaises(ValueError): util.resolve_name('.bacon', '')
def lazy_import_module( name: str, package: Optional[str] = None ) -> ModuleType: """Lazy import a python module. Args: name (:obj:`str`): specifies what module to import in absolute or relative terms (e.g. either ``pkg.mod`` or ``..mod``). package (:obj:`str`, optional): If ``name`` is specified in relative terms, then the ``package`` argument must be set to the name of the package which is to act as the anchor for resolving the package name. Defaults to ``None``. Raises: ImportError: if the given ``name`` and ``package`` can not be loaded. :rtype: :obj:`Module <types.ModuleType>` * The lazy imported module with the execution of it's loader postponed until an attribute accessed. .. Warning:: For projects where startup time is critical, this function allows for potentially minimizing the cost of loading a module if it is never used. For projects where startup time is not essential then use of this function is heavily discouraged due to error messages created during loading being postponed and thus occurring out of context. Examples: >>> from flutils.moduleutils import lazy_import_module >>> module = lazy_import_module('mymodule') Relative import: >>> module = lazy_import_module('.mysubmodule', package='mymodule') """ if isinstance(package, str) and package: package = cast(str, package) fullname = util.resolve_name(name, package=package) else: fullname = util.resolve_name(name, package='') # Return the module if it's already been imported. if fullname in sys.modules: return sys.modules[fullname] # Find the spec for the desired module spec = util.find_spec(fullname) if spec is None: raise ImportError("name=%r package=%r" % (name, package)) # Use the _LazyLoader to wrap the real loader. The _LazyLoader # will only load and execute the module when an attribute is # accessed. loader = spec.loader loader = cast(Loader, loader) lazy_loader = _LazyLoader(loader) # Within a Python import there is the process of module # creation. This is basically a two step process that # is handled by the loaders <https://bit.ly/2Jz8E4C>: # 1. Create a module namespace from a spec. # (see types.ModuleType <https://bit.ly/2qlJyyf>) # 2. Execute the module in it's own namespace. # # All loaders SHOULD have a create_module(spec) which # creates the namespace. Additionally, all loaders # should have the exec_module(module) which executes # the module. # # In the case of any file loader the creation of a # module namespace would require the loading of the. # file. Which would defeat the purpose of lazy loading. # in this case the create_module(spec) method will # return None. # # These two methods were added to the loaders # in Python (version 3.4) and some of the loaders will # not make use of these methods. These loaders still # use the load_module(fullname) method, which combines # the two steps (mentioned above) into one method. In # this case the create_module(spec) may not exist or # will return None. # Create a module namespace. if hasattr(spec.loader, 'create_module'): module = lazy_loader.create_module(spec) else: module = None # If the loader doesn't make use of the create_module # method, then create a very simple module namespace. if module is None: # create a dummy module to work with module = ModuleType(fullname) module.__spec__ = spec # Have the _LazyLoader execute the module. This # preps the module namespace to be lazy loaded # and makes the module a _LazyModule namespace. lazy_loader.exec_module(module) # Add the module to the python module map. sys.modules[fullname] = module return module
def import_module_as_main(name, script_mode): """Import a module, pretending it's __main__. We support only new-style loaders that provide `exec_module`; old-style loaders that provide `load_module` and `create_module` are not supported. Upon success, replaces `sys.modules["__main__"]` with the module that was imported. Upon failure, propagates any exception raised. This is a customized approximation of the standard import semantics, based on: https://docs.python.org/3/library/importlib.html#approximating-importlib-import-module https://docs.python.org/3/reference/import.html#loading https://docs.python.org/3/reference/import.html#import-related-module-attributes https://docs.python.org/3/reference/import.html#module-path https://docs.python.org/3/reference/import.html#special-considerations-for-main """ absolute_name = resolve_name(name, package=None) # If already loaded, normally we should return the module from `sys.modules`, # but `sys.modules["__main__"]` is for now this bootstrapper, not the user __main__. # # Note __main__ and somemod are distinct modules, even when the same file, # because __main__ triggers `if __name__ == '__main__'` checks, but somemod doesn't. # # try: # return sys.modules[absolute_name] # except KeyError: # pass path = None if '.' in absolute_name: if script_mode: raise ValueError( "In script mode, please add the containing directory to `sys.path` and then top-level import the final component of the name." ) # Import the module's parent package normally to initialize parent packages. # Get the appropriate `path` for `find_spec` to find subpackages and modules. parent_name, _, child_name = absolute_name.rpartition('.') parent_module = import_module(parent_name) path = parent_module.__spec__.submodule_search_locations for finder in sys.meta_path: if not hasattr( finder, "find_spec" ): # pkg_resources.extern.VendorImporter has no find_spec continue # https://docs.python.org/3/library/importlib.html#importlib.abc.MetaPathFinder.find_spec spec = finder.find_spec(absolute_name, path) if spec is not None: break else: msg = f"No module named {absolute_name}" raise ModuleNotFoundError(msg, name=absolute_name) spec.name = "__main__" if spec.loader: spec.loader.name = "__main__" module = module_from_spec(spec) try_mainpy = False if script_mode: # e.g. "macropython somepackage/__init__.py" module.__package__ = "" elif path: # e.g. "macropython -m somepackage.module" module.__package__ = parent_name elif spec.origin.endswith( "__init__.py"): # e.g. "macropython -m somepackage" module.__package__ = absolute_name try_mainpy = True if spec.origin == "namespace": # https://docs.python.org/3/reference/import.html#__path__ module.__path__ = spec.submodule_search_locations if try_mainpy: # e.g. "import somepackage" in the above case; it's not __main__, so import it normally. assert not script_mode parent_module = import_module(absolute_name) elif spec.loader: sys.modules[ "__main__"] = module # replace this bootstrapper with the new __main__ try: spec.loader.exec_module(module) except Exception as err: sys.modules["__main__"] = _macropython_module if isinstance(err, MacroApplicationError): # To avoid noise, discard most of the traceback of the chained # macro-expansion errors emitted by the expander core. The # linked (`__cause__`) exceptions have the actual tracebacks. # # Keep just the last entry, which should state that this # exception came from `expand` in `core.py`. tb = err.__traceback__ while tb.tb_next: tb = tb.tb_next raise err.with_traceback(tb) raise # # __main__ has no parent module so we don't need to do this. # if path is not None: # setattr(parent_module, child_name, module) else: # namespace packages have loader=None try_mainpy = True # __init__.py (if any) has run; run __main__.py, like `python -m somepackage` does. if try_mainpy: has_mainpy = True try: # __main__.py doesn't need to have its name set to "__main__", so import it normally. import_module(f"{absolute_name}.__main__") except ImportError as e: if "No module named" in e.msg: has_mainpy = False else: raise if not has_mainpy: raise ImportError( f"No module named {absolute_name}.__main__; '{absolute_name}' is a package and cannot be directly executed" ) return module
"""DO NOT EDIT THIS FILE WHILE THE 'COAX_TEST_CAVITY' MEASUREMENT SETUP REMAINS UNCHANGED""" """This file initializes instruments that are part of the measurement setup ("stage")""" import sys from importlib import reload from importlib.util import resolve_name # import Instrument classes from the codebase from qcrew.codebase.instruments import LabBrick, QuantumElement # import qm from qm.QuantumMachinesManager import QuantumMachinesManager config_module_path = resolve_name( ".configuration", "qcrew.experiments.coax_test_cavity.imports" ##xiaozhou ) if config_module_path not in sys.modules: import qcrew.experiments.coax_test_cavity.imports.configuration as cfg ##xiaozhou else: reload(cfg) ######################################################################################## ######################################################################################## config = cfg.config qubit_IF = cfg.qubit_IF qubit_LO = cfg.qubit_LO rr_IF = cfg.rr_IF rr_LO = cfg.rr_LO cavity_IF = cfg.cavity_IF cavity_LO = cfg.cavity_LO
def load_deprecated_from_module(self, module_name, level=None): # level may be none when it's taken from the ImportFrom node if level is None: level = 0 # update module/pkg based on level rmodule_name = '.' * level + module_name # perform the module lookup try: module_name = resolve_name(rmodule_name, self.pkg_name) except (ImportError, ValueError): return None module_path = resolve_module(module_name, self.search_paths) # hopefully a module was found if module_path is None: return None module_key = self.key_factory(module_path, name_hint=module_name) # either find it in the cache if module_key in self.cache: data = self.cache[module_key] if data['version'] == Format.version: return dict(load_deprecated(entry) for entry in data['deprecated']) elif data['generator'] == 'manual': warnings.warn( ("skipping module {} because it has an obsolete, " "manually generated, cache file: {}") .format(module_name, module_key.module_hash)) return {} # or fill a new entry # To avoid loop, mark the key as in process self.cache[module_key] = {'generator': 'manual', 'deprecated': []} with open(module_path) as fd: try: module, syntax_errors = frilouz.parse(ast.parse, fd.read()) except UnicodeDecodeError: return [] duc = SilentDefUseChains() duc.visit(module) anc = beniget.Ancestors() anc.visit(module) # Collect deprecated functions if self.recursive and module_path not in self.visited: self.visited.add(module_path) current_pkg = ".".join(module_name.split('.')[:-1]) resolver = ImportResolver(self.decorator, self.reason_keyword, self.search_paths, self.recursive, parent=self, pkg_name=current_pkg) resolver.visit(module) deprecated_imports = [make_deprecated(d, reason) for _, _, d, reason in resolver.get_deprecated_users(duc, anc)] else: deprecated_imports = [] deprecated = self.collect_deprecated(module, duc, anc, pkg_name=module_name) deprecated.update(deprecated_imports) dl = {symbol_name(d[0]): d[1] for d in deprecated if d is not None} data = {'generator': 'memestra', 'deprecated': [store_deprecated(d, dl[d]) for d in sorted(dl)]} self.cache[module_key] = data return dl
def load(cls, locale, path): """ Loads a `locale` definition from a package `path` and exposes its contents. If the `path` is relative, it is transformed to absolute using the call stack. Generates locales candidates to use as fallback, e.g.: 'en_US.UTF-8' will yield 'en_us' and 'en'. Then tries to import from the most to the least precise ('en_us', then 'en') until it imports something. The cache uses the complete localization file's path to avoid conflicts and overrides. The code is very similar to `flashback.importing.import_module_from_path` with the following tweaks: - It handles candidate generation - It caches the imported module Examples: ```python from flashback.i16g import Locale Locale.load('fr_FR', '.languages') #=> Whatever defined in fr_FR Locale.load('fr_FR.UTF-8@latin', 'config.locales') #=> Whatever defined in fr_FR Locale.load('not-implemented', 'conf.production') #=> NotImplementedError ``` Params: - `locale (str)` the given locale - `path (str)` the path in which to find the locale definition Returns: - `Module` the content of the loaded locale Raises: - `NotImplementedError` if the given locale implementation is not found """ locale = cls.simplify(locale) candidate_locales = sorted({locale, locale.split('_')[0]}, key=len, reverse=True) if path.startswith('.'): caller_module = inspect.getmodule(get_frameinfo(1).frame) caller_package = caller_module.__package__ module_path = util.resolve_name(path, caller_package) else: module_path = path for candidate_locale in candidate_locales: locale_full_path = module_path + '.' + candidate_locale if locale_full_path in cls.__cache: return cls.__cache[locale_full_path] # Loads the module, will suppress ImportError if module cannot be loaded # because it will raise NotImplementedError only once it has been through all locale # candidates try: imported_locale = import_module(locale_full_path) cls.__cache[locale_full_path] = imported_locale return imported_locale except ImportError: pass raise NotImplementedError( f"locale {locale!r} is not implemented in {module_path}")
def test_absolute(self): # bacon self.assertEqual('bacon', util.resolve_name('bacon', None))
def test_aboslute_within_package(self): # bacon in spam self.assertEqual('bacon', util.resolve_name('bacon', 'spam'))
"""DO NOT EDIT THIS FILE WHILE THE 'COAX_TEST' MEASUREMENT SETUP REMAINS UNCHANGED""" """This file initializes instruments that are part of the measurement setup ("stage")""" import sys from importlib import reload from importlib.util import resolve_name # import Instrument classes from the codebase from qcrew.codebase.instruments import LabBrick, QuantumElement # import qm from qm.QuantumMachinesManager import QuantumMachinesManager config_module_path = resolve_name( ".configuration", "qcrew.experiments.sample_B.imports" ) if config_module_path not in sys.modules: import qcrew.experiments.sample_B.imports.configuration as cfg else: reload(cfg) ######################################################################################## ######################################################################################## config = cfg.config qubit_IF = cfg.qubit_IF qubit_LO = cfg.qubit_LO rr_IF = cfg.rr_IF rr_LO = cfg.rr_LO qubit_mixer_offsets = cfg.qubit_mixer_offsets rr_mixer_offsets = cfg.rr_mixer_offsets
"""DO NOT EDIT THIS FILE WHILE THE 'COAX_TEST' MEASUREMENT SETUP REMAINS UNCHANGED""" """This file initializes instruments that are part of the measurement setup ("stage")""" import sys from importlib import reload from importlib.util import resolve_name # import Instrument classes from the codebase from qcrew.codebase.instruments import LabBrick, QuantumElement # import qm from qm.QuantumMachinesManager import QuantumMachinesManager config_module_path = resolve_name(".configuration", "qcrew.experiments.coax_test.imports") if config_module_path not in sys.modules: import qcrew.experiments.coax_test.imports.configuration as cfg else: reload(cfg) ######################################################################################## ######################################################################################## config = cfg.config qubit_IF = cfg.qubit_IF qubit_LO = cfg.qubit_LO rr_IF = cfg.rr_IF rr_LO = cfg.rr_LO qubit_mixer_offsets = cfg.qubit_mixer_offsets rr_mixer_offsets = cfg.rr_mixer_offsets ########################################################################################
def collect_deprecated(self, node, duc, ancestors, pkg_name=None): deprecated = set() for dlocal in duc.locals[node]: dnode = dlocal.node if not isinstance(dnode, ast.alias): continue original_path = tuple(dnode.name.split('.')) # for imports with a "from" clause, such as # # from foo import bar # # the AST alias will be just `bar`, but we want any functions # defined as such: # # @bar # def foo(): pass # # to be picked up when `foo.bar` is used as the target decorator. we # check if the parent of the alias is an ImportFrom node and fix the # original path to be fully qualified here. In the example above, it # becomes `foo.bar` instead of just `bar`. alias_parent = ancestors.parents(dnode)[-1] if isinstance(alias_parent, ast.ImportFrom): module = "." # A module can be None if a relative import from "." occurs if alias_parent.module is not None: module = resolve_name(alias_parent.module, self.pkg_name) original_path = tuple(module.split('.')) + original_path nbterms = len(original_path) if original_path == self.decorator[:nbterms]: for user in dlocal.users(): parents = list(ancestors.parents(user.node)) attrs = list(reversed(self.decorator[nbterms:])) while attrs and parents: attr = attrs[-1] parent = parents.pop() if not isinstance(parent, (ast.Attribute)): break if parent.attr != attr: break attrs.pop() # path parsing fails if some attr left if attrs: continue # Only handle decorators attached to a def self.extract_decorator_from_parents( parents, deprecated) if not self.recursive: return deprecated # In recursive mode, we consider deprecated any imported # deprecated symbol. for dlocal in duc.locals[node]: dnode = dlocal.node if not isinstance(dnode, ast.alias): continue alias_parent = ancestors.parents(dnode)[-1] if isinstance(alias_parent, ast.ImportFrom): if not alias_parent.module: continue resolver = ImportResolver(self.decorator, self.reason_keyword, self.search_paths, self.recursive, self, pkg_name) imported_deprecated = resolver.load_deprecated_from_module( alias_parent.module, level=alias_parent.level) if not imported_deprecated: continue if dnode.name in imported_deprecated: dinfo = make_deprecated(dnode, imported_deprecated[dnode.name]) deprecated.add(dinfo) elif dnode.name == '*': for name, reason in imported_deprecated.items(): dinfo = make_deprecated(DeprecatedStar(name, dnode), reason) deprecated.add(dinfo) return deprecated