Skip to content

Commit 86b6d8c

Browse files
committed
Remove defaults.py
The changes in OCP-on-NERC#1 made `defaults.py` redundant.
1 parent 2fae503 commit 86b6d8c

File tree

4 files changed

+84
-55
lines changed

4 files changed

+84
-55
lines changed

basecommand.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import abc
44
from typing import Protocol
55
from typing import Any
6+
from typing_extensions import override
67

78
import argparse
89

@@ -22,6 +23,17 @@ def add_parser(
2223
) -> argparse.ArgumentParser: ...
2324

2425

26+
class ArgumentDefaultsHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
27+
"""Provide the behavior of both argparse.ArgumentDefaultsHelpFormatter
28+
and argparse.RawDescriptionHelpFormatter."""
29+
30+
@override
31+
def _fill_text(
32+
self, text: str, width: int | None = None, indent: str | None = None
33+
):
34+
return "".join(indent + line for line in text.splitlines(keepends=True))
35+
36+
2537
class Command(abc.ABC):
2638
"""
2739
Base class for all command implementations.
@@ -37,7 +49,7 @@ def build_parser(cls, subparsers: SubParserFactory) -> argparse.ArgumentParser:
3749
cls.name,
3850
help=cls.help,
3951
description=cls.__doc__,
40-
formatter_class=argparse.RawDescriptionHelpFormatter,
52+
formatter_class=ArgumentDefaultsHelpFormatter,
4153
)
4254

4355
return p

batchtools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import argparse
22
import sys
33

4-
import defaults
54
from basecommand import Command
65
from basecommand import SubParserFactory
76
from bj import ListJobsCommand
@@ -33,7 +32,6 @@ def build_parser(self) -> None:
3332
"--verbose",
3433
"-v",
3534
action="count",
36-
default=defaults.verbose,
3735
help="Increase verbosity of output",
3836
)
3937

@@ -59,7 +57,9 @@ def run(self, args: list[str] | None = None):
5957

6058
def main() -> None:
6159
if not is_logged_in():
62-
sys.exit("You are not logged in to the oc cli. Retrieve the token using 'oc login --web' or retrieving the login token from the openshift UI.")
60+
sys.exit(
61+
"You are not logged in to the oc cli. Retrieve the token using 'oc login --web' or retrieving the login token from the openshift UI."
62+
)
6363

6464
app = BatchTools()
6565
app.run()

br.py

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
# pyright: reportUninitializedInstanceVariable=false
2+
from typing import Any
13
from typing import cast
2-
import typing_extensions
4+
from typing import Annotated
5+
from typing import get_args
36
from typing_extensions import override
47

58
import argparse
69
import os
710
import socket
811
import sys
912
import time
13+
import uuid
1014

1115
import openshift_client as oc
1216

@@ -19,19 +23,47 @@
1923

2024

2125
class CreateJobCommandArgs(argparse.Namespace):
22-
gpu: str = "v100"
23-
image: str = "image-registry.openshift-image-registry.svc:5000/redhat-ods-applications/csw-run-f25:latest"
24-
context: bool = True
25-
name: str = "job"
26-
job_id: int = os.getpid()
27-
job_delete: bool = True
28-
wait: bool = True
29-
timeout: int = 60 * 15 * 4
30-
max_sec: int = 60 * 15
31-
gpu_numreq: int = 1
32-
gpu_numlim: int = 1
33-
verbose: int = 0
34-
command: list[str] = []
26+
"""This class serves two purposes:
27+
28+
1. It provides type hints that permit a properly configured IDE (or type
29+
checker) to perform type checking on command line option values.
30+
31+
2. It provides default values for command line options.
32+
33+
We accomplish this using annotated types. The first metadata argument in
34+
the annotation provides the default value; this can be a static value, or
35+
it can be callable. This gives us the ability to perform lazy evaluation of
36+
default values.
37+
"""
38+
39+
gpu: Annotated[str, "v100"]
40+
image: Annotated[
41+
str,
42+
"image-registry.openshift-image-registry.svc:5000/redhat-ods-applications/csw-run-f25:latest",
43+
]
44+
context: Annotated[bool, True]
45+
name: Annotated[str, "job"]
46+
job_id: Annotated[
47+
str, lambda: uuid.uuid5(uuid.NAMESPACE_OID, f"{os.getpid()}-{time.time()}").hex
48+
]
49+
job_delete: Annotated[bool, True]
50+
wait: Annotated[bool, True]
51+
timeout: Annotated[int, 60 * 15 * 4]
52+
max_sec: Annotated[int, 60 * 15]
53+
gpu_numreq: Annotated[int, 1]
54+
gpu_numlim: Annotated[int, 1]
55+
verbose: Annotated[int, 0]
56+
command: list[str]
57+
58+
@classmethod
59+
def get_default_for(cls, name: str) -> Any:
60+
spec = get_args(cls.__annotations__[name])
61+
if len(spec) == 1:
62+
return
63+
if callable(spec[1]):
64+
return spec[1]()
65+
else:
66+
return spec[1]
3567

3668

3769
class CreateJobCommand(Command):
@@ -75,66 +107,65 @@ def build_parser(cls, subparsers: SubParserFactory):
75107
p = super().build_parser(subparsers)
76108
p.add_argument(
77109
"--gpu",
78-
default=CreateJobCommandArgs.gpu,
79-
help=f"Select GPU type (default {CreateJobCommandArgs.gpu})",
110+
default=CreateJobCommandArgs.get_default_for("gpu"),
111+
help="Select GPU type",
80112
)
81113
p.add_argument(
82114
"--image",
83-
default=CreateJobCommandArgs.image,
84-
help=f"Specify container image for job (default {CreateJobCommandArgs.image})",
115+
default=CreateJobCommandArgs.get_default_for("image"),
116+
help="Specify container image for job",
85117
)
86118
p.add_argument(
87119
"--context",
88120
action=argparse.BooleanOptionalAction,
89-
default=CreateJobCommandArgs.context,
90-
help=f"Copy working directory (default {CreateJobCommandArgs.context})",
121+
default=CreateJobCommandArgs.get_default_for("context"),
122+
help="Copy working directory",
91123
)
92124
p.add_argument(
93125
"--name",
94-
default=CreateJobCommandArgs.name,
95-
help=f"Base job name (default {CreateJobCommandArgs.name})",
126+
default=CreateJobCommandArgs.get_default_for("name"),
127+
help="Base job name",
96128
)
97129
p.add_argument(
98130
"--job-id",
99-
default=CreateJobCommandArgs.job_id,
100-
type=int,
101-
help="Job ID suffix (default current pid)",
131+
default=CreateJobCommandArgs.get_default_for("job_id"),
132+
help="Job ID suffix",
102133
)
103134
p.add_argument(
104135
"--job-delete",
105136
action=argparse.BooleanOptionalAction,
106-
default=CreateJobCommandArgs.job_delete,
107-
help=f"Delete job on completion (default {CreateJobCommandArgs.job_delete})",
137+
default=CreateJobCommandArgs.get_default_for("job_delete"),
138+
help="Delete job on completion",
108139
)
109140
p.add_argument(
110141
"--wait",
111142
action=argparse.BooleanOptionalAction,
112-
default=CreateJobCommandArgs.wait,
113-
help=f"Wait for job completion (default {CreateJobCommandArgs.wait})",
143+
default=CreateJobCommandArgs.get_default_for("wait"),
144+
help="Wait for job completion",
114145
)
115146
p.add_argument(
116147
"--timeout",
117-
default=CreateJobCommandArgs.timeout,
148+
default=CreateJobCommandArgs.get_default_for("timeout"),
118149
type=int,
119-
help=f"Wait timeout in seconds (default {CreateJobCommandArgs.timeout})",
150+
help="Wait timeout in seconds",
120151
)
121152
p.add_argument(
122153
"--max-sec",
123-
default=CreateJobCommandArgs.max_sec,
154+
default=CreateJobCommandArgs.get_default_for("max_sec"),
124155
type=int,
125-
help=f"Maximum execution time in seconds (default {CreateJobCommandArgs.max_sec})",
156+
help="Maximum execution time in seconds",
126157
)
127158
p.add_argument(
128159
"--gpu-numreq",
129-
default=CreateJobCommandArgs.gpu_numreq,
160+
default=CreateJobCommandArgs.get_default_for("gpu_numreq"),
130161
type=int,
131-
help=f"Number of GPUs requested (default {CreateJobCommandArgs.gpu_numreq})",
162+
help="Number of GPUs requested",
132163
)
133164
p.add_argument(
134165
"--gpu-numlim",
135-
default=CreateJobCommandArgs.gpu_numlim,
166+
default=CreateJobCommandArgs.get_default_for("gpu_numlim"),
136167
type=int,
137-
help=f"Number of GPUs limited (default {CreateJobCommandArgs.gpu_numlim})",
168+
help="Number of GPUs limited",
138169
)
139170
p.add_argument(
140171
"command",

defaults.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)