Skip to content

Commit 6c132de

Browse files
authored
Merge pull request #9 from larsks/fix/remove-defaults.py
Remove defaults.py
2 parents dd3871f + a580f33 commit 6c132de

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

basecommand.py

Lines changed: 15 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,19 @@ 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. This means that argparse will
29+
care of documenting default values for us, and it won't reformat our
30+
carefully crafted help text."""
31+
32+
@override
33+
def _fill_text(
34+
self, text: str, width: int | None = None, indent: str | None = None
35+
):
36+
return "".join(indent + line for line in text.splitlines(keepends=True))
37+
38+
2539
class Command(abc.ABC):
2640
"""
2741
Base class for all command implementations.
@@ -37,7 +51,7 @@ def build_parser(cls, subparsers: SubParserFactory) -> argparse.ArgumentParser:
3751
cls.name,
3852
help=cls.help,
3953
description=cls.__doc__,
40-
formatter_class=argparse.RawDescriptionHelpFormatter,
54+
formatter_class=ArgumentDefaultsHelpFormatter,
4155
)
4256

4357
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: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
# pyright: reportUninitializedInstanceVariable=false
12
from typing import cast
2-
import typing_extensions
33
from typing_extensions import override
44

55
import argparse
66
import os
77
import socket
88
import sys
99
import time
10+
import uuid
1011

1112
import openshift_client as oc
1213

@@ -19,19 +20,27 @@
1920

2021

2122
class CreateJobCommandArgs(argparse.Namespace):
23+
"""This class serves two purposes:
24+
25+
1. It provides type hints that permit a properly configured IDE (or type
26+
checker) to perform type checking on command line option values.
27+
28+
2. It provides default values for command line options.
29+
"""
30+
2231
gpu: str = "v100"
2332
image: str = "image-registry.openshift-image-registry.svc:5000/redhat-ods-applications/csw-run-f25:latest"
2433
context: bool = True
2534
name: str = "job"
26-
job_id: int = os.getpid()
35+
job_id: str = uuid.uuid5(uuid.NAMESPACE_OID, f"{os.getpid()}-{time.time()}").hex
2736
job_delete: bool = True
2837
wait: bool = True
2938
timeout: int = 60 * 15 * 4
3039
max_sec: int = 60 * 15
3140
gpu_numreq: int = 1
3241
gpu_numlim: int = 1
3342
verbose: int = 0
34-
command: list[str] = []
43+
command: list[str]
3544

3645

3746
class CreateJobCommand(Command):
@@ -76,65 +85,64 @@ def build_parser(cls, subparsers: SubParserFactory):
7685
p.add_argument(
7786
"--gpu",
7887
default=CreateJobCommandArgs.gpu,
79-
help=f"Select GPU type (default {CreateJobCommandArgs.gpu})",
88+
help="Select GPU type",
8089
)
8190
p.add_argument(
8291
"--image",
8392
default=CreateJobCommandArgs.image,
84-
help=f"Specify container image for job (default {CreateJobCommandArgs.image})",
93+
help="Specify container image for job",
8594
)
8695
p.add_argument(
8796
"--context",
8897
action=argparse.BooleanOptionalAction,
8998
default=CreateJobCommandArgs.context,
90-
help=f"Copy working directory (default {CreateJobCommandArgs.context})",
99+
help="Copy working directory",
91100
)
92101
p.add_argument(
93102
"--name",
94103
default=CreateJobCommandArgs.name,
95-
help=f"Base job name (default {CreateJobCommandArgs.name})",
104+
help="Base job name",
96105
)
97106
p.add_argument(
98107
"--job-id",
99108
default=CreateJobCommandArgs.job_id,
100-
type=int,
101-
help="Job ID suffix (default current pid)",
109+
help="Job ID suffix",
102110
)
103111
p.add_argument(
104112
"--job-delete",
105113
action=argparse.BooleanOptionalAction,
106114
default=CreateJobCommandArgs.job_delete,
107-
help=f"Delete job on completion (default {CreateJobCommandArgs.job_delete})",
115+
help="Delete job on completion",
108116
)
109117
p.add_argument(
110118
"--wait",
111119
action=argparse.BooleanOptionalAction,
112120
default=CreateJobCommandArgs.wait,
113-
help=f"Wait for job completion (default {CreateJobCommandArgs.wait})",
121+
help="Wait for job completion",
114122
)
115123
p.add_argument(
116124
"--timeout",
117125
default=CreateJobCommandArgs.timeout,
118126
type=int,
119-
help=f"Wait timeout in seconds (default {CreateJobCommandArgs.timeout})",
127+
help="Wait timeout in seconds",
120128
)
121129
p.add_argument(
122130
"--max-sec",
123131
default=CreateJobCommandArgs.max_sec,
124132
type=int,
125-
help=f"Maximum execution time in seconds (default {CreateJobCommandArgs.max_sec})",
133+
help="Maximum execution time in seconds",
126134
)
127135
p.add_argument(
128136
"--gpu-numreq",
129137
default=CreateJobCommandArgs.gpu_numreq,
130138
type=int,
131-
help=f"Number of GPUs requested (default {CreateJobCommandArgs.gpu_numreq})",
139+
help="Number of GPUs requested",
132140
)
133141
p.add_argument(
134142
"--gpu-numlim",
135143
default=CreateJobCommandArgs.gpu_numlim,
136144
type=int,
137-
help=f"Number of GPUs limited (default {CreateJobCommandArgs.gpu_numlim})",
145+
help="Number of GPUs limited",
138146
)
139147
p.add_argument(
140148
"command",

defaults.py

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

0 commit comments

Comments
 (0)