Python sys.modules() Examples

The following are 40 code examples for showing how to use sys.modules(). These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.

You may also want to check out all available functions/classes of the module sys , or try the search function .

Example 1
Project: apm-python-agent-principle   Author: mozillazg   File: _hook.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        print('load_module {}'.format(fullname))
        if fullname in sys.modules:
            return sys.modules[fullname]

        # 先从 sys.meta_path 中删除自定义的 finder
        # 防止下面执行 import_module 的时候再次触发此 finder
        # 从而出现递归调用的问题
        finder = sys.meta_path.pop(0)
        # 导入 module
        module = importlib.import_module(fullname)

        module_hook(fullname, module)

        sys.meta_path.insert(0, finder)
        return module 
Example 2
Project: apm-python-agent-principle   Author: mozillazg   File: hook.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        print('load_module {}'.format(fullname))
        if fullname in sys.modules:
            return sys.modules[fullname]

        # 先从 sys.meta_path 中删除自定义的 finder
        # 防止下面执行 import_module 的时候再次触发此 finder
        # 从而出现递归调用的问题
        finder = sys.meta_path.pop(0)
        # 导入 module
        module = importlib.import_module(fullname)

        module_hook(fullname, module)

        sys.meta_path.insert(0, finder)
        return module 
Example 3
Project: cherrypy   Author: cherrypy   File: conf.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def mock_pywin32():
    """Mock pywin32 module.

    Resulting in Linux hosts, including ReadTheDocs,
    and other environments that don't have pywin32 can generate the docs
    properly including the PDF version.
    See:
    http://read-the-docs.readthedocs.org/en/latest/faq.html#i-get-import-errors-on-libraries-that-depend-on-c-modules
    """
    if try_import('win32api'):
        return

    from unittest import mock

    MOCK_MODULES = [
        'win32api', 'win32con', 'win32event', 'win32service',
        'win32serviceutil',
    ]
    for mod_name in MOCK_MODULES:
        sys.modules[mod_name] = mock.MagicMock() 
Example 4
Project: cherrypy   Author: cherrypy   File: reprconf.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def build_Name(self, o):
        name = o.id
        if name == 'None':
            return None
        if name == 'True':
            return True
        if name == 'False':
            return False

        # See if the Name is a package or module. If it is, import it.
        try:
            return modules(name)
        except ImportError:
            pass

        # See if the Name is in builtins.
        try:
            return getattr(builtins, name)
        except AttributeError:
            pass

        raise TypeError('unrepr could not resolve the name %s' % repr(name)) 
Example 5
Project: cherrypy   Author: cherrypy   File: reprconf.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def attributes(full_attribute_name):
    """Load a module and retrieve an attribute of that module."""

    # Parse out the path, module, and attribute
    last_dot = full_attribute_name.rfind('.')
    attr_name = full_attribute_name[last_dot + 1:]
    mod_path = full_attribute_name[:last_dot]

    mod = modules(mod_path)
    # Let an AttributeError propagate outward.
    try:
        attr = getattr(mod, attr_name)
    except AttributeError:
        raise AttributeError("'%s' object has no attribute '%s'"
                             % (mod_path, attr_name))

    # Return a reference to the attribute.
    return attr 
Example 6
Project: django-click   Author: GaretJax   File: adapter.py    License: MIT License 6 votes vote down vote up
def __call__(self, func):
        module = sys.modules[func.__module__]

        # Get the command name as Django expects it
        self.name = func.__module__.rsplit(".", 1)[-1]

        # Build the click command
        decorators = [
            click.command(name=self.name, cls=self.cls, **self.kwargs),
        ] + self.get_params(self.name)

        for decorator in reversed(decorators):
            func = decorator(func)

        # Django expects the command to be callable (it instantiates the class
        # pointed at by the `Command` module-level property)...
        # ...let's make it happy.
        module.Command = lambda: func

        return func 
