Exemple #1
0
def main(argv=sys.argv[1:]):
    parser = argparse.ArgumentParser(
        description='Query the ament resource index.')
    arg = parser.add_argument('resource_type',
                              nargs='?',
                              metavar='TYPE',
                              help='The type of the resource')
    arg.completer = resource_type_completer
    arg = parser.add_argument('resource_name',
                              nargs='?',
                              metavar='NAME',
                              help='The name of the resource')
    arg.completer = resource_name_completer

    try:
        from argcomplete import autocomplete
    except ImportError:
        pass
    else:
        autocomplete(parser, exclude=['-h', '--help'])

    args = parser.parse_args(argv)

    if args.resource_type is None:
        for resource_type in sorted(get_resource_types()):
            print(resource_type)
        return

    if args.resource_name is None:
        resources = get_resources(args.resource_type)
        for resource_name in sorted(resources.keys()):
            print(resource_name + '\t' + resources[resource_name])
        return

    try:
        content, path = get_resource(args.resource_type, args.resource_name)
    except (LookupError, OSError) as e:
        return str(e)
    print(path)
    if content:
        print('<<<')
        print(content)
        print('>>>')
Exemple #2
0
def resource_name_completer(prefix, parsed_args, **kwargs):
    resource_type = getattr(parsed_args, 'resource_type', None)
    if not resource_type:
        return []
    return get_resources(resource_type).keys()