Skip to content

Commit a8694e8

Browse files
committed
pre-commit
1 parent 9398671 commit a8694e8

File tree

12 files changed

+178
-175
lines changed

12 files changed

+178
-175
lines changed

cli2gui/application/application2args.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,79 @@
77
from pathlib import Path
88
from typing import Any
99

10-
from cli2gui.types import ParserType, SEP
10+
from cli2gui.types import SEP, ParserType
1111

1212

13-
def processValue(key:str, value:str) -> tuple[str, Any]:
13+
def processValue(key: str, value: str) -> tuple[str, Any]:
1414
if SEP not in key:
1515
return key, value or None
1616
key, _type = key.split(SEP, maxsplit=1)
1717
if len(str(value)) == 0 or value is None:
18-
return key,None
18+
return key, None
1919
if _type == "ItemType.Bool":
20-
return key,bool(value)
20+
return key, bool(value)
2121
if _type == "ItemType.File":
22-
return key,open(value, encoding="utf-8")
22+
return key, open(value, encoding="utf-8")
2323
if _type == "ItemType.Path":
24-
return key,Path(value)
24+
return key, Path(value)
2525
if _type == "ItemType.Int":
26-
return key,int(value)
26+
return key, int(value)
2727
if _type == "ItemType.Text":
28-
return key,value
28+
return key, value
2929
if _type == "ItemType.Float":
30-
return key,float(value)
30+
return key, float(value)
3131
if _type == "ItemType.List":
32-
return key,value
32+
return key, value
3333
if _type == "ItemType.Tuple":
34-
return key,value
34+
return key, value
3535
if _type == "ItemType.DateTime":
36-
return key,value
36+
return key, value
37+
38+
return key, value
3739

38-
return key,value
3940

4041
def argparseFormat(values: dict[str, Any]) -> argparse.Namespace:
4142
"""Format args for argparse."""
4243
args = {}
4344
for key in values:
44-
key, value = processValue(key, values[key])
45-
args[key] = value
45+
cleankey, value = processValue(key, values[key])
46+
args[cleankey] = value
4647
return argparse.Namespace(**args)
4748

4849

4950
def optparseFormat(values: dict[str, Any]) -> tuple[optparse.Values, list[str]]:
5051
"""Format args for optparse."""
5152
args = {}
5253
for key in values:
53-
key, value = processValue(key, values[key])
54-
args[key] = value
54+
cleankey, value = processValue(key, values[key])
55+
args[cleankey] = value
5556
return (optparse.Values(args), [])
5657

5758

5859
def getoptFormat(values: dict[str, Any]) -> tuple[list[Any], list[Any]]:
5960
"""Format args for getopt."""
60-
return ([
61-
processValue(key, values[key])
62-
for key in values if values[key]],
63-
[]
64-
)
61+
return ([processValue(key, values[key]) for key in values if values[key]], [])
6562

6663

6764
def docoptFormat(values: dict[str, Any]) -> dict[str, Any]:
6865
"""Format args for docopt."""
6966
import docopt
67+
7068
args = {}
7169
for key in values:
72-
key, value = processValue(key, values[key])
73-
args[key] = value
70+
cleankey, value = processValue(key, values[key])
71+
args[cleankey] = value
7472
return docopt.Dict(args)
7573

7674

7775
def clickFormat(values: dict[str, Any]) -> list[Any]:
7876
"""Format args for click."""
7977
args = []
8078
for key in values:
81-
8279
val = str(values[key])
8380
if not callable(key) and len(val) > 0:
84-
key, value = processValue(key, values[key])
85-
args.extend([key, value])
81+
cleankey, value = processValue(key, values[key])
82+
args.extend([cleankey, value])
8683
return args
8784

8885

