示例#1
0
def main(context, config_file, config_json, config_module, include):
    """Configs can be passed by file or directly from json.
    If neither --config-file or --config-json is passed,
    attempts to read the file from stdin.

    Example:

      pytext train < demos/docnn.json
    """
    for path in include or []:
        # remove possible trailing / from autocomplete in --include
        add_include(path.rstrip("/"))

    context.obj = Attrs()

    def load_config():
        # Cache the config object so it can be accessed multiple times
        if not hasattr(context.obj, "config"):
            if config_module:
                context.obj.config = import_module(config_module).config
            else:
                if config_file:
                    with open(config_file) as file:
                        config = json.load(file)
                elif config_json:
                    config = json.loads(config_json)
                else:
                    eprint("No config file specified, reading from stdin")
                    config = json.load(sys.stdin)
                context.obj.config = parse_config(config)
        return context.obj.config

    context.obj.load_config = load_config
示例#2
0
 def load_config():
     # Cache the config object so it can be accessed multiple times
     if not hasattr(context.obj, "config"):
         if config_module:
             context.obj.config = import_module(config_module).config
         else:
             if config_file:
                 with PathManager.open(config_file) as file:
                     config = json.load(file)
             elif config_json:
                 config = json.loads(config_json)
             else:
                 eprint("No config file specified, reading from stdin")
                 config = json.load(sys.stdin)
             # before parsing the config, include the custom components
             for path in config.get("include_dirs", []):
                 add_include(path.rstrip("/"))
             context.obj.config = parse_config(config)
     return context.obj.config