Beispiel #1
0
    def load_module(self, module_name):
        if module_name in self.loaded:
            self.logger.debug(
                'Module "%s" is already loaded from %s' %
                (module_name, self.loaded[module_name].package.__path__[0]))
            return

        path = self.get_module_path(module_name)

        try:
            fp, pathname, description = imp.find_module(module_name, [path])
            try:
                module = LoadedModule(
                    imp.load_module(module_name, fp, pathname, description))
            finally:
                if fp:
                    fp.close()
        except Exception as e:
            if logging.root.level <= logging.DEBUG:
                self.logger.exception(e)
            raise ModuleLoadError(module_name, e)

        if module.version != self.version:
            raise ModuleLoadError(
                module_name,
                "Module requires Weboob %s, but you use Weboob %s. Hint: use 'weboob-config update'"
                % (module.version, self.version))

        self.loaded[module_name] = module
        self.logger.debug('Loaded module "%s" from %s' %
                          (module_name, module.package.__path__[0]))
Beispiel #2
0
    def get_module_path(self, module_name):
        minfo = self.repositories.get_module_info(module_name)
        if minfo is None:
            raise ModuleLoadError(module_name, 'No such module %s' % module_name)
        if minfo.path is None:
            raise ModuleLoadError(module_name, 'Module %s is not installed' % module_name)

        return minfo.path
Beispiel #3
0
    def build_backend(self,
                      module_name,
                      params=None,
                      storage=None,
                      name=None,
                      nofail=False):
        """
        Create a single backend which is not listed in configuration.

        :param module_name: name of module
        :param params: parameters to give to backend
        :type params: :class:`dict`
        :param storage: storage to use
        :type storage: :class:`weboob.tools.storage.IStorage`
        :param name: name of backend
        :type name: :class:`basestring`
        :rtype: :class:`weboob.tools.backend.Module`
        :param nofail: if true, this call can't fail
        :type nofail: :class:`bool`
        """
        minfo = self.repositories.get_module_info(module_name)
        if minfo is None:
            raise ModuleLoadError(module_name, 'Module does not exist.')

        if not minfo.is_installed():
            self.repositories.install(minfo)

        return super(Weboob, self).build_backend(module_name, params, storage,
                                                 name, nofail)
Beispiel #4
0
    def random_errors(rate):
        """
        Generate random errors, at a given rate.

        :param rate: Rate at which errors should be generated.
        """
        n = random.randrange(100)

        # Once per module instanciation, try a 2% rate import error.
        global TriedImportError  # pylint: disable=global-statement
        if not TriedImportError:
            TriedImportError = True
            if n < 2:
                import NotExistingModule  # pylint: disable=import-error,unused-variable

        # With a probability of rate%, raise an exception.
        if n >= rate:
            return

        possible_errors = [
            ActionNeeded,
            BrowserIncorrectPassword,
            BrowserPasswordExpired,
            NoAccountsException,
            ModuleInstallError,
            NotImplementedError,
            GenericException,
            ModuleLoadError('FakeWeboobBank', 'Random error'),
            Exception,
        ]

        raise possible_errors[random.randrange(len(possible_errors))]
Beispiel #5
0
    def add_backend(self,
                    module_name,
                    backend_name,
                    params=None,
                    edit=False,
                    ask_register=True):
        if params is None:
            params = {}

        module = None
        config = None
        try:
            if not edit:
                minfo = self.weboob.repositories.get_module_info(module_name)
                if minfo is None:
                    raise ModuleLoadError(module_name, 'Module does not exist')
                if not minfo.is_installed():
                    print('Module "%s" is available but not installed.' %
                          minfo.name)
                    self.install_module(minfo)
                module = self.weboob.modules_loader.get_or_load_module(
                    module_name)
                config = module.config
            else:
                module_name, items = self.weboob.backends_config.get_backend(
                    backend_name)
                module = self.weboob.modules_loader.get_or_load_module(
                    module_name)
                items.update(params)
                params = items
                config = module.config.load(self.weboob,
                                            module_name,
                                            backend_name,
                                            params,
                                            nofail=True)
        except ModuleLoadError as e:
            print('Unable to load module "%s": %s' % (module_name, e),
                  file=self.stderr)
            return 1

        # ask for params non-specified on command-line arguments
        asked_config = False
        for key, value in config.items():
            if value.transient:
                continue

            if not asked_config:
                asked_config = True
                print('')
                print('Configuration of backend %s' % module.name)
                print('-------------------------%s' % ('-' * len(module.name)))
            if key not in params or edit:
                params[key] = self.ask(value,
                                       default=params[key] if
                                       (key in params) else value.default)
            else:
                print(
                    u'[%s] %s: %s' %
                    (key, value.description,
                     '(masked)' if value.masked else to_unicode(params[key])))
        if asked_config:
            print('-------------------------%s' % ('-' * len(module.name)))

        i = 2
        while not edit and self.weboob.backends_config.backend_exists(
                backend_name):
            if not self.ask(
                    'Backend "%s" already exists. Add a new one for module %s?'
                    % (backend_name, module.name),
                    default=False):
                return 1

            backend_name = backend_name.rstrip('0123456789')
            while self.weboob.backends_config.backend_exists(
                    '%s%s' % (backend_name, i)):
                i += 1
            backend_name = self.ask('Please give new instance name',
                                    default='%s%s' % (backend_name, i),
                                    regexp=r'^[\w\-_]+$')

        try:
            config = config.load(self.weboob,
                                 module.name,
                                 backend_name,
                                 params,
                                 nofail=True)
            for key, value in params.items():
                if key not in config:
                    continue
                config[key].set(value)

            config.save(edit=edit)
            print('Backend "%s" successfully %s.' %
                  (backend_name, 'edited' if edit else 'added'))
            return backend_name
        except BackendAlreadyExists:
            print('Backend "%s" already exists.' % backend_name,
                  file=self.stderr)
            return 1