Skip to content
Snippets Groups Projects
Commit f014a23a authored by Raphaël Gomès's avatar Raphaël Gomès
Browse files

bin: rewrite `result-search` with `click`

This is much simpler and more powerful than rolling it by hand.
parent e402bad5
No related branches found
No related tags found
1 merge request!23Use click for argument parsing, etc.
#!/usr/bin/env python3
#
# find result matching patterns
import argparse
import glob
import json
import os
import re
import sys
......@@ -5,12 +2,8 @@
import glob
import json
import os
import re
import sys
import poulpe
def _parsers():
cmd_parser = argparse.ArgumentParser(prog='result-search')
import click
......@@ -16,20 +9,5 @@
cmd_parser.add_argument(
'RESULTS',
default=['.'],
nargs='+',
help="the path to result files or directories containing `.pbr` files",
)
cmd_parser.add_argument(
'--content-as-json',
action='store_const',
const=True,
)
cmd_parser.add_argument(
'--filter',
action='append',
)
return cmd_parser
import poulpe
def parse_filters(filter_definitions):
......@@ -41,7 +19,6 @@
match a regular exception.
"""
filters = []
if filter_definitions is not None:
for fd in filter_definitions:
key, value = fd.split('=', 1)
for fd in filter_definitions:
key, value = fd.split('=', 1)
......@@ -47,11 +24,11 @@
key = key.split('.')
if value.startswith('re:'):
value = value[3:]
else:
value = re.escape(value)
value = re.compile(value)
filters.append((key, value))
key = key.split('.')
if value.startswith('re:'):
value = value[3:]
else:
value = re.escape(value)
value = re.compile(value)
filters.append((key, value))
return filters
......@@ -74,7 +51,32 @@
yield path
def main(args):
parser = _parsers()
param = parser.parse_args(args)
@click.command()
@click.argument("result_paths", metavar="RESULTS", nargs=-1)
@click.option(
"--content-as-json",
is_flag=True,
default=False,
help="Display the contents as JSON instead of the filenames",
)
@click.option(
"--filter",
"filters",
multiple=True,
help="""Criteria to filter results with. Filters are additive.
They are of the form `key=value`, where `key` is the dot-delimited key
inside the result file, and `value` is the expected value.
If `value` starts with `re:`, then it is checked against
the regular expression that follows.""",
)
def result_search(result_paths, content_as_json, filters):
"""Find result files matching filters.
\b
ARGS:
<RESULTS> are paths to result files or directories containing `.pbr`
"""
if not result_paths:
result_paths = ['.']
results = []
......@@ -80,7 +82,7 @@
results = []
for r in param.RESULTS:
for r in result_paths:
if os.path.isdir(r):
results.extend(glob.glob(os.path.join(r, '*.pbr')))
elif os.path.isfile(r):
results.append(r)
......@@ -82,8 +84,8 @@
if os.path.isdir(r):
results.extend(glob.glob(os.path.join(r, '*.pbr')))
elif os.path.isfile(r):
results.append(r)
filters = parse_filters(param.filter)
filters = parse_filters(filters)
results = apply_filter(filters, results)
......@@ -88,6 +90,6 @@
results = apply_filter(filters, results)
if param.content_as_json:
if content_as_json:
first = True
print('[')
for path in results:
......@@ -100,8 +102,6 @@
else:
for path in results:
print(path)
ret = 0
return ret
if __name__ == "__main__":
......@@ -105,6 +105,4 @@
if __name__ == "__main__":
ret = main(sys.argv[1:])
assert ret is not None
sys.exit(ret)
result_search()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment