Skip to content

Commit 25a3557

Browse files
committed
Reformat CLI command implementations
1 parent 0360c3c commit 25a3557

File tree

9 files changed

+528
-396
lines changed

9 files changed

+528
-396
lines changed

pybm/command.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
class CLICommand:
1111
"""CLI command base class."""
12+
1213
usage = ""
1314

1415
def __init__(self, name: str, **parser_kwargs):
@@ -20,20 +21,26 @@ def __init__(self, name: str, **parser_kwargs):
2021
usage=self.usage,
2122
description=self.__doc__,
2223
formatter_class=argparse.RawDescriptionHelpFormatter,
23-
**parser_kwargs
24+
**parser_kwargs,
25+
)
26+
27+
self.parser.add_argument(
28+
self.parser.prefix_chars + "h",
29+
self.parser.prefix_chars * 2 + "help",
30+
action="help",
31+
default=argparse.SUPPRESS,
32+
help="Show this message and exit.",
33+
)
34+
35+
self.parser.add_argument(
36+
self.parser.prefix_chars + "v",
37+
action="store_true",
38+
default=False,
39+
dest="verbose",
40+
help="Enable verbose mode. Makes pybm "
41+
"log information that might be useful "
42+
"for debugging.",
2443
)
25-
self.parser.add_argument(self.parser.prefix_chars + 'h',
26-
self.parser.prefix_chars * 2 + "help",
27-
action='help',
28-
default=argparse.SUPPRESS,
29-
help='Show this message and exit.')
30-
self.parser.add_argument(self.parser.prefix_chars + "v",
31-
action="store_true",
32-
default=False,
33-
dest="verbose",
34-
help="Enable verbose mode. Makes pybm "
35-
"log information that might be useful "
36-
"for debugging.")
3744

3845
def add_arguments(self):
3946
"""Add arguments to class argument parser member."""
@@ -48,11 +55,7 @@ def format_call(self, args: List[str]) -> str:
4855
def run_wrapped(self, args: List[str]):
4956
try:
5057
return self.run(args)
51-
except (
52-
PybmError,
53-
GitError,
54-
BuilderError,
55-
) as e:
58+
except (PybmError, GitError, BuilderError) as e:
5659
sys.stderr.write(f"Error: {e}")
5760
sys.stderr.write(os.linesep)
5861
sys.exit(ERROR)

