コード例 #1
0
def load_config_file(config_file, ignore_names):
    """ Loads and builds the model from the configuration

    Arguments:
        config_file: The configuration file
        ignore_names: A set of names that should be ignored during the loading.
    """
    config_dicts = parsing.parse_file(config_file)
    config_file.close()
    log("INI file is parsed.")

    # first load the configuration into a dictionary

    if "main" not in config_dicts:
        raise Exception("Configuration does not contain the main block.")

    existing_objects = dict()

    main_config = config_dicts['main']

    configuration = dict()
    # TODO ensure tf_manager goes last in a better way
    for key, value in sorted(main_config.items(),
                             key=lambda t: t[0]
                             if t[0] != 'tf_manager' else 'zzz'):
        if key not in ignore_names:
            try:
                configuration[key] = build_object(value, config_dicts,
                                                  existing_objects, 0)
            except Exception as exc:
                raise ConfigBuildException(key, exc) from None

    return configuration
コード例 #2
0
def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("config",
                        metavar="INI-FILE",
                        help="a configuration file")
    parser.add_argument("--code",
                        "-c",
                        action="store_true",
                        help="instead of building the config, generate "
                        "equivalent Python code and write it to stdout")
    args = parser.parse_args()

    with open(args.config, "r", encoding="utf-8") as f:
        _, config_dict = parse_file(f)

    if args.code:
        imports, statements = _patch_config_builder()

    config, _ = build_config(config_dict, ignore_names=set())

    if args.code:
        print("import argparse\nimport tensorflow as tf")
        print(*sorted(imports), sep="\n", end="\n\n")
        print(*statements, sep="\n", end="\n\n")
        print("model = argparse.Namespace({})".format(",".join(
            "\n    {}={}".format(key, config[key]) for key in config)))
コード例 #3
0
def load_config_file(config_file, ignore_names):
    """ Loads and builds the model from the configuration

    Arguments:
        config_file: The configuration file
        ignore_names: A set of names that should be ignored during the loading.
    """
    config_dicts = parsing.parse_file(config_file)
    config_file.close()
    log("INI file is parsed.")

    # first load the configuration into a dictionary

    if "main" not in config_dicts:
        raise Exception("Configuration does not contain the main block.")

    existing_objects = dict()

    main_config = config_dicts['main']

    configuration = dict()
    for key, value in main_config.items():
        if key not in ignore_names:
            try:
                configuration[key] = build_object(
                    value, config_dicts, existing_objects, 0)
            except Exception as exc:
                raise ConfigBuildException(key, exc) from None

    return configuration
コード例 #4
0
    def load_file(self, path: str) -> None:
        log("Loading INI file: '{}'".format(path), color='blue')

        try:
            with open(path, 'r', encoding='utf-8') as file:
                self.config_dict = parse_file(file)
            log("INI file is parsed.")
            arguments = self.make_namespace(self.config_dict['main'])
        # pylint: disable=broad-except
        except Exception as exc:
            log("Failed to load INI file: {}".format(exc), color='red')
            traceback.print_exc()
            exit(1)

        self.args = arguments
コード例 #5
0
ファイル: configuration.py プロジェクト: soumye/neuralmonkey
    def load_file(self,
                  path: str,
                  changes: Optional[List[str]] = None) -> None:
        log("Loading INI file: '{}'".format(path), color="blue")

        try:
            with open(path, "r", encoding="utf-8") as file:
                self.raw_config, self.config_dict = parse_file(file, changes)
            log("INI file is parsed.")

            arguments = self.make_namespace(self.config_dict["main"])
        # pylint: disable=broad-except
        except Exception as exc:
            log("Failed to load INI file: {}".format(exc), color="red")
            traceback.print_exc()
            exit(1)

        self.args = arguments
コード例 #6
0
ファイル: configuration.py プロジェクト: ufal/neuralmonkey
    def load_file(self, path: str,
                  changes: Optional[List[str]] = None) -> None:
        log("Loading INI file: '{}'".format(path), color="blue")

        try:
            with open(path, "r", encoding="utf-8") as file:
                raw_config, config_dict = parse_file(file, changes)
            log("INI file is parsed.")

            self.raw_config.update(raw_config)
            self.config_dict.update(config_dict)
        # pylint: disable=broad-except
        except Exception as exc:
            log("Failed to load INI file: {}".format(exc), color="red")
            traceback.print_exc()
            exit(1)

        if "main" in self.config_dict:
            self.args = self.make_namespace(self.config_dict["main"])