Esempio n. 1
0
    def load_spider(self, name, params=None):
        spider_cls = None
        if name in self._spiders_cls:
            spider_cls = self._spiders_cls[name]
        else:
            for fn in os.listdir(self._path):
                if fn in ('.', '..'): continue
                fp = os.path.join(self._path, fn)
                if os.path.isfile(fp):
                    mod = loader.load_module(fp)
                    if mod and name in mod:
                        spider_cls = mod[name]
                        self._spiders_cls[name] = spider_cls

        if spider_cls:
            s = spider_cls()
            p = self._spiders_cfg.get(name, {})
            if params:
                p.update(params)
            if p:
                s.set_params(**p)
            
            return s

        return None
Esempio n. 2
0
    def load_projects(self, path):
        self._projs = {}
        for fn in os.listdir(path):
            if fn in ('.', '..'): continue
            proj_path = os.path.join(path, fn)
            if os.path.isdir(proj_path):
                config_file = os.path.join(proj_path, 'configs.py')
                if os.path.exists(config_file):
                    cfg = loader.load_module(config_file)
                    if cfg:
                        proj = Project(cfg, proj_path)
                        name = hasattr(cfg, 'name') and cfg.name or fn
                        self._projs[name] = proj

        self.load_jobs()
Esempio n. 3
0
def import_strategies(app_config):
    def remove_postfix(s):
        try:
            _id = re.match(r"(.*)_\d+$", s).group(1)
        except AttributeError as e:
            _id = s
        return _id

    log = logging.getLogger(__name__)
    strategy_modules = dict()
    strategy_classes = OrderedDict()

    try:
        strategy_labels = [
            ss for ss in app_config['STRATEGIES'].keys() if len(ss)
        ]
    except configparser.Error as e:
        raise e
    else:
        if not strategy_labels:
            raise NoStrategyFound(
                'No strategy found on configuration, pls specify at list one in section STRATEGIES'
            )
        else:
            for strategy_label in strategy_labels:
                strategy_id = remove_postfix(strategy_label)
                try:
                    if strategy_id not in strategy_modules:
                        strategy_modules[strategy_id] = load_module(
                            strategy_id)
                        log.debug(
                            'module {} for strategy <{}> succesfully added to cerebro'
                            .format(str(strategy_modules[strategy_id]),
                                    strategy_label))
                    else:
                        # TODO TEST : strategy_classes che valore ha qui ?
                        log.debug('module {} for strategy <{}> already loaded'.
                                  format(str(strategy_modules[strategy_id]),
                                         strategy_label))

                    strategy_classes[strategy_label] = strategy_modules[
                        strategy_id].get_strategy_class()

                except Exception as e:
                    raise e

    return strategy_classes
Esempio n. 4
0
async def load_mod(context, message, content):
    fq = loader.fq_from_leaf(content)

    if fq in sys.modules:
        return await context.reply("It's already loaded, nya.")

    bot = context.of("discordbot")

    try:
        mod = loader.load_module(content)
        await bot.init_module(mod)
    except Exception as e:
        await context.reply("Loading '{0}' failed with a {1}. Fix it and `load` the module again.".format(
            content, e.__class__.__name__))
        raise
    else:
        await context.reply("\u2705")
Esempio n. 5
0
    def __init__(self, config=None, name=None, fromdate=None, todate=None):

        self.name = name    # TODO controllare se None...

        self.log = logging.getLogger (__name__)
        self.log.info('__init__ Strategy <{}> <{}>'.format(repr(self.__class__), self.name))

        if fromdate is not None and isinstance(fromdate, datetime.date):
            self.fromdate = fromdate.strftime('%Y-%m-%d')
        else:
            self.log.error('Invalid fromdate par. to strategy {}'.format(self.name))
        if todate is not None and isinstance(todate, datetime.date):
            self.todate = todate.strftime('%Y-%m-%d')
        else:
            self.log.error('Invalid todate par. to strategy {}'.format(self.name))

        '''
        # TODO
        # dopo l'introduzione dei settings/conventions config qui ora è un dict
        if config is not None and isinstance(config, configparser.ConfigParser):
            try:
                configured_indicators = [_ind.strip() for _ind in config.get('STRATEGIES', name).split(',') if len(_ind)]
            except configparser.NoOptionError as e:
                # TODO possibile skip della stategia ?
                self.log.error('No indicators found for strategy <{}> : {}'.format(name, e))
                sys.exit(1)
            try:
                self.parquet_storage       = config.get('STORAGE', 'parquet')
            except configparser.NoOptionError as e:
                self.log.error('Missing option "parquet" in section "STORAGE" : fix it in order to save indicators result') 
                self.parquet_storage = None
                # raise ?

        else:
            raise (StrategyInizializationFailed('invalid **kwarg params passed to {} instance : config=<{}>, name=<{}>, fromdate=<{}>, todate=<{}>' \
                    .format( repr(self.__class__), type(config), type(name), type(fromdate), type(todate) )))
        '''
        if config is not None and isinstance(config, dict):
            configured_indicators = [_ind.strip() for _ind in config['STRATEGIES'][self.name] if len(_ind)]
            if not len(configured_indicators):
                self.log.error('No indicators found for strategy <{}> : {}'.format(self.name, e))
                sys.exit(1)
            self.parquet_storage = config['STORAGE']['parquet']
            if not self.parquet_storage:
                self.log.error('Missing option "parquet" in section "STORAGE" : fix it in order to save indicators result')
                sys.exit(1)
        else:
            raise (StrategyInizializationFailed('invalid **kwarg params passed to {} instance : config=<{}>, name=<{}>, fromdate=<{}>, todate=<{}>' \
                    .format( repr(self.__class__), type(config), type(name), type(fromdate), type(todate) )))

        self.loop_count     = 0
        self.indicators     = dict() ##

        for i_name in configured_indicators:
            try:
                self.indicators[i_name] = dict()
                _mod        = self.indicators[i_name]['__module__'] = load_module(i_name) # .. può restituire direttamente l'istanza dell'indicatore?
                ind_class   = self.indicators[i_name]['__class__']  = _mod.get_indicator_class()

                for _, datafeed in enumerate(self.datas):
                    self.indicators[i_name][datafeed._name] = dict()
                    self.indicators[i_name][datafeed._name]['indicator_instance'] = ind_class(datafeed, strategy=self)
                    self.indicators[i_name][datafeed._name]['output_dataframe']   = pd.DataFrame()

            except Exception as e:  # ModuleNotFoundError ...
                # gli indicatori non validi sono scartati e non pregiudicano l'esecuzione
                self.log.error(e)