Python pkgutil.ImpImporter() Examples

The following are 5 code examples of pkgutil.ImpImporter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pkgutil , or try the search function .
Example #1
Source File: test_pkgutil.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_importer_deprecated(self):
        with self.check_deprecated():
            x = pkgutil.ImpImporter("") 
Example #2
Source File: test_pkgutil.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_importer_deprecated(self):
        with self.check_deprecated():
            x = pkgutil.ImpImporter("") 
Example #3
Source File: test_pkgutil.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_importer_deprecated(self):
        with self.check_deprecated():
            pkgutil.ImpImporter("") 
Example #4
Source File: path.py    From python-sploitkit with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, path):
        super(PyModulePath, self).__init__()
        self.is_pymodule = self.is_file() and \
                           lexer.analyse_text(self.open().read()) == 1.0
        if self.is_pymodule:
            self.module = ImpImporter(str(self.resolve().parent)) \
                          .find_module(self.stem).load_module(self.stem) 
Example #5
Source File: action_loader.py    From loaner with Apache License 2.0 4 votes vote down vote up
def load_actions(filters=None, log_exceptions=True):
  """Loads Actions from the actions directory, and instantiates them.

  Args:
    filters: list, strings with names of action classes to load. Loader will
        skip classes not listed. In the absence of this list no filters are
        applied.
    log_exceptions: bool, whether to supress exceptions and log their messages
        instead.

  Returns:
    A dictionary of actions, with their names as keys and instaniated Action
    classes as their values.

  Raises:
    AttributeError: if log_exceptions is False and Action classes are missing
        ACTION_NAME or FRIENDLY_NAME attributes, or the run method.
  """
  global _CACHED_ACTIONS
  if _CACHED_ACTIONS:
    return _CACHED_ACTIONS
  actions = {base_action.ActionType.SYNC: {}, base_action.ActionType.ASYNC: {}}
  importer = pkgutil.ImpImporter(os.path.abspath(
      os.path.join(os.path.dirname(__file__), '..', 'actions')))
  for module_name, module in importer.iter_modules():
    del module  # Not used.
    if module_name.endswith('_test') or module_name.startswith('base_action'):
      continue
    try:
      loaded_module = importer.find_module(module_name).load_module(module_name)
    except ImportError:
      logging.info('Error importing module %s', module_name)
      continue
    for obj_name, obj in inspect.getmembers(loaded_module):
      if inspect.isclass(obj) and issubclass(obj, base_action.BaseAction):
        if filters and obj.ACTION_NAME not in filters:
          continue
        # Defaults to async for backward compatibility.
        action_type = getattr(obj, 'ACTION_TYPE', base_action.ActionType.ASYNC)
        try:
          action = obj()
        except AttributeError as e:
          error_message = _INSTANTIATION_ERROR_MSG % (
              obj_name, module_name, e.message)
          if log_exceptions:
            logging.warning(error_message)
            continue
          else:
            raise AttributeError(error_message)
        if (
            action.ACTION_NAME in actions[base_action.ActionType.SYNC] or
            action.ACTION_NAME in actions[base_action.ActionType.ASYNC]):
          logging.warning(_DUPLICATE_ACTION_MSG, obj.ACTION_NAME)
          continue
        actions[action_type][action.ACTION_NAME] = action
  _CACHED_ACTIONS = actions
  return actions