Example 7
Project: friendly-telegram   Author: friendly-telegram   File: loader.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def unloadmodcmd(self, message):
        """Unload module by class name"""
        args = utils.get_args(message)
        if not args:
            await utils.answer(message, self.strings["what_class"])
            return
        clazz = args[0]
        worked = self.allmodules.unload_module(clazz)
        without_prefix = []
        for mod in worked:
            assert mod.startswith("friendly-telegram.modules."), mod
            without_prefix += [unescape_percent(mod[len("friendly-telegram.modules."):])]
        it = set(self._db.get(__name__, "loaded_modules", [])).difference(without_prefix)
        self._db.set(__name__, "loaded_modules", list(it))
        it = set(self._db.get(__name__, "unloaded_modules", [])).union(without_prefix)
        self._db.set(__name__, "unloaded_modules", list(it))
        if worked:
            await utils.answer(message, self.strings["unloaded"])
        else:
            await utils.answer(message, self.strings["not_unloaded"]) 
Example 8
Project: neuropythy   Author: noahbenson   File: __init__.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def reload_neuropythy():
    '''
    reload_neuropythy() reloads all of the modules of neuropythy and returns the reloaded
    neuropythy module. This is similar to reload(neuropythy) except that it reloads all the
    neuropythy submodules prior to reloading neuropythy.

    Example:
      import neuropythy as ny
      # ... some nonsense that breaks the library ...
      ny = ny.reload_neuropythy()
    '''
    import sys, six
    if not six.PY2:
        try:              from importlib import reload
        except Exception: from imp import reload
    for mdl in submodules:
        if mdl in sys.modules:
            sys.modules[mdl] = reload(sys.modules[mdl])
    return reload(sys.modules['neuropythy']) 
Example 9
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: torch.py    License: Apache License 2.0 6 votes vote down vote up
def _init_torch_module():
    """List and add all the torch backed ndarray functions to current module."""
    plist = ctypes.POINTER(FunctionHandle)()
    size = ctypes.c_uint()
    check_call(_LIB.MXListFunctions(ctypes.byref(size),
                                    ctypes.byref(plist)))

    module_obj = sys.modules[__name__]
    for i in range(size.value):
        hdl = FunctionHandle(plist[i])
        function = _make_torch_function(hdl)
        # if function name starts with underscore, register as static method of NDArray
        if function is not None:
            setattr(module_obj, function.__name__, function)

# Initialize the NDArray module 
Example 10
Project: Paradrop   Author: ParadropLabs   File: settings.py    License: Apache License 2.0 6 votes vote down vote up
def iterate_module_attributes(module):
    """
    Iterate over the attributes in a module.

    This is a generator function.

    Returns (name, value) tuples.
    """
    for name in dir(module):
        # Ignore fields marked as private or builtin.
        if name.startswith('_'):
            continue

        value = getattr(module, name)

        # Ignore callable objects (functions) and loaded modules.
        if callable(value) or isinstance(value, types.ModuleType):
            continue

        yield (name, value) 
Example 11
Project: Paradrop   Author: ParadropLabs   File: settings.py    License: Apache License 2.0 6 votes vote down vote up
def load_from_file(path):
    """
    Load settings from an INI file.

    This will check the configuration file for a lowercase version of all of
    the settings in this module. It will look in a section called "base".

    The example below will set PORTAL_SERVER_PORT.

        [base]
        portal_server_port = 4444
    """
    config = configparser.SafeConfigParser()
    config.read(path)

    mod = sys.modules[__name__]
    for name, _ in iterate_module_attributes(mod):
        # Check if lowercase version exists in the file and load the
        # appropriately-typed value.
        key = name.lower()
        if config.has_option(constants.BASE_SETTINGS_SECTION, key):
            value = config.get(constants.BASE_SETTINGS_SECTION, key)
            setattr(mod, name, parseValue(value)) 
