-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcli_data.py
More file actions
122 lines (91 loc) · 3.57 KB
/
cli_data.py
File metadata and controls
122 lines (91 loc) · 3.57 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
113
114
115
116
117
118
119
120
121
122
import logging
import os
from pathlib import Path
from typing import Any
from multiversx_sdk_cli import cli_shared, errors, utils, workstation
logger = logging.getLogger("cli.data")
DATA_FILENAME = "mxpy.data-storage.json"
def setup_parser(subparsers: Any) -> Any:
parser = cli_shared.add_group_subparser(subparsers, "data", "Data manipulation omnitool")
subparsers = parser.add_subparsers()
sub = cli_shared.add_command_subparser(subparsers, "data", "parse", "Parses values from a given file")
sub.add_argument("--file", required=True, help="path of the file to parse")
sub.add_argument(
"--expression",
required=True,
help="the Python-Dictionary expression to evaluate in order to extract the data",
)
sub.set_defaults(func=parse)
sub = cli_shared.add_command_subparser(subparsers, "data", "store", "Stores a key-value pair within a partition")
sub.add_argument("--key", required=True, help="the key")
sub.add_argument("--value", required=True, help="the value to save")
sub.add_argument("--partition", default="*", help="the storage partition (default: %(default)s)")
sub.add_argument(
"--use-global",
action="store_true",
default=False,
help="use the global storage (default: %(default)s)",
)
sub.set_defaults(func=store)
sub = cli_shared.add_command_subparser(
subparsers, "data", "load", "Loads a key-value pair from a storage partition"
)
sub.add_argument("--key", required=True, help="the key")
sub.add_argument("--partition", default="*", help="the storage partition (default: %(default)s)")
sub.add_argument(
"--use-global",
action="store_true",
default=False,
help="use the global storage (default: %(default)s)",
)
sub.set_defaults(func=load)
parser.epilog = cli_shared.build_group_epilog(subparsers)
return subparsers
def parse(args: Any):
file = Path(args.file).expanduser()
expression: str = args.expression
suffix = file.suffix
data = None
if suffix == ".json":
data = utils.read_json_file(str(file))
else:
raise errors.BadUsage(f"File isn't parsable: {file}")
try:
result = eval(expression, {"data": data})
except KeyError:
result = ""
print(result)
def store(args: Any):
logger.warning("Never use this command to store sensitive information! Data is unencrypted.")
key = args.key
value = args.value
partition = args.partition
use_global = args.use_global
data = _read_file(use_global)
if partition not in data:
data[partition] = dict()
data_in_partition = data[partition]
data_in_partition[key] = value
_write_file(use_global, data)
logger.info(f"Data has been stored at key = '{key}', in partition = '{partition}'.")
def load(args: Any):
key = args.key
partition = args.partition
use_global = args.use_global
data = _read_file(use_global)
data_in_partition = data.get(partition, dict())
value = data_in_partition.get(key, "")
print(value)
def _read_file(use_global: bool) -> dict[str, Any]:
filename = _get_filename(use_global)
if not os.path.isfile(filename):
return dict()
data: dict[str, Any] = utils.read_json_file(filename)
return data
def _write_file(use_global: bool, data: dict[str, Any]):
filename = _get_filename(use_global)
utils.write_json_file(str(filename), data)
def _get_filename(use_global: bool):
if use_global:
return workstation.get_tools_folder() / DATA_FILENAME
return Path(os.getcwd()) / DATA_FILENAME