示例#1
0
    def formatter(record, handler):

        log = "[{formatter_tag}] [{time}] {level}: {msg}".format(
            formatter_tag=tag,
            level=record.level_name,
            msg=to_utf8(record.message),
            time=record.time,
        )

        if record.formatted_exception:
            log += "\n" + record.formatted_exception
        return log
示例#2
0
def user_std_handler_log_formatter(record, handler):
    from rqrobot.environment import Environment
    try:
        dt = Environment.get_instance().calendar_dt.strftime(DATETIME_FORMAT)
    except Exception:
        dt = datetime.now().strftime(DATETIME_FORMAT)

    log = "[{dt}][{level}][{filename}][{lineno}]: {msg}".format(
        dt=dt,
        level=record.level_name,
        filename=os.path.split(record.filename)[-1],
        lineno=record.lineno,
        msg=to_utf8(record.message),
    )
    return log
示例#3
0
def gettext(message):
    # trans_txt = localization.trans.gettext(message)
    return to_utf8(message)
示例#4
0
def parse_config(config_args,
                 config_path=None,
                 click_type=False,
                 source_code=None,
                 user_funcs=None):
    conf = default_config()
    deep_update(user_config(), conf)
    deep_update(project_config(), conf)
    if config_path is not None:
        deep_update(load_yaml(config_path), conf)

    if 'base__strategy_file' in config_args and config_args[
            'base__strategy_file']:
        # FIXME: ugly, we need this to get code
        conf['base']['strategy_file'] = config_args['base__strategy_file']
    elif ('base' in config_args and 'strategy_file' in config_args['base']
          and config_args['base']['strategy_file']):
        conf['base']['strategy_file'] = config_args['base']['strategy_file']

    if user_funcs is None:
        for k, v in six.iteritems(code_config(conf, source_code)):
            if k in conf['whitelist']:
                deep_update(v, conf[k])

    mod_configs = config_args.pop('mod_configs', [])
    for k, v in mod_configs:
        key = 'mod__{}'.format(k.replace('.', '__'))
        config_args[key] = mod_config_value_parse(v)

    if click_type:
        for k, v in six.iteritems(config_args):
            if v is None:
                continue
            if k == 'base__accounts' and not v:
                continue

            key_path = k.split('__')
            sub_dict = conf
            for p in key_path[:-1]:
                if p not in sub_dict:
                    sub_dict[p] = {}
                sub_dict = sub_dict[p]
            sub_dict[key_path[-1]] = v
    else:
        deep_update(config_args, conf)

    config = RqAttrDict(conf)

    set_locale(config.extra.locale)

    def _to_date(v):
        return pd.Timestamp(v).date()

    config.base.start_date = _to_date(config.base.start_date)
    config.base.end_date = _to_date(config.base.end_date)

    if config.base.data_bundle_path is None:
        config.base.data_bundle_path = os.path.join(
            os.path.expanduser(rqrobot_path), "bundle")

    config.base.run_type = parse_run_type(config.base.run_type)
    config.base.accounts = parse_accounts(config.base.accounts)
    config.base.init_positions = parse_init_positions(
        config.base.init_positions)
    config.base.persist_mode = parse_persist_mode(config.base.persist_mode)
    config.base.market = parse_market(config.base.market)

    if config.extra.context_vars:
        if isinstance(config.extra.context_vars, six.string_types):
            config.extra.context_vars = json.loads(
                to_utf8(config.extra.context_vars))

    if config.base.frequency == "1d":
        logger.DATETIME_FORMAT = "%Y-%m-%d"

    return config
示例#5
0
def dump_config(config_path, config, dumper=yaml.Dumper):
    dirname = os.path.dirname(config_path)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    with codecs.open(config_path, mode='w', encoding='utf-8') as stream:
        stream.write(to_utf8(yaml.dump(config, Dumper=dumper)))