Example 12
Project: Paradrop   Author: ParadropLabs   File: settings.py    License: Apache License 2.0 6 votes vote down vote up
def updatePaths(configHomeDir, runtimeHomeDir="/var/run/paradrop"):
    # Get a handle to our settings defined above
    mod = sys.modules[__name__]

    mod.CONFIG_HOME_DIR = configHomeDir
    mod.RUNTIME_HOME_DIR = runtimeHomeDir
    mod.FC_CHUTESTORAGE_FILE = os.path.join(mod.CONFIG_HOME_DIR, "chutes")
    mod.EXTERNAL_DATA_DIR = os.path.join(mod.CONFIG_HOME_DIR, "chute-data/{chute}/")
    mod.EXTERNAL_SYSTEM_DIR = os.path.join(runtimeHomeDir, "system", "{chute}")
    mod.LOG_DIR = os.path.join(mod.CONFIG_HOME_DIR, "logs/")
    mod.KEY_DIR = os.path.join(mod.CONFIG_HOME_DIR, "keys/")
    mod.MISC_DIR = os.path.join(mod.CONFIG_HOME_DIR, "misc/")
    mod.CONFIG_FILE = os.path.join(mod.CONFIG_HOME_DIR, "config")
    mod.HOST_CONFIG_FILE = os.path.join(mod.CONFIG_HOME_DIR, "hostconfig.yaml")
    mod.DEFAULT_HOST_CONFIG_FILE = os.path.join(mod.CONFIG_HOME_DIR, "hostconfig.default.yaml")
    mod.UCI_CONFIG_DIR = os.path.join(mod.CONFIG_HOME_DIR, "uci/config.d/")
    mod.UCI_BACKUP_DIR = os.path.join(mod.CONFIG_HOME_DIR, "uci/config-backup.d/")
    mod.PDCONFD_WRITE_DIR = os.path.join(mod.RUNTIME_HOME_DIR, 'pdconfd') 
Example 13
Project: calmjs   Author: calmjs   File: test_artifact.py    License: GNU General Public License v2.0 6 votes vote down vote up
def test_iter_builders_side_effect(self):
        # inject dummy module and add cleanup
        mod = ModuleType('calmjs_testing_dummy')
        mod.complete = generic_builder
        self.addCleanup(sys.modules.pop, 'calmjs_testing_dummy')
        sys.modules['calmjs_testing_dummy'] = mod

        working_dir = utils.mkdtemp(self)
        utils.make_dummy_dist(self, (
            ('entry_points.txt', '\n'.join([
                '[calmjs.artifacts]',
                'artifact.js = calmjs_testing_dummy:complete',
            ])),
        ), 'app', '1.0', working_dir=working_dir)
        mock_ws = WorkingSet([working_dir])
        registry = ArtifactRegistry('calmjs.artifacts', _working_set=mock_ws)
        registry.update_artifact_metadata('app', {})

        root = join(working_dir, 'app-1.0.egg-info', 'calmjs_artifacts')
        self.assertFalse(exists(root))
        ep, toolchain, spec = next(registry.iter_builders_for('app'))
        self.assertFalse(exists(root))
        # directory only created after the toolchain is executed
        toolchain(spec)
        self.assertTrue(exists(root)) 
Example 14
Project: calmjs   Author: calmjs   File: test_indexer.py    License: GNU General Public License v2.0 6 votes vote down vote up
def test_missing_distribution(self):
        d_egg_root = join(mkdtemp(self), 'dummyns')
        make_dummy_dist(self, ((
            'namespace_packages.txt',
            'not_ns\n',
        ), (
            'entry_points.txt',
            '[dummyns]\n'
            'dummyns = dummyns:attr\n',
        ),), 'dummyns', '2.0', working_dir=d_egg_root)
        working_set = pkg_resources.WorkingSet([
            d_egg_root,
            self.ds_egg_root,
        ])
        dummyns_ep = next(working_set.iter_entry_points('dummyns'))
        with pretty_logging(stream=StringIO()) as fd:
            p = indexer.resource_filename_mod_entry_point(
                'dummyns', dummyns_ep)
        # not stubbed working_set, so this is derived using fallback
        # value from the sys.modules['dummyns'] location
        self.assertEqual(normcase(p), normcase(self.dummyns_path))
        self.assertIn("distribution 'dummyns 2.0' not found", fd.getvalue()) 