@@ -99,7 +96,6 @@ def argFormat(values: dict[str, Any], argumentParser: str | ParserType) -> Any:
9996
Any: args
10097
10198
"""
102-
formattedArgs = None
10399
convertMap = {
104100
ParserType.OPTPARSE: optparseFormat,
105101
ParserType.ARGPARSE: argparseFormat,
@@ -110,4 +106,4 @@ def argFormat(values: dict[str, Any], argumentParser: str | ParserType) -> Any:
110106
}
111107
if argumentParser in convertMap:
112108
return convertMap[argumentParser](values)
113-
return formattedArgs
109+
return None

cli2gui/decorators.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ def createFromParser(
6363
runCmd = quote(sourcePath)
6464
else:
6565
runCmd = f"{quote(sys.executable)} -u {quote(sourcePath)}"
66-
buildSpec.program_name = buildSpec.program_name or Path(sys.argv[0]).name.replace(
67-
".py", ""
68-
)
66+
buildSpec.program_name = buildSpec.program_name or Path(sys.argv[0]).name.replace(".py", "")
6967

7068
# CUSTOM: this seems like a pretty poor pattern to use...
7169
if buildSpec.parser == ParserType.CUSTOM:
@@ -90,13 +88,19 @@ def createFromParser(
9088
},
9189
}
9290
if parser in convertMap["self"]:
93-
return FullBuildSpec(**convertMap["self"][parser](selfParser).__dict__, **buildSpec.__dict__)
91+
return FullBuildSpec(
92+
**convertMap["self"][parser](selfParser).__dict__, **buildSpec.__dict__
93+
)
9494
if parser in convertMap["args"]:
95-
return FullBuildSpec(**convertMap["args"][parser](argsParser).__dict__, **buildSpec.__dict__)
95+
return FullBuildSpec(
96+
**convertMap["args"][parser](argsParser).__dict__, **buildSpec.__dict__
97+
)
9698

9799
# click is unique in behaviour so we cant use the mapping -_-
98100
if parser == ParserType.CLICK:
99-
return FullBuildSpec(**click2json.convert(buildSpec.run_function).__dict__, **buildSpec.__dict__)
101+
return FullBuildSpec(
102+
**click2json.convert(buildSpec.run_function).__dict__, **buildSpec.__dict__
103+
)
100104

101105
msg = f"!Parser must be one of: {[x.value for x in ParserType]}"
102106
raise RuntimeError(msg)

cli2gui/gui/abstract_gui.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class AbstractGUI(ABC):
99
"""Abstract base class for GUI wrappers."""
1010

11+
@abstractmethod
1112
def __init__(self) -> None:
1213
pass
1314

cli2gui/gui/dearpygui_wrapper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import dearpygui.dearpygui as dpg
77

8-
from cli2gui.types import SEP, Item, ItemType, Group, FullBuildSpec
98
from cli2gui.gui import helpers
109
from cli2gui.gui.abstract_gui import AbstractGUI
10+
from cli2gui.types import SEP, FullBuildSpec, Group, Item, ItemType
1111

1212
THISDIR = Path(__file__).resolve().parent
1313

@@ -30,7 +30,7 @@ def __init__(self, base24Theme: list[str]) -> None:
3030

3131
def _helpText(self, item: Item) -> None:
3232
dpg.add_text(
33-
helpers.stringSentencecase(f'\n- {item.dest}: {item.commands}'),
33+
helpers.stringSentencecase(f"\n- {item.dest}: {item.commands}"),
3434
color=hex_to_rgb(self.base24Theme[13]),
3535
)
3636
dpg.add_text(helpers.stringSentencecase(item.help))
@@ -304,7 +304,7 @@ def main(
304304

305305
# Define "Run" and "Exit" buttons
306306
def _run_callback() -> None:
307-
_items = [item for item in items if getattr(item, "dest")]
307+
_items = [item for item in items if item.dest]
308308
myd = {f"{item.dest}{SEP}{item.type}": dpg.get_value(item.dest) for item in _items}
309309
run_callback(myd)
310310

cli2gui/gui/pysimplegui_wrapper.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
from PIL import Image, ImageTk
88

9-
from cli2gui.types import ItemType, Item, SEP, FullBuildSpec
109
from cli2gui.gui import helpers
1110
from cli2gui.gui.abstract_gui import AbstractGUI
11+
from cli2gui.types import SEP, FullBuildSpec, Group, Item, ItemType
12+
13+
# ruff: noqa: ANN401
14+
1215

1316
class PySimpleGUIWrapper(AbstractGUI):
1417
"""Wrapper class for PySimpleGUI."""
@@ -123,7 +126,9 @@ def _dropdown(self, key: str, argItems: list[str]) -> Any:
123126
key=key,
124127
)
125128

126-
def _fileBrowser(self, key: str, default: str | None = None, _type: ItemType = ItemType.File) -> list[Any]:
129+
def _fileBrowser(
130+
self, key: str, default: str | None = None, _type: ItemType = ItemType.File
131+
) -> list[Any]:
127132
"""Return a fileBrowser button and field."""
128133
height = self.sizes["input_size"][1]
129134
width = self.sizes["input_size"][0]
@@ -154,9 +159,7 @@ def _helpArgHelp(self, helpText: str) -> Any:
154159
"""Return a label for the arg help text."""
155160
return self._label(helpers.stringSentencecase(helpText))
156161

157-
def _helpArgNameAndHelp(
158-
self, commands: list[str], helpText: str, displayName: str
159-
) -> Any:
162+
def _helpArgNameAndHelp(self, commands: list[str], helpText: str, displayName: str) -> Any:
160163
"""Return a column containing the argument name and help text."""
161164
return self.sg.Column(
162165
[[self._helpArgName(displayName, commands)], [self._helpArgHelp(helpText)]],
@@ -189,7 +192,9 @@ def _helpFlagWidget(
189192
"""Return a set of self that make up an arg with true/ false."""
190193
return [
191194
self._helpArgNameAndHelp(item.commands, item.help, item.display_name),
192-
self.sg.Column([[self._check(f"{item.dest}{SEP}{item.type}", default=item.default)]], pad=(0, 0)),
195+
self.sg.Column(
196+
[[self._check(f"{item.dest}{SEP}{item.type}", default=item.default)]], pad=(0, 0)
197+
),
193198
]
194199

195200
def _helpTextWidget(
@@ -199,7 +204,10 @@ def _helpTextWidget(
199204
"""Return a set of self that make up an arg with text."""
200205
return [
201206
self._helpArgNameAndHelp(item.commands, item.help, item.display_name),
202-
self.sg.Column([[self._inputText(f"{item.dest}{SEP}{item.type}", default=item.default)]], pad=(0, 0)),
207+
self.sg.Column(
208+
[[self._inputText(f"{item.dest}{SEP}{item.type}", default=item.default)]],
209+
pad=(0, 0),
210+
),
203211
]
204212

205213
def _helpCounterWidget(
@@ -209,7 +217,9 @@ def _helpCounterWidget(
209217
"""Return a set of self that make up an arg with text."""
210218
return [
211219
self._helpArgNameAndHelp(item.commands, item.help, item.display_name),
212-
self.sg.Column([[self._spin(f"{item.dest}{SEP}{item.type}", default=item.default)]], pad=(0, 0)),
220+
self.sg.Column(
221+
[[self._spin(f"{item.dest}{SEP}{item.type}", default=item.default)]], pad=(0, 0)
222+
),
213223
]
214224

215225
def _helpFileWidget(
@@ -230,7 +240,13 @@ def _helpDropdownWidget(
230240
return [
231241
self._helpArgNameAndHelp(item.commands, item.help, item.display_name),
232242
self.sg.Column(
233-
[[self._dropdown(f"{item.dest}{SEP}{item.type}", item.additional_properties["choices"])]],
243+
[
244+
[
245+
self._dropdown(
246+
f"{item.dest}{SEP}{item.type}", item.additional_properties["choices"]
247+
)
248+
]
249+
],
234250
pad=(0, 0),
235251
),
236252
]

cli2gui/tojson/argparse2json.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from sys import argv
2121
from typing import Any, Generator, TypedDict
2222

23-
from cli2gui.types import ParserRep, Group, Item, ItemType
23+
from cli2gui.types import Group, Item, ItemType, ParserRep
2424

2525

2626
class ArgparseGroup(TypedDict):
@@ -111,29 +111,29 @@ def extractRawGroups(actionGroup: argparse._ArgumentGroup) -> ArgparseGroup:
111111
def actionToJson(action: argparse.Action, widget: ItemType) -> Item:
112112
"""Generate json for an action and set the widget - used by the application."""
113113
choices = [str(choice) for choice in action.choices] if action.choices else []
114-
return Item(**{
115-
"type": widget,
116-
"display_name": str(action.metavar or action.dest),
117-
"help": str(action.help),
118-
"commands": list(action.option_strings),
119-
"dest": action.dest,
120-
"default": action.default,
121-
"additional_properties": {"choices": choices, "nargs": action.nargs},
122-
})
114+
return Item(
115+
type=widget,
116+
display_name=str(action.metavar or action.dest),
117+
help=str(action.help),
118+
commands=list(action.option_strings),
119+
dest=action.dest,
120+
default=action.default,
121+
additional_properties={"choices": choices, "nargs": action.nargs},
122+
)
123123

124124

125125
def buildRadioGroup(mutexGroup: _MutuallyExclusiveGroup) -> Item:
126126
"""Create a radio group for a mutex group of arguments."""
127127
commands = [action.option_strings for action in mutexGroup._group_actions]
128-
return Item(**{
129-
"display_name": "",
130-
"help": "",
131-
"dest": "",
132-
"default": "",
133-
"type": ItemType.RadioGroup,
134-
"commands": commands,
135-
"additional_properties": {"radio": list(categorizeItems(mutexGroup._group_actions))},
136-
} )
128+
return Item(
129+
display_name="",
130+
help="",
131+
dest="",
132+
default="",
133+
type=ItemType.RadioGroup,
134+
commands=commands,
135+
additional_properties={"radio": list(categorizeItems(mutexGroup._group_actions))},
136+
)
137137

138138

139139
def categorizeItems(
@@ -164,11 +164,11 @@ def categorizeItems(
164164
def categorizeGroups(groups: list[ArgparseGroup]) -> list[Group]:
165165
"""Categorize the parser groups and arg_items."""
166166
return [
167-
Group(**{
168-
"name": group["name"],
169-
"arg_items": list(categorizeItems(group["arg_items"])),
170-
"groups": categorizeGroups(group["groups"]),
171-
})
167+
Group(
168+
name=group["name"],
169+
arg_items=list(categorizeItems(group["arg_items"])),
170+
groups=categorizeGroups(group["groups"]),
171+
)
172172
for group in groups
173173
]
174174

@@ -204,4 +204,6 @@ def convert(parser: argparse.ArgumentParser) -> ParserRep:
204204
for _, subparser in iterParsers(parser):
205205
widgets.extend(process(subparser))
206206

207-
return ParserRep(parser_description= f"{parser.prog}: {parser.description or ''}", widgets= widgets)
207+
return ParserRep(
208+
parser_description=f"{parser.prog}: {parser.description or ''}", widgets=widgets
209+
)

0 commit comments

Comments
 (0)