Skip to content

Commit 9398671

Browse files
committed
use types properly, add support for more arg types
1 parent 04e8eda commit 9398671

File tree

16 files changed

+344
-267
lines changed

16 files changed

+344
-267
lines changed

cli2gui/application/application.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ def run(buildSpec: types.FullBuildSpec) -> Any:
2424
"""
2525

2626
# Set the theme
27-
theme = helpers.get_base24_theme(buildSpec["theme"], buildSpec["darkTheme"])
27+
theme = helpers.get_base24_theme(buildSpec.theme, buildSpec.darkTheme)
2828

29-
if buildSpec["gui"] in [
29+
if buildSpec.gui in [
3030
"pysimplegui",
3131
"pysimpleguiqt",
3232
"pysimpleguiweb",
@@ -37,18 +37,18 @@ def run(buildSpec: types.FullBuildSpec) -> Any:
3737
gui_wrapper = DearPyGuiWrapper
3838

3939
if gui_wrapper is PySimpleGUIWrapper:
40-
gui = gui_wrapper(theme, buildSpec["gui"])
40+
gui = gui_wrapper(theme, buildSpec.gui)
4141
elif gui_wrapper is DearPyGuiWrapper:
4242
gui = gui_wrapper(theme)
4343

4444
def quit_callback() -> None:
4545
sys.exit(0)
4646

4747
def run_callback(values: dict[str, Any]) -> None:
48-
args = argFormat(values, buildSpec["parser"])
49-
if not buildSpec["run_function"]:
48+
args = argFormat(values, buildSpec.parser)
49+
if not buildSpec.run_function:
5050
return args
51-
buildSpec["run_function"](args)
51+
buildSpec.run_function(args)
5252
return None
5353

5454
try:

cli2gui/application/application2args.py

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,86 @@
33
from __future__ import annotations
44

55
import argparse
6+
import optparse
7+
from pathlib import Path
68
from typing import Any
79

8-
from cli2gui.types import ParserType
9-
10+
from cli2gui.types import ParserType, SEP
11+
12+
13+
def processValue(key:str, value:str) -> tuple[str, Any]:
14+
if SEP not in key:
15+
return key, value or None
16+
key, _type = key.split(SEP, maxsplit=1)
17+
if len(str(value)) == 0 or value is None:
18+
return key,None
19+
if _type == "ItemType.Bool":
20+
return key,bool(value)
21+
if _type == "ItemType.File":
22+
return key,open(value, encoding="utf-8")
23+
if _type == "ItemType.Path":
24+
return key,Path(value)
25+
if _type == "ItemType.Int":
26+
return key,int(value)
27+
if _type == "ItemType.Text":
28+
return key,value
29+
if _type == "ItemType.Float":
30+
return key,float(value)
31+
if _type == "ItemType.List":
32+
return key,value
33+
if _type == "ItemType.Tuple":
34+
return key,value
35+
if _type == "ItemType.DateTime":
36+
return key,value
37+
38+
return key,value
1039

1140
def argparseFormat(values: dict[str, Any]) -> argparse.Namespace:
1241
"""Format args for argparse."""
1342
args = {}
1443
for key in values:
15-
# Empty strings and paths
16-
if isinstance(values[key], str) and len(values[key]) == 0:
17-
args[key] = None
18-
# Paths end in '#', set the base key (used by argparse)
19-
elif key[-1] == "#": # File
20-
args[key[:-1]] = open(values[key], encoding="utf-8")
21-
else:
22-
args[key] = values[key]
44+
key, value = processValue(key, values[key])
45+
args[key] = value
2346
return argparse.Namespace(**args)
2447

2548

26-
def optparseFormat(values: dict[str, Any]) -> dict[str, Any]:
49+
def optparseFormat(values: dict[str, Any]) -> tuple[optparse.Values, list[str]]:
2750
"""Format args for optparse."""
2851
args = {}
2952
for key in values:
30-
args[key] = values[key] if values[key] else None
31-
return args
53+
key, value = processValue(key, values[key])
54+
args[key] = value
55+
return (optparse.Values(args), [])
3256

3357

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

3866

3967
def docoptFormat(values: dict[str, Any]) -> dict[str, Any]:
4068
"""Format args for docopt."""
69+
import docopt
4170
args = {}
4271
for key in values:
43-
args[key] = (
44-
values[key] if not (isinstance(values[key], str) and len(values[key]) == 0) else None
45-
)
46-
return args
72+
key, value = processValue(key, values[key])
73+
args[key] = value
74+
return docopt.Dict(args)
4775

4876

4977
def clickFormat(values: dict[str, Any]) -> list[Any]:
5078
"""Format args for click."""
5179
args = []
5280
for key in values:
81+
5382
val = str(values[key])
5483
if not callable(key) and len(val) > 0:
55-
args.extend([key, val])
84+
key, value = processValue(key, values[key])
85+
args.extend([key, value])
5686
return args
5787

5888

cli2gui/decorators.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ 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(
66+
buildSpec.program_name = buildSpec.program_name or Path(sys.argv[0]).name.replace(
6767
".py", ""
6868
)
6969

7070
# CUSTOM: this seems like a pretty poor pattern to use...
71-
if buildSpec["parser"] == ParserType.CUSTOM:
72-
buildSpec["parser"] = input(
71+
if buildSpec.parser == ParserType.CUSTOM:
72+
buildSpec.parser = input(
7373
f"!Custom parser selected! Choose one of: {[x.value for x in ParserType]}"
7474
)
75-
if buildSpec["parser"] not in ParserType._value2member_map_: # noqa: SLF001
75+
if buildSpec.parser not in ParserType._value2member_map_: # noqa: SLF001
7676
msg = f"!Custom parser must be one of: {[x.value for x in ParserType]}"
7777
raise RuntimeError(msg)
7878

79-
parser = buildSpec["parser"]
79+
parser = buildSpec.parser
8080
# Select parser
8181
convertMap = {
8282
"self": {
@@ -90,13 +90,13 @@ def createFromParser(
9090
},
9191
}
9292
if parser in convertMap["self"]:
93-
return FullBuildSpec(**convertMap["self"][parser](selfParser), **buildSpec)
93+
return FullBuildSpec(**convertMap["self"][parser](selfParser).__dict__, **buildSpec.__dict__)
9494
if parser in convertMap["args"]:
95-
return FullBuildSpec(**convertMap["args"][parser](argsParser), **buildSpec)
95+
return FullBuildSpec(**convertMap["args"][parser](argsParser).__dict__, **buildSpec.__dict__)
9696

9797
# click is unique in behaviour so we cant use the mapping -_-
9898
if parser == ParserType.CLICK:
99-
return FullBuildSpec(**click2json.convert(buildSpec["run_function"]), **buildSpec)
99+
return FullBuildSpec(**click2json.convert(buildSpec.run_function).__dict__, **buildSpec.__dict__)
100100

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

0 commit comments

Comments
 (0)