Example 15
Project: quart   Author: pgjones   File: _patch.py    License: MIT License 6 votes vote down vote up
def _patch_modules() -> None:
    if "flask" in sys.modules:
        raise ImportError("Cannot mock flask, already imported")

    # Create a set of Flask modules, prioritising those within the
    # flask_patch namespace over simple references to the Quart
    # versions.
    flask_modules = {}
    for name, module in list(sys.modules.items()):
        if name.startswith("quart.flask_patch._"):
            continue
        elif name.startswith("quart.flask_patch"):
            setattr(module, "_QUART_PATCHED", True)
            flask_modules[name.replace("quart.flask_patch", "flask")] = module
        elif name.startswith("quart.") and not name.startswith("quart.serving"):
            flask_name = name.replace("quart.", "flask.")
            if flask_name not in flask_modules:
                flask_modules[flask_name] = _convert_module(flask_name, module)

    sys.modules.update(flask_modules) 
Example 16
Project: quart   Author: pgjones   File: helpers.py    License: MIT License 6 votes vote down vote up
def find_package(name: str) -> Tuple[Optional[Path], Path]:
    """Finds packages install prefix (or None) and it's containing Folder
    """
    module = name.split(".")[0]
    loader = pkgutil.get_loader(module)
    if name == "__main__" or loader is None:
        package_path = Path.cwd()
    else:
        if hasattr(loader, "get_filename"):
            filename = loader.get_filename(module)  # type: ignore
        else:
            __import__(name)
            filename = sys.modules[name].__file__
        package_path = Path(filename).resolve().parent
        if hasattr(loader, "is_package"):
            is_package = loader.is_package(module)  # type: ignore
            if is_package:
                package_path = Path(package_path).resolve().parent
    sys_prefix = Path(sys.prefix).resolve()
    try:
        package_path.relative_to(sys_prefix)
    except ValueError:
        return None, package_path
    else:
        return sys_prefix, package_path 
Example 17
Project: EXOSIMS   Author: dsavransky   File: keplerSTM_indprop.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, x0, mu, epsmult = 4.0, noc = False):
        #determine number of planets and validate input
        nplanets = x0.size/6.
        if (nplanets - np.floor(nplanets) > 0):
            raise Exception('The length of x0 must be a multiple of 6.')
        
        if (mu.size != nplanets):
            raise Exception('The length of mu must be the length of x0 divided by 6')
        
        self.nplanets = int(nplanets)
        self.mu = np.squeeze(mu)
        if (self.mu.size == 1):
            self.mu = np.array(mu)
        
        self.epsmult = epsmult
        
        if not(noc) and ('EXOSIMS.util.KeplerSTM_C.CyKeplerSTM' in sys.modules):
            self.havec = True
            self.x0 = np.squeeze(x0)
        else:
            self.havec = False
            self.updateState(np.squeeze(x0)) 
Example 18
Project: glazier   Author: google   File: interact_test.py    License: Apache License 2.0 6 votes vote down vote up
def testKeystroke(self, sleep):
    msvcrt = mock.Mock()
    msvcrt.kbhit.return_value = False
    sys.modules['msvcrt'] = msvcrt
    # no reply
    result = interact.Keystroke('mesg', timeout=1)
    self.assertEqual(result, None)
    self.assertEqual(sleep.call_count, 1)
    # special character reply
    msvcrt.kbhit.side_effect = iter([False, False, False, False, True])
    msvcrt.getch.return_value = b'0xe0'
    result = interact.Keystroke('mesg', timeout=100)
    self.assertEqual(result, '0xe0')
    self.assertEqual(sleep.call_count, 6)
    # reply
    msvcrt.kbhit.side_effect = iter([False, False, False, False, True])
    msvcrt.getch.return_value = b'v'
    result = interact.Keystroke('mesg', timeout=100)
    self.assertEqual(result, 'v')
    self.assertEqual(sleep.call_count, 11)
    # validation miss
    msvcrt.kbhit.side_effect = iter([True])
    result = interact.Keystroke('mesg', validator='[0-9]')
    self.assertEqual(result, None) 
