Exemplo n.º 1
0
    def loads(cls, config_content, **kwargs):
        """
        :param config_content:  Config file content
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: cls.container() object holding config parameters
        """
        return json.loads(config_content, object_hook=dict_to_container,
                          **Base.mk_opt_args(cls._load_opts, kwargs))
Exemplo n.º 2
0
def yaml_dump(data, stream, **kwargs):
    """
    An wrapper of yaml.{safe_,}dump
    """
    keys = filter_keys(kwargs.keys(), "safe")
    if kwargs.get("safe", False):
        return yaml.safe_dump(data, stream, **Base.mk_opt_args(keys, kwargs))
    else:
        return yaml.dump(data, stream, **kwargs)
Exemplo n.º 3
0
def yaml_load(stream, **kwargs):
    """
    An wrapper of yaml.{safe_,}load
    """
    keys = filter_keys(kwargs.keys(), "safe")
    if kwargs.get("safe", False):
        return yaml.safe_load(stream, **Base.mk_opt_args(keys, kwargs))
    else:
        return yaml.load(stream, **kwargs)
Exemplo n.º 4
0
    def loads(cls, config_content, **kwargs):
        """
        :param config_content:  Config file content
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: cls.container() object holding config parameters
        """
        return json.loads(config_content, object_hook=dict_to_container,
                          **Base.mk_opt_args(cls._load_opts, kwargs))
Exemplo n.º 5
0
def _load_impl(config_fp, sep=_SEP, **kwargs):
    """
    :param config_fp: File or file-like object provides ini-style conf
    :return: Dict or dict-like object represents config values
    """
    config = dict()

    # Optional arguements for configparser.SafeConfigParser{,readfp}
    kwargs_0 = Base.mk_opt_args(("defaults", "dict_type", "allow_no_value"),
                                kwargs)
    kwargs_1 = Base.mk_opt_args(("filename", ), kwargs)

    try:
        try:
            parser = configparser.SafeConfigParser(**kwargs_0)
        except TypeError:
            # It seems ConfigPaser.*ConfigParser in python 2.6 does not support
            # 'allow_no_value' option parameter, and TypeError will be thrown.
            kwargs_0 = Base.mk_opt_args(("defaults", "dict_type"), kwargs)
            parser = configparser.SafeConfigParser(**kwargs_0)

        parser.readfp(config_fp, **kwargs_1)

        if parser.defaults():
            config["DEFAULT"] = dict()

            for k, v in iteritems(parser.defaults()):
                config["DEFAULT"][k] = _parse(v, sep)

        for s in parser.sections():
            config[s] = dict()

            for k, v in parser.items(s):
                config[s][k] = _parse(v, sep)

    except Exception:
        logging.warn(sys.exc_info()[-1])
        raise

    return config
Exemplo n.º 6
0
def _load_impl(config_fp, sep=_SEP, **kwargs):
    """
    :param config_fp: File or file-like object provides ini-style conf
    :return: Dict or dict-like object represents config values
    """
    config = dict()

    # Optional arguements for configparser.SafeConfigParser{,readfp}
    kwargs_0 = Base.mk_opt_args(("defaults", "dict_type", "allow_no_value"),
                                kwargs)
    kwargs_1 = Base.mk_opt_args(("filename", ), kwargs)

    try:
        try:
            parser = configparser.SafeConfigParser(**kwargs_0)
        except TypeError:
            # It seems ConfigPaser.*ConfigParser in python 2.6 does not support
            # 'allow_no_value' option parameter, and TypeError will be thrown.
            kwargs_0 = Base.mk_opt_args(("defaults", "dict_type"), kwargs)
            parser = configparser.SafeConfigParser(**kwargs_0)

        parser.readfp(config_fp, **kwargs_1)

        if parser.defaults():
            config["DEFAULT"] = dict()

            for k, v in iteritems(parser.defaults()):
                config["DEFAULT"][k] = _parse(v, sep)

        for s in parser.sections():
            config[s] = dict()

            for k, v in parser.items(s):
                config[s][k] = _parse(v, sep)

    except Exception:
        logging.warn(sys.exc_info()[-1])
        raise

    return config
