def parse_file(config_file: Iterable[str]) -> Dict[str, Any]:
    """ Parses an INI file and creates all values """

    parsed_dicts = OrderedDict()  # type: Dict[str, Any]
    time_stamp = time.strftime("%Y-%m-%d-%H-%M-%S")

    config = _parse_ini(config_file)

    for section in config:
        parsed_dicts[section] = OrderedDict()
        for key, (lineno, value_string) in config[section].items():
            # expansion
            # TODO do this using **kwargs with dict from names to values
            value_string = re.sub(r"\$TIME", time_stamp, value_string)

            try:
                value = _parse_value(value_string)
            except IniError as exc:
                raise
            except Exception as exc:
                raise IniError(
                    lineno, "Cannot parse value: '{}'.".format(value_string),
                    exc) from None

            parsed_dicts[section][key] = value

    return parsed_dicts
示例#2
0
def parse_file(config_file):
    """ Parses an INI file into a dictionary """

    parsed_dicts = dict()
    time_stamp = time.strftime("%Y-%m-%d-%H-%M-%S")

    config = parse_config(config_file)

    for section in config:
        parsed_dicts[section] = dict()
        for key, (lineno, value_string) in config[section].items():
            # expansion
            # TODO do this using **kwargs with dict from names to values
            value_string = re.sub(r"\$TIME", time_stamp, value_string)

            try:
                value = _parse_value(value_string)
            except IniError as exc:
                raise
            except Exception as exc:
                raise IniError(lineno,
                               "Cannot parse value: '{}'".format(value_string),
                               exc) from None

            parsed_dicts[section][key] = value

    return parsed_dicts
示例#3
0
    def parse_section(section: str, output_dict: Dict[str, Any]):
        for key, (lineno, value_string) in config[section].items():
            try:
                value = _parse_value(value_string, vars_dict)
            except IniError as exc:
                raise
            except Exception as exc:
                raise IniError(
                    lineno, "Cannot parse value: '{}'.".format(value_string),
                    exc) from None

            output_dict[key] = value
示例#4
0
def parse_file(
    config_file: Iterable[str],
    changes: Optional[Iterable[str]] = None
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
    """ Parses an INI file and creates all values """

    parsed_dicts = OrderedDict()  # type: Dict[str, Any]
    time_stamp = time.strftime("%Y-%m-%d-%H-%M-%S")

    config = _parse_ini(config_file)

    if changes is not None:
        for change in changes:
            _apply_change(config, change)

    for section in config:
        parsed_dicts[section] = OrderedDict()
        for key, (lineno, value_string) in config[section].items():
            # expansion
            # TODO do this using **kwargs with dict from names to values
            value_string = re.sub(r"\$TIME", time_stamp, value_string)

            try:
                value = _parse_value(value_string)
            except IniError as exc:
                raise
            except Exception as exc:
                raise IniError(
                    lineno, "Cannot parse value: '{}'.".format(value_string),
                    exc) from None

            parsed_dicts[section][key] = value

    # also return the unparsed config dict; need to remove line numbers
    raw_config = OrderedDict([
        (name, OrderedDict([(key, val) for key, (_, val) in section.items()]))
        for name, section in config.items()
    ])

    return raw_config, parsed_dicts