Example 19
Project: sanic   Author: huge-success   File: reloader_helpers.py    License: MIT License 6 votes vote down vote up
def _iter_module_files():
    """This iterates over all relevant Python files.

    It goes through all
    loaded files from modules, all files in folders of already loaded modules
    as well as all files reachable through a package.
    """
    # The list call is necessary on Python 3 in case the module
    # dictionary modifies during iteration.
    for module in list(sys.modules.values()):
        if module is None:
            continue
        filename = getattr(module, "__file__", None)
        if filename:
            old = None
            while not os.path.isfile(filename):
                old = filename
                filename = os.path.dirname(filename)
                if filename == old:
                    break
            else:
                if filename[-4:] in (".pyc", ".pyo"):
                    filename = filename[:-1]
                yield filename 
Example 20
Project: pywren-ibm-cloud   Author: pywren   File: cloudpickle.py    License: Apache License 2.0 6 votes vote down vote up
def _whichmodule(obj, name):
    """Find the module an object belongs to.

    This function differs from ``pickle.whichmodule`` in two ways:
    - it does not mangle the cases where obj's module is __main__ and obj was
      not found in any module.
    - Errors arising during module introspection are ignored, as those errors
      are considered unwanted side effects.
    """
    module_name = getattr(obj, '__module__', None)
    if module_name is not None:
        return module_name
    # Protect the iteration by using a list copy of sys.modules against dynamic
    # modules that trigger imports of other modules upon calls to getattr.
    for module_name, module in list(sys.modules.items()):
        if module_name == '__main__' or module is None:
            continue
        try:
            if _getattribute(module, name)[0] is obj:
                return module_name
        except Exception:
            pass
    return None 
Example 21
Project: jawfish   Author: war-and-code   File: suite.py    License: MIT License 6 votes vote down vote up
def _handleModuleTearDown(self, result):
        previousModule = self._get_previous_module(result)
        if previousModule is None:
            return
        if result._moduleSetUpFailed:
            return

        try:
            module = sys.modules[previousModule]
        except KeyError:
            return

        tearDownModule = getattr(module, 'tearDownModule', None)
        if tearDownModule is not None:
            _call_if_exists(result, '_setupStdout')
            try:
                tearDownModule()
            except Exception as e:
                if isinstance(result, _DebugResult):
                    raise
                errorName = 'tearDownModule (%s)' % previousModule
                self._addClassOrModuleLevelException(result, e, errorName)
            finally:
                _call_if_exists(result, '_restoreStdout') 
Example 22
Project: unicorn-hat-hd   Author: pimoroni   File: test_setup.py    License: MIT License 5 votes vote down vote up
def test_setup():
    """Test that the library sets up correctly with numpy and spidev."""
    sys.modules['spidev'] = mock.MagicMock()
    import unicornhathd
    unicornhathd.setup() 
Example 23
Project: unicorn-hat-hd   Author: pimoroni   File: test_setup.py    License: MIT License 5 votes vote down vote up
def test_brightness():
    """Test brightness API change and alias."""
    sys.modules['spidev'] = mock.MagicMock()
    import unicornhathd
    unicornhathd.setup()

    unicornhathd.set_brightness(0.5)
    assert unicornhathd.set_brightness == unicornhathd.brightness 
Example 24
Project: unicorn-hat-hd   Author: pimoroni   File: test_setup.py    License: MIT License 5 votes vote down vote up
def test_rotation():
    """Test rotation API change and alias."""
    sys.modules['spidev'] = mock.MagicMock()
    import unicornhathd
    unicornhathd.setup()

    unicornhathd.set_rotation(90)
    assert unicornhathd.set_rotation == unicornhathd.rotation 
Example 25
Project: unicorn-hat-hd   Author: pimoroni   File: test_setup.py    License: MIT License 5 votes vote down vote up
def test_tuple_colour():
    """Test valid text colour."""
    sys.modules['spidev'] = mock.MagicMock()
    import unicornhathd
    unicornhathd.setup()

    unicornhathd.set_pixel(0, 0, (255, 0, 0))

    assert tuple(unicornhathd._buf[0, 0]) == (255, 0, 0) 
Example 26
Project: unicorn-hat-hd   Author: pimoroni   File: test_setup.py    License: MIT License 5 votes vote down vote up
def test_valid_text_colour():
    """Test valid text colour."""
    sys.modules['spidev'] = mock.MagicMock()
    import unicornhathd
    unicornhathd.setup()

    unicornhathd.set_pixel(0, 0, "Teal")

    assert tuple(unicornhathd._buf[0, 0]) == unicornhathd.COLORS['teal'] 
