コード例 #1
0
ファイル: loader.py プロジェクト: lgxqf/easy_mock
def prepare_path(path):
    if not os.path.exists(path):
        err_msg = f"path not exist: {path}"
        common.log().error(err_msg)
        raise FileNotFoundError(err_msg)

    if not os.path.isabs(path):
        path = os.path.join(os.getcwd(), path)

    return path
コード例 #2
0
ファイル: loader.py プロジェクト: lgxqf/easy_mock
def load_processor_func() -> Dict[Text, Callable]:
    """ load processor.py project module functions
    """
    try:
        from os.path import abspath, join, dirname
        sys.path.insert(0, join(abspath(dirname(__file__)), os.getcwd()))
        imported_module = importlib.import_module("processor")
    except Exception as e:
        common.log().error(f"error occurred in processor.py: {e}")
        sys.exit(1)

    # reload to refresh previously loaded module
    imported_module = importlib.reload(imported_module)
    return load_module_functions(imported_module)
コード例 #3
0
ファイル: loader.py プロジェクト: lgxqf/easy_mock
def get_yaml(path, layer="apis"):
    """Obtain configuration information according to layer parameters. eg: group.apis
    """

    with open(path, 'r', encoding='utf-8') as f:
        cfg = f.read()

    try:
        yaml_data = yaml.load(cfg, Loader=yaml.FullLoader) or {}
    except Exception as e:
        common.log().error(f"File parsing failed, please check: {path}")
        raise e

    for la in layer.split("."):
        yaml_data = yaml_data.get(la)

    return yaml_data
コード例 #4
0
ファイル: loader.py プロジェクト: lgxqf/easy_mock
def load_default_conf(options: Text) -> Union[bool, str]:
    """Load an option in DEFAULT in the config.ini file section.
    """
    try:
        config_file = ConfigParser()
        config_file.read(locate_file(os.getcwd(), "config.ini"),
                         encoding='utf8')
        # if "bool" == option_type:
        #     return config_file.getboolean(options)
        value = config_file['DEFAULT'][options]

        if value.lower() == "false":
            return False

        return config_file['DEFAULT'][options]

    except Exception as e:
        common.log().debug(f"config.ini option {options} not found: {e}")
        return ""
コード例 #5
0
def cli():
    parser = argparse.ArgumentParser(description=__description__)
    parser.add_argument('-v',
                        '--version',
                        dest='version',
                        action="store_true",
                        help="show version")
    parser.add_argument(
        dest='file_path',
        help="yaml configuration file, directory, or .proto file")
    parser.add_argument('-p',
                        dest='port',
                        help="port that needs mock service.")
    parser.add_argument('-https',
                        dest='enable_https',
                        action='store_true',
                        default=False,
                        help="enable mock server https protocol")
    parser.add_argument('-req',
                        dest='yaml_req',
                        action='store_true',
                        default=False,
                        help="generate request_schema in yaml")
    parser.add_argument('-res',
                        dest='yaml_res',
                        action='store_true',
                        default=True,
                        help="generate response_schema in yaml")

    args = parser.parse_args()

    # print(args)
    print("\n")

    if args.version:
        print(__version__)
        exit(0)

    if args.file_path:
        file_path = args.file_path

        if file_path.endswith(".proto"):
            Pb2Yaml.pb2ymal(file_path, args.yaml_req, args.yaml_res)
            exit(0)
        elif file_path.endswith(".yml") or file_path.endswith(".yaml"):
            config.file_path = file_path
        else:
            common.log().error("yml or pb file is not specified.")
            exit(1)

    if args.port:
        config.port = args.port
    else:
        config.port = load_default_conf("port") or "9000"

    if args.enable_https:
        config.is_https = True
    else:
        config.is_https = bool(load_default_conf("is_https"))

    if args.file_path:
        config.file_path = args.file_path
    else:
        config.file_path = os.getcwd()

    main()