Exemplo n.º 7
0
def _load(stream, to_container=dict, sep=_SEP, **kwargs):
    """
    :param stream: File or file-like object provides ini-style conf
    :param to_container: any callable to make container
    :param sep: Seprator string

    :return: Dict or dict-like object represents config values
    """
    _parse_val = _parse if kwargs.get("ac_parse_value", False) else _noop

    # Optional arguements for configparser.SafeConfigParser{,readfp}
    kwargs_0 = mk_opt_args(("defaults", "dict_type", "allow_no_value"), kwargs)
    kwargs_1 = mk_opt_args(("filename", ), kwargs)

    try:
        parser = configparser.SafeConfigParser(**kwargs_0)
    except TypeError:
        # .. note::
        #    It seems ConfigPaser.*ConfigParser in python 2.6 does not support
        #    'allow_no_value' option parameter, and TypeError will be thrown.
        kwargs_0 = mk_opt_args(("defaults", "dict_type"), kwargs)
        parser = configparser.SafeConfigParser(**kwargs_0)

    cnf = to_container()
    parser.readfp(stream, **kwargs_1)

    # .. note:: Process DEFAULT config parameters as special ones.
    defaults = parser.defaults()
    if defaults:
        cnf["DEFAULT"] = to_container()
        for key, val in iteritems(defaults):
            cnf["DEFAULT"][key] = _parse_val(val, sep)

    for sect in parser.sections():
        cnf[sect] = to_container()
        for key, val in parser.items(sect):
            cnf[sect][key] = _parse_val(val, sep)

    return cnf
Exemplo n.º 8
0
def _load(filepath=None, stream=None, sep=_SEP, **kwargs):
    """
    :param filepath: Config file path
    :param stream: File or file-like object provides ini-style conf
    :param sep: Seprator string

    :return: Dict or dict-like object represents config values
    """
    # Optional arguements for configparser.SafeConfigParser{,readfp}
    kwargs_0 = mk_opt_args(("defaults", "dict_type", "allow_no_value"), kwargs)
    kwargs_1 = mk_opt_args(("filename", ), kwargs)

    try:
        parser = configparser.SafeConfigParser(**kwargs_0)
    except TypeError:
        # .. note::
        #    It seems ConfigPaser.*ConfigParser in python 2.6 does not support
        #    'allow_no_value' option parameter, and TypeError will be thrown.
        kwargs_0 = mk_opt_args(("defaults", "dict_type"), kwargs)
        parser = configparser.SafeConfigParser(**kwargs_0)

    container = dict
    cnf = container()
    (fname, arg) = _switch_read_fn_arg(filepath, stream)
    getattr(parser, fname)(arg, **kwargs_1)

    # .. note:: Process DEFAULT config parameters as special ones.
    if parser.defaults():
        cnf["DEFAULT"] = container()
        for key, val in iteritems(parser.defaults()):
            cnf["DEFAULT"][key] = _parse(val, sep)

    for sect in parser.sections():
        cnf[sect] = container()
        for key, val in parser.items(sect):
            cnf[sect][key] = _parse(val, sep)

    return cnf
Exemplo n.º 9
0
    def load(cls, config_path, ignore_missing=False, **kwargs):
        """
        :param config_path:  Config file path
        :param ignore_missing: Ignore just return empty result if given file
            (``config_path``) does not exist
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: cls.container() object holding config parameters
        """
        # NOTE: Hack. See also: :method:``load`` of the class
        # :class:``anyconfig.backends.Base.ConfigParser``.
        if ignore_missing and not cls.exists(config_path):
            return cls.container()()

        return json.load(open(config_path), object_hook=dict_to_container,
                         **Base.mk_opt_args(cls._load_opts, kwargs))
Exemplo n.º 10
0
    def load(cls, config_path, ignore_missing=False, **kwargs):
        """
        :param config_path:  Config file path
        :param ignore_missing: Ignore just return empty result if given file
            (``config_path``) does not exist
        :param kwargs: optional keyword parameters to be sanitized :: dict

        :return: cls.container() object holding config parameters
        """
        # NOTE: Hack. See also: :method:``load`` of the class
        # :class:``anyconfig.backends.Base.ConfigParser``.
        if ignore_missing and not cls.exists(config_path):
            return cls.container()()

        return json.load(open(config_path), object_hook=dict_to_container,
                         **Base.mk_opt_args(cls._load_opts, kwargs))
Exemplo n.º 11
0
 def yaml_dump(data, fp, **kwargs):
     keys = filter_keys(kwargs.keys(), "safe")
     if kwargs.get("safe", False):
         return yaml.safe_dump(data, fp, **Base.mk_opt_args(keys, kwargs))
     else:
         return yaml.dump(data, fp, **kwargs)
Exemplo n.º 12
0
 def yaml_dump(data, fp, **kwargs):
     keys = filter_keys(kwargs.keys(), "safe")
     if kwargs.get("safe", False):
         return yaml.safe_dump(data, fp, **Base.mk_opt_args(keys, kwargs))
     else:
         return yaml.dump(data, fp, **kwargs)