Example 27
Project: apm-python-agent-principle   Author: mozillazg   File: hook.py    License: MIT License 5 votes vote down vote up
def load_module(self, fullname):
        print('load_module {}'.format(fullname))
        if fullname in sys.modules:
            return sys.modules[fullname]

        # 先从 sys.meta_path 中删除自定义的 finder
        # 防止下面执行 import_module 的时候再次触发此 finder
        # 从而出现递归调用的问题
        finder = sys.meta_path.pop(0)
        # 导入 module
        module = importlib.import_module(fullname)

        module_hook(fullname, module)

        sys.meta_path.insert(0, finder)
        return module 
Example 28
Project: apm-python-agent-principle   Author: mozillazg   File: meta_path1.py    License: MIT License 5 votes vote down vote up
def load_module(self, fullname):
        print('load_module {}'.format(fullname))
        sys.modules[fullname] = sys
        return sys 
Example 29
Project: cherrypy   Author: cherrypy   File: plugins.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def sysfiles(self):
        """Return a Set of sys.modules filenames to monitor."""
        search_mod_names = filter(
            re.compile(self.match).match,
            list(sys.modules.keys()),
        )
        mods = map(sys.modules.get, search_mod_names)
        return set(filter(None, map(self._file_for_module, mods))) 
Example 30
Project: cherrypy   Author: cherrypy   File: reprconf.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def modules(modulePath):
    """Load a module and retrieve a reference to that module."""
    __import__(modulePath)
    return sys.modules[modulePath] 
Example 31
Project: python-clean-architecture   Author: pcah   File: imports.py    License: MIT License 5 votes vote down vote up
def import_all_names(_file, _name):
    """
    Util for a tricky dynamic import of all names from all submodules.
    Use it in the __init__.py using following idiom:

        import_all_names(__file__, __name__)

    Supports __all__ attribute of the submodules.
    """
    path = os.path.dirname(os.path.abspath(_file))
    parent_module = sys.modules[_name]

    dir_list = []
    for py in [filename[:-3] for filename in os.listdir(path)
               if filename.endswith('.py') and filename != '__init__.py']:
        module = __import__('.'.join([_name, py]), fromlist=[py])
        module_names = getattr(module, '__all__', None) or dir(module)
        objects = dict(
            (name, getattr(module, name))
            for name in module_names
            if not name.startswith('_')
        )
        for name, obj in objects.items():
            if hasattr(parent_module, name) and \
               getattr(parent_module, name) is not obj:
                msg = (
                    "Function import_all_names hit upon conflicting "
                    "names. '{0}' is already imported to {1} module."
                ).format(name, module)
                import warnings
                warnings.warn(msg)
            setattr(parent_module, name, obj)
        dir_list.extend(objects)
    parent_module.__dir__ = lambda: dir_list


# noinspection PyUnboundLocalVariable 
Example 32
Project: friendly-telegram   Author: friendly-telegram   File: loader.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self):
        super().__init__()
        self.config = loader.ModuleConfig("MODULES_REPO",
                                          "https://gitlab.com/friendly-telegram/modules-repo/-/raw/master",
                                          lambda: self.strings["repo_config_doc"])
        self._pending_setup = [] 
