-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapplication2args.py
More file actions
112 lines (90 loc) · 2.87 KB
/
application2args.py
File metadata and controls
112 lines (90 loc) · 2.87 KB
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
"""Functions to create args from key/value pairs."""
from __future__ import annotations
import argparse
import optparse
from pathlib import Path
from typing import Any
from cli2gui.models import SEP, ParserType
def processValue(key: str, value: str) -> tuple[str, Any]:
if SEP not in key:
return key, value or None
key, _type = key.split(SEP, maxsplit=1)
if len(str(value)) == 0 or value is None:
return key, None
if _type == "ItemType.Bool":
return key, bool(value)
if "ItemType.File" in _type:
_, mode, encoding = _type.split(";", maxsplit=2)
if "b" in mode:
return key, open(value, mode=mode)
return key, open(value, mode=mode, encoding=encoding)
if _type == "ItemType.Path":
return key, Path(value)
if _type == "ItemType.Int":
return key, int(value)
if _type == "ItemType.Text":
return key, value
if _type == "ItemType.Float":
return key, float(value)
if _type == "ItemType.List":
return key, value
if _type == "ItemType.Tuple":
return key, value
if _type == "ItemType.DateTime":
return key, value
return key, value
def argparseFormat(values: dict[str, Any]) -> argparse.Namespace:
"""Format args for argparse."""
args = {}
for key, _value in values.items():
cleankey, value = processValue(key, _value)
args[cleankey] = value
return argparse.Namespace(**args)
def optparseFormat(values: dict[str, Any]) -> tuple[optparse.Values, list[str]]:
"""Format args for optparse."""
args = {}
for key, _value in values.items():
cleankey, value = processValue(key, _value)
args[cleankey] = value
return (optparse.Values(args), [])
def getoptFormat(values: dict[str, Any]) -> tuple[list[Any], list[Any]]:
"""Format args for getopt."""
return ([processValue(key, _value) for key, _value in values.items() if _value], [])
def docoptFormat(values: dict[str, Any]) -> dict[str, Any]:
"""Format args for docopt."""
import docopt
args = {}
for key, _value in values.items():
cleankey, value = processValue(key, _value)
args[cleankey] = value
return docopt.Dict(args)
def clickFormat(values: dict[str, Any]) -> list[Any]:
"""Format args for click."""
args = []
for key, _value in values.items():
val = str(_value)
if not callable(key) and len(val) > 0:
cleankey, value = processValue(key, _value)
args.extend([cleankey, value])
return args
def argFormat(values: dict[str, Any], argumentParser: str | ParserType) -> Any:
"""Format the args for the desired parser.
Args:
----
values (dict[str, Any]): values from simple gui
argumentParser (str): argument parser to use
Returns:
-------
Any: args
"""
convertMap = {
ParserType.OPTPARSE: optparseFormat,
ParserType.ARGPARSE: argparseFormat,
ParserType.DEPHELL_ARGPARSE: argparseFormat,
ParserType.DOCOPT: docoptFormat,
ParserType.GETOPT: getoptFormat,
ParserType.CLICK: clickFormat,
}
if argumentParser in convertMap:
return convertMap[argumentParser](values)
return None