コード例 #1
0
ファイル: parser.py プロジェクト: adriagalin/ovh-cli
    def ensure_parser(self, chunk, path=None, help="", schema=None):
        '''
        if ``chunk`` starts with '{' set ``name`` and ``path`` to None.
        Otherwise, set name to snake-case path.

        If path is specified, override default one.

        If parser ``name`` already exists, return it. Otherwise, instanciate
        parser and register it.

        :return: parser
        :raise ArgParserTypeConflict: when mixing ``argument`` ``router`` parsers
        '''
        # Is it an argument ?
        if chunk[0] == '{':
            name = None
            path = chunk
        else:
            name = camel_to_snake(chunk)
            path = path or chunk

        # check duplicated name
        if name in self._routes:
            return self._routes[name]

        # register
        self._routes[name] = ArgParser(name, path, help, schema)
        return self._routes[name]
コード例 #2
0
    def ensure_parser(self, chunk, path=None, help="", schema=None):
        '''
        if ``chunk`` starts with '{' set ``name`` and ``path`` to None.
        Otherwise, set name to snake-case path.

        If path is specified, override default one.

        If parser ``name`` already exists, return it. Otherwise, instanciate
        parser and register it.

        :return: parser
        :raise ArgParserTypeConflict: when mixing ``argument`` ``router`` parsers
        '''
        # Is it an argument ?
        if chunk[0] == '{':
            name = None
            path = chunk
        else:
            name = camel_to_snake(chunk)
            path = path or chunk

        # check duplicated name
        if name in self._routes:
            return self._routes[name]

        # register
        self._routes[name] = ArgParser(name, path, help, schema)
        return self._routes[name]
コード例 #3
0
ファイル: ovh-cli.py プロジェクト: Alkorin/ovh-cli
def init_arg_parser(endpoint, refresh=False):
    '''
    Build command line parser from json and cache result on disk for faster
    load.

    As there is (currently) no ambiguity, always take only the second part of
    the 'resourcePath' as command name. For instance, '/hosting/privateDatabase'
    leads to 'private-database'.

    All command line arguments are converted to snake-case.

    :param str endpoint: api endpoint name.
    :param boolean refresh: when ``True``, bypass cache, no matter its state.
    '''

    cache_file = SCHEMAS_BASE_PATH+endpoint

    # First attempt to load parser from cache
    try:
        if not refresh:
            with open(cache_file, 'r') as f:
                return pickle.load(f)
    except:
        pass

    # cache dir exists ?
    if not os.path.exists(SCHEMAS_BASE_PATH):
        os.makedirs(SCHEMAS_BASE_PATH)

    # get schemas
    load_schemas(ENDPOINTS[endpoint])

    # Build parser
    parser = ArgParser(None, None)

    for schema in SCHEMAS.values():
        if not 'resourcePath' in schema:
            continue

        # add root command
        base_path = schema['resourcePath']
        api_cmd = camel_to_snake(base_path[1:])
        api_parser = parser.ensure_parser(api_cmd, base_path[1:])

        # add subcommands
        for api in schema['apis']:
            command_path = api['path'][len(base_path):]
            command_parser = api_parser.ensure_path_parser(command_path, api['description'], schema)

            # add actions
            for operation in api['operations']:
                command_parser.register_http_verb(
                        operation['httpMethod'],
                        operation['parameters'],
                        operation['description']
                )

    # cache resulting parser
    with open(cache_file, 'w') as f:
        pickle.dump(parser, f, pickle.HIGHEST_PROTOCOL)

    return parser
コード例 #4
0
ファイル: ovh-cli.py プロジェクト: ppcharlier/ovh-cli-1
def init_arg_parser(endpoint, refresh=False):
    '''
    Build command line parser from json and cache result on disk for faster
    load.

    As there is (currently) no ambiguity, always take only the second part of
    the 'resourcePath' as command name. For instance, '/hosting/privateDatabase'
    leads to 'private-database'.

    All command line arguments are converted to snake-case.

    :param str endpoint: api endpoint name.
    :param boolean refresh: when ``True``, bypass cache, no matter its state.
    '''

    cache_file = SCHEMAS_BASE_PATH + endpoint

    # First attempt to load parser from cache
    try:
        if not refresh:
            with open(cache_file, 'r') as f:
                return pickle.load(f)
    except:
        pass

    # cache dir exists ?
    if not os.path.exists(SCHEMAS_BASE_PATH):
        os.makedirs(SCHEMAS_BASE_PATH)

    # get schemas
    load_schemas(ENDPOINTS[endpoint])

    # Build parser
    parser = ArgParser(None, None)

    for schema in SCHEMAS.values():
        if not 'resourcePath' in schema:
            continue

        # add root command
        base_path = schema['resourcePath']
        api_cmd = camel_to_snake(base_path[1:])
        api_parser = parser.ensure_parser(api_cmd, base_path[1:])

        # add subcommands
        for api in schema['apis']:
            command_path = api['path'][len(base_path):]
            command_parser = api_parser.ensure_path_parser(
                command_path, api['description'], schema)

            # add actions
            for operation in api['operations']:
                command_parser.register_http_verb(operation['httpMethod'],
                                                  operation['parameters'],
                                                  operation['description'])

    # cache resulting parser
    with open(cache_file, 'w') as f:
        pickle.dump(parser, f, pickle.HIGHEST_PROTOCOL)

    return parser