Example 33
Project: friendly-telegram   Author: friendly-telegram   File: loader.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def load_module(self, doc, message, name=None, origin="<string>"):
        if name is None:
            uid = "__extmod_" + str(uuid.uuid4())
        else:
            uid = name.replace("%", "%%").replace(".", "%d")
        module_name = "friendly-telegram.modules." + uid
        try:
            module = importlib.util.module_from_spec(ModuleSpec(module_name, StringLoader(doc, origin), origin=origin))
            sys.modules[module_name] = module
            module.borg = uniborg.UniborgClient(module_name)
            module._ = _  # noqa: F821
            module.__spec__.loader.exec_module(module)
        except Exception:  # That's okay because it might try to exit or something, who knows.
            logger.exception("Loading external module failed.")
            if message is not None:
                await utils.answer(message, self.strings["load_failed"])
            return False
        if "register" not in vars(module):
            if message is not None:
                await utils.answer(message, self.strings["load_failed"])
            logging.error("Module does not have register(), it has " + repr(vars(module)))
            return False
        try:
            try:
                module.register(self.register_and_configure, module_name)
            except TypeError:
                module.register(self.register_and_configure)
            await self._pending_setup.pop()
        except Exception:
            logger.exception("Module threw")
            if message is not None:
                await utils.answer(message, self.strings["load_failed"])
            return False
        if message is not None:
            await utils.answer(message, self.strings["loaded"])
        return True 
Example 34
Project: friendly-telegram   Author: friendly-telegram   File: uniborg.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, module_name):
        self.instance_id = -1
        self._storage = None
        self._config = UniborgConfig()
        self._commands = {}
        self._watchers = []
        self._unknowns = []
        self._wrapper = None  # Set in registerfunc
        self._module = module_name
        sys.modules[self._module].__dict__["logger"] = logging.getLogger(self._module)
        sys.modules[self._module].__dict__["storage"] = self._storage
        sys.modules[self._module].__dict__["Config"] = self._config 
Example 35
Project: friendly-telegram   Author: friendly-telegram   File: loader.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.commands = {}
        self.aliases = {}
        self.modules = []
        self.watchers = []
        self._compat_layer = None
        self._log_handlers = []
        self.instances.append(self)
        self.client = None 
Example 36
Project: friendly-telegram   Author: friendly-telegram   File: loader.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def complete_registration(self, instance):
        """Complete registration of instance"""
        # Mainly for the Help module
        instance.allmodules = self
        # And for Remote
        instance.allloaders = self.instances
        instance.log = self.log  # Like botlog from PP
        for module in self.modules:
            if module.__class__.__name__ == instance.__class__.__name__:
                logging.debug("Removing module for update %r", module)
                self.modules.remove(module)
        self.modules += [instance] 
Example 37
Project: friendly-telegram   Author: friendly-telegram   File: loader.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def send_config(self, db, babel, skip_hook=False):
        """Configure modules"""
        for mod in self.modules:
            self.send_config_one(mod, db, babel, skip_hook) 
Example 38
Project: friendly-telegram   Author: friendly-telegram   File: loader.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def send_ready(self, client, db, allclients):
        """Send all data to all modules"""
        self.client = client
        await self._compat_layer.client_ready(client)
        try:
            await asyncio.gather(*[self.send_ready_one(mod, client, db, allclients, True) for mod in self.modules])
            await asyncio.gather(*[mod._client_ready2(client, db) for mod in self.modules])  # pylint: disable=W0212
        except Exception:
            logging.exception("Failed to send mod init complete signal") 
Example 39
Project: friendly-telegram   Author: friendly-telegram   File: loader.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def get_classname(self, name):
        for module in reversed(self.modules):
            if name in (module.name, module.__class__.__module__):
                return module.__class__.__module__
        return name 
Example 40
Project: friendly-telegram   Author: friendly-telegram   File: loader.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def unload_module(self, classname):
        """Remove module and all stuff from it"""
        worked = []
        to_remove = []
        for module in self.modules:
            if classname in (module.name, module.__class__.__name__):
                worked += [module.__module__]
                logging.debug("Removing module for unload %r", module)
                self.modules.remove(module)
                to_remove += module.commands.values()
                if hasattr(module, "watcher"):
                    to_remove += [module.watcher]
        logging.debug("to_remove: %r", to_remove)
        for watcher in self.watchers.copy():
            if watcher in to_remove:
                logging.debug("Removing watcher for unload %r", watcher)
                self.watchers.remove(watcher)
        aliases_to_remove = []
        for name, command in self.commands.copy().items():
            if command in to_remove:
                logging.debug("Removing command for unload %r", command)
                del self.commands[name]
                aliases_to_remove.append(name)
        for alias, command in self.aliases.copy().items():
            if command in aliases_to_remove:
                del self.aliases[alias]
        return worked