pybm/commands/base.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ class BaseCommand(CLICommand):
1616
report - Report results of successful benchmarking runs.
1717
run - Run specified benchmarks in different environments.
1818
"""
19+
1920
usage = "pybm <command> [<options>]"
2021

2122
def __init__(self):
2223
super(BaseCommand, self).__init__(name="")
2324

2425
def add_arguments(self):
2526
# special version action and version kwarg
26-
self.parser.add_argument("--version",
27-
action="version",
28-
help="Show pybm version and exit.",
29-
version=f"%(prog)s version {__version__}")
27+
self.parser.add_argument(
28+
"--version",
29+
action="version",
30+
help="Show pybm version and exit.",
31+
version=f"%(prog)s version {__version__}",
32+
)
3033

3134
def run(self, args: List[str]):
3235
self.add_arguments()

pybm/commands/compare.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,42 @@ class CompareCommand(CLICommand):
1313
"""
1414
Report benchmark results from specified sources.
1515
"""
16+
1617
usage = "pybm compare <run> <anchor-ref> <compare-refs> [<options>]\n"
1718

1819
def __init__(self):
1920
super(CompareCommand, self).__init__(name="compare")
2021
self.config = PybmConfig.load(".pybm/config.yaml")
2122

2223
def add_arguments(self):
23-
self.parser.add_argument("run",
24-
type=str,
25-
metavar="<run>",
26-
help="Benchmark run to report results for. "
27-
"To report the preceding run, use the "
28-
"\"latest\" keyword. To report results "
29-
"of the n-th preceding run "
30-
"(i.e., n runs ago), "
31-
"use the \"latest^{n}\" syntax.")
32-
self.parser.add_argument("refs",
33-
nargs="+",
34-
metavar="<refs>",
35-
help="Benchmarked refs to compare. The first "
36-
"given ref will be treated as the "
37-
"anchor ref, relative to which all "
38-
"differences are reported. An error is "
39-
"raised if any of the given "
40-
"refs are not present in the run.")
24+
self.parser.add_argument(
25+
"run",
26+
type=str,
27+
metavar="<run>",
28+
help="Benchmark run to report results for. "
29+
"To report the preceding run, use the "
30+
'"latest" keyword. To report results '
31+
"of the n-th preceding run "
32+
"(i.e., n runs ago), "
33+
'use the "latest^{n}" syntax.',
34+
)
35+
self.parser.add_argument(
36+
"refs",
37+
nargs="+",
38+
metavar="<refs>",
39+
help="Benchmarked refs to compare. The first "
40+
"given ref will be treated as the "
41+
"anchor ref, relative to which all "
42+
"differences are reported. An error is "
43+
"raised if any of the given "
44+
"refs are not present in the run.",
45+
)
4146

4247
reporter: BenchmarkReporter = get_reporter_class(config=self.config)
4348
reporter_name = self.config.get_value("reporter.className")
44-
reporter_group_desc = f"Additional options from configured " \
45-
f"reporter class {reporter_name!r}"
49+
reporter_group_desc = (
50+
f"Additional options from configured reporter class {reporter_name!r}"
51+
)
4652
reporter_group = self.parser.add_argument_group(reporter_group_desc)
4753
# add builder-specific options into the group
4854
for arg in reporter.add_arguments():
@@ -66,14 +72,18 @@ def run(self, args: List[str]) -> int:
6672
# TODO: Make this dynamic to support other run identifiers
6773
result = sorted(get_subdirs(result_dir))[-1]
6874
result_path = result_dir / result
75+
6976
if result_path.exists():
70-
reporter.compare(*refs,
71-
result=result,
72-
target_filter=options.target_filter,
73-
benchmark_filter=options.benchmark_filter,
74-
context_filter=options.context_filter)
77+
reporter.compare(
78+
*refs,
79+
result=result,
80+
target_filter=options.target_filter,
81+
benchmark_filter=options.benchmark_filter,
82+
context_filter=options.context_filter,
83+
)
7584
else:
76-
raise PybmError(f"No benchmark results found for the requested "
77-
f"run {run!r}.")
85+
raise PybmError(
86+
f"No benchmark results found for the requested run {run!r}."
87+
)
7888

7989
return SUCCESS

pybm/commands/config.py

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,65 @@
1212

1313
class ConfigCommand(CLICommand):
1414
"""Display and manipulate configuration values."""
15-
usage = "pybm config get <option>\n" \
16-
" or: pybm config set <option> <value>\n" \
17-
" or: pybm config list\n" \
18-
" or: pybm config describe <option>\n"
15+
16+
usage = (
17+
"pybm config get <option>\n"
18+
" or: pybm config set <option> <value>\n"
19+
" or: pybm config list\n"
20+
" or: pybm config describe <option>\n"
21+
)
1922

2023
def __init__(self):
2124
super().__init__(name="config")
2225

2326
def add_arguments(self, subcommand: str = None):
2427
# special version action and version kwarg
2528
if subcommand == "get":
26-
self.parser.add_argument("option",
27-
type=str,
28-
metavar="<option>",
29-
help="Config option to display. For a "
30-
"comprehensive list of options, "
31-
"run `pybm config list`.")
29+
self.parser.add_argument(
30+
"option",
31+
type=str,
32+
metavar="<option>",
33+
help="Config option to display. For a "
34+
"comprehensive list of options, "
35+
"run `pybm config list`.",
36+
)
3237
elif subcommand == "set":
33-
self.parser.add_argument("option",
34-
type=str,
35-
metavar="<option>",
36-
help="Config option to set. For a "
37-
"comprehensive list of options, "
38-
"run `pybm config list`.")
38+
self.parser.add_argument(
39+
"option",
40+
type=str,
41+
metavar="<option>",
42+
help="Config option to set. For a "
43+
"comprehensive list of options, "
44+
"run `pybm config list`.",
45+
)
3946
# TODO: Revise nargs value for this option
40-
self.parser.add_argument("value",
41-
metavar="<value>",
42-
help="Config option to display. For a "
43-
"comprehensive list of options, "
44-
"run `pybm config list`.")
47+
self.parser.add_argument(
48+
"value",
49+
metavar="<value>",
50+
help="Config option to display. For a "
51+
"comprehensive list of options, "
52+
"run `pybm config list`.",
53+
)
4554
elif subcommand == "describe":
46-
self.parser.add_argument("option",
47-
type=str,
48-
metavar="<option>",
49-
help="Config option to describe. For a "
50-
"comprehensive list of options, "
51-
"run `pybm config list`.")
55+
self.parser.add_argument(
56+
"option",
57+
type=str,
58+
metavar="<option>",
59+
help="Config option to describe. For a "
60+
"comprehensive list of options, "
61+
"run `pybm config list`.",
62+
)
5263

5364
@contextlib.contextmanager
54-
def context(self, op: str, attr: str, value: Optional[str],
55-
verbose: bool = False):
65+
def context(self, op: str, attr: str, value: Optional[str], verbose: bool = False):
5666
# TODO: This is BS
5767
is_group = "." not in attr
5868
expr = "value" + "s" * is_group + f" {value!r}" * (value is not None)
5969
try:
6070
if verbose:
6171
opt_type = "group" if is_group else "option"
6272
op = op.capitalize()
63-
print(f"{op}ting {expr} for config {opt_type} {attr!r}"
64-
f".....", end="")
73+
print(f"{op}ting {expr} for config {opt_type} {attr!r}.....", end="")
6574
yield
6675
if verbose:
6776
print("done.")
@@ -72,58 +81,73 @@ def context(self, op: str, attr: str, value: Optional[str],
7281

7382
def get(self, options: argparse.Namespace, verbose: bool) -> int:
7483
attr: str = options.option
84+
7585
with self.context("get", attr, None, verbose):
7686
value = PybmConfig.load(".pybm/config.yaml").get_value(attr)
87+
7788
if is_dataclass(value):
7889
for k, v in asdict(value).items():
7990
print(f"{k} = {v}")
8091
else:
8192
print(f"{attr} = {value}")
93+
8294
return SUCCESS
8395

8496
def set(self, options: argparse.Namespace, verbose: bool) -> int:
8597
attr, value = options.option, options.value
8698
path = ".pybm/config.yaml"
99+
87100
with self.context("set", attr, value, verbose):
88101
PybmConfig.load(path).set_value(attr, value).save(path)
102+
89103
return SUCCESS
90104

91105
@staticmethod
92106
def list(options: argparse.Namespace, verbose: bool) -> int:
93107
del verbose # unused
94108
config = PybmConfig.load(".pybm/config.yaml")
109+
95110
for name in get_all_names(config):
96111
group = config.get_value(name)
97-
print(f"Config values for group \"{name}\":")
112+
print(f"Config values for group {name!r}:")
113+
98114
for k, v in asdict(group).items():
99115
val = v if v != "" else "(empty string)"
100116
print(f"{k} : {val}")
117+
101118
print("")
102119
return SUCCESS
103120

104121
@staticmethod
105122
def describe(options: argparse.Namespace, verbose: bool) -> int:
106123
del verbose # unused
107124
attr = options.option
125+
108126
if "__" in attr:
109-
raise PybmError("Only unprivileged configuration attributes can "
110-
"be described via `pybm config describe`.")
127+
raise PybmError(
128+
"Only unprivileged configuration attributes can "
129+
"be described via `pybm config describe`."
130+
)
131+
111132
config = PybmConfig.load(".pybm/config.yaml")
112133
config.describe(attr)
134+
113135
return SUCCESS
114136

115137
def run(self, args: List[str]):
116138
subcommand_handlers = {
117139
"set": self.set,
118140
"get": self.get,
119141
"list": self.list,
120-
"describe": self.describe
142+
"describe": self.describe,
121143
}
122144

123145
if not args or args[0] not in subcommand_handlers:
124146
self.parser.print_help()
125147
return ERROR
148+
126149
subcommand, *args = args
150+
127151
self.add_arguments(subcommand=subcommand)
128152

129153
# double hyphen prevents `config set` values to be mistaken for

0 commit comments

Comments
 (0)