-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdecorators.py
317 lines (275 loc) · 9.33 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""Decorator and entry point for the program."""
from __future__ import annotations
import getopt
import sys
import warnings
from argparse import ArgumentParser
from collections.abc import Callable
from optparse import OptionParser
from pathlib import Path
from shlex import quote
from typing import Any, Iterable
from cli2gui.application import application
from cli2gui.models import BuildSpec, FullBuildSpec, GUIType, ParserType
from cli2gui.tojson import (
argparse2json,
click2json,
docopt2json,
getopt2json,
optparse2json,
)
DO_COMMAND = "--cli2gui"
DO_NOT_COMMAND = "--disable-cli2gui"
def createFromParser(
selfParser: Any,
argsParser: tuple[Any, ...],
kwargsParser: dict[Any, Any],
sourcePath: str,
buildSpec: BuildSpec,
**kwargs: dict[Any, Any],
) -> FullBuildSpec:
"""Generate a buildSpec from a parser.
Args:
----
selfParser (Any): A parser that acts on self. eg. ArgumentParser.parse_args
argsParser (tuple[Any, ...]): A parser that acts on function
arguments. eg. getopt.getopt
kwargsParser (dict[Any, Any]): A parser that acts on named params
sourcePath (str): Program source path
buildSpec (BuildSpec): Build spec
**kwargs (dict[Any, Any]): kwargs
Returns:
-------
types.FullBuildSpec: buildSpec to be used by the application
Raises:
------
RuntimeError: Throw error if incorrect parser selected
"""
_ = kwargsParser
runCmd = kwargs.get("target")
if runCmd is None:
if hasattr(sys, "frozen"):
runCmd = quote(sourcePath)
else:
runCmd = f"{quote(sys.executable)} -u {quote(sourcePath)}"
buildSpec.program_name = buildSpec.program_name or Path(sys.argv[0]).name.replace(".py", "")
# CUSTOM: this seems like a pretty poor pattern to use...
if buildSpec.parser == ParserType.CUSTOM:
buildSpec.parser = input(
f"!Custom parser selected! Choose one of: {[x.value for x in ParserType]}"
)
if buildSpec.parser not in ParserType._value2member_map_:
msg = f"!Custom parser must be one of: {[x.value for x in ParserType]}"
raise RuntimeError(msg)
parser = buildSpec.parser
# Select parser
convertMap = {
"self": {
ParserType.OPTPARSE: optparse2json.convert,
ParserType.ARGPARSE: argparse2json.convert,
ParserType.DEPHELL_ARGPARSE: argparse2json.convert,
ParserType.DOCOPT: docopt2json.convert,
},
"args": {
ParserType.GETOPT: getopt2json.convert,
},
}
if parser in convertMap["self"]:
return FullBuildSpec(
**convertMap["self"][parser](selfParser).__dict__, **buildSpec.__dict__
)
if parser in convertMap["args"]:
return FullBuildSpec(
**convertMap["args"][parser](argsParser).__dict__, **buildSpec.__dict__
)
# click is unique in behaviour so we cant use the mapping -_-
if parser == ParserType.CLICK:
return FullBuildSpec(
**click2json.convert(buildSpec.run_function).__dict__, **buildSpec.__dict__
)
msg = f"!Parser must be one of: {[x.value for x in ParserType]}"
raise RuntimeError(msg)
def Click2Gui(
run_function: Callable[..., Any],
gui: str | GUIType = "dearpygui",
theme: str | list[str] = "",
darkTheme: str | list[str] = "",
image: str = "",
program_name: str = "",
program_description: str = "",
max_args_shown: int = 5,
menu: str | dict[str, Any] = "",
**kwargs: dict[str, Any],
) -> None:
"""Use this decorator in the function containing the argument parser.
Serializes data to JSON and launches the Cli2Gui application.
Args:
----
run_function (Callable[..., Any]): The name of the function to call eg.
gui (str, optional): Override the gui to use. Current options are:
"dearpygui", "pysimplegui", "pysimpleguiqt","pysimpleguiweb","freesimplegui",
Defaults to "dearpygui".
theme (Union[str, list[str]], optional): Set a base24 theme. Can
also pass a base24 scheme file. eg. one-light.yaml. Defaults to "".
darkTheme (Union[str, list[str]], optional): Set a base24 dark
theme variant. Can also pass a base24 scheme file. eg. one-dark.yaml.
Defaults to "".
image (str, optional): Set the program icon. File
extensions can be any that PIL supports. Defaults to "".
program_name (str, optional): Override the program name.
Defaults to "".
program_description (str, optional): Override the program
description. Defaults to "".
max_args_shown (int, optional): Maximum number of args shown before
using a scrollbar. Defaults to 5.
menu (Union[dict[str, Any]], optional): Add a menu to the program.
Defaults to "". eg. THIS_DIR = str(Path(__file__).resolve().parent)
menu={"File": THIS_DIR + "/file.md"}
**kwargs (dict[Any, Any]): kwargs
Returns:
-------
Any: Runs the application
"""
bSpec = BuildSpec(
run_function=run_function,
parser=ParserType.CLICK,
gui=gui,
theme=theme,
darkTheme=darkTheme,
image=image,
program_name=program_name,
program_description=program_description,
max_args_shown=max_args_shown,
menu=menu,
)
buildSpec = createFromParser(
None, (), kwargs, sys.argv[0], bSpec, **{**locals(), **locals()["kwargs"]}
)
return application.run(buildSpec)
def Cli2Gui(
run_function: Callable[..., Any],
auto_enable: bool = False,
parser: str | ParserType = "argparse",
gui: str | ParserType = "dearpygui",
theme: str | list[str] = "",
darkTheme: str | list[str] = "",
image: str = "",
program_name: str = "",
program_description: str = "",
max_args_shown: int = 5,
menu: str | dict[str, Any] = "",
) -> Any:
"""Use this decorator in the function containing the argument parser.
Serialises data to JSON and launches the Cli2Gui application.
Args:
----
run_function (Callable[..., Any]): The name of the function to call eg.
auto_enable (bool, optional): Enable the GUI by default. If enabled by
default requires `--disable-cli2gui`, otherwise requires `--cli2gui`.
Defaults to False.
parser (str, optional): Override the parser to use. Current
options are: "argparse", "getopt", "optparse", "docopt",
"dephell_argparse". Defaults to "argparse".
gui (str, optional): Override the gui to use. Current options are:
"dearpygui", "pysimplegui", "pysimpleguiqt","pysimpleguiweb","freesimplegui",
Defaults to "dearpygui".
theme (Union[str, list[str]], optional): Set a base24 theme. Can
also pass a base24 scheme file. eg. one-light.yaml. Defaults to "".
darkTheme (Union[str, list[str]], optional): Set a base24 dark
theme variant. Can also pass a base24 scheme file. eg. one-dark.yaml.
Defaults to "".
image (str, optional): Set the program icon. File
extensions can be any that PIL supports. Defaults to "".
program_name (str, optional): Override the program name.
Defaults to "".
program_description (str, optional): Override the program
description. Defaults to "".
max_args_shown (int, optional): Maximum number of args shown before
using a scrollbar. Defaults to 5.
menu (Union[dict[str, Any]], optional): Add a menu to the program.
Defaults to "". eg. THIS_DIR = str(Path(__file__).resolve().parent)
menu={"File": THIS_DIR + "/file.md"}
Returns:
-------
Any: Runs the application
"""
bSpec = BuildSpec(
run_function=run_function,
parser=parser,
gui=gui,
theme=theme,
darkTheme=darkTheme,
image=image,
program_name=program_name,
program_description=program_description,
max_args_shown=max_args_shown,
menu=menu,
)
def build(callingFunction: Callable[..., Any]) -> Callable[..., Any]:
"""Generate the buildspec and run the GUI.
Args:
----
callingFunction (Callable[..., Any]): The calling function eg.
ArgumentParser.parse_args
Returns:
-------
Callable[..., Any]: some calling function
"""
def runCli2Gui(self: Any, *args: Iterable[Any], **kwargs: dict[str, Any]) -> None:
"""Run the gui/ application.
:return None: the gui/ application
"""
buildSpec = createFromParser(
self,
args,
kwargs,
callingFunction.__name__,
bSpec,
**{**locals(), **locals()["kwargs"]},
)
return application.run(buildSpec)
def inner(*args: tuple[Any, Any], **kwargs: dict[Any, Any]) -> Any:
"""Replace the inner functions with run_cli2gui. eg. When.
ArgumentParser.parse_args is called, do run_cli2gui.
Returns
-------
Any: Do the calling_function
"""
getopt.getopt = runCli2Gui
getopt.gnu_getopt = runCli2Gui
OptionParser.parse_args = runCli2Gui
ArgumentParser.parse_args = runCli2Gui
try:
import docopt
docopt.docopt = runCli2Gui
except ImportError:
pass
try:
import dephell_argparse
dephell_argparse.Parser.parse_args = runCli2Gui
except ImportError:
pass
# Using type=argparse.FileType('r') leads to a resource warning
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=ResourceWarning)
return callingFunction(*args, **kwargs)
inner.__name__ = callingFunction.__name__
return inner
def runWithoutCli2Gui(callingFunction: Callable[..., Any]) -> Callable[..., Any]:
def inner(*args: Iterable[Any], **kwargs: dict[Any, Any]) -> Any:
# Using type=argparse.FileType('r') leads to a resource warning
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=ResourceWarning)
return callingFunction(*args, **kwargs)
inner.__name__ = callingFunction.__name__
return inner
"""If enabled by default requires do_not_command, otherwise requires do_command."""
if (not auto_enable and DO_COMMAND not in sys.argv) or (
auto_enable and DO_NOT_COMMAND in sys.argv
):
if DO_NOT_COMMAND in sys.argv:
sys.argv.remove(DO_NOT_COMMAND)
return runWithoutCli2Gui
return build
if __name__ == "__main__":
pass