annotate sat_frontends/jp/cmd_param.py @ 3433:63d20854df3a

core (constants): added EXIT_CONFLICT
author Goffi <goffi@goffi.org>
date Fri, 04 Dec 2020 12:30:46 +0100
parents 559a625a236b
children be6d91572633
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
3
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # jp: a SAT command line tool
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3123
diff changeset
5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
7
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
12
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
17
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
20
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
21
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
22 from . import base
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
23 from sat.core.i18n import _
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
24 from .constants import Const as C
1545
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
25 __commands__ = ["Param"]
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
26
1545
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
27
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
28 class Get(base.CommandBase):
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
29 def __init__(self, host):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
30 super(Get, self).__init__(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
31 host, 'get', need_connect=False, help=_('get a parameter value'))
1545
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
32
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
33 def add_parser_options(self):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
34 self.parser.add_argument(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
35 "category", nargs='?', help=_("category of the parameter"))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
36 self.parser.add_argument("name", nargs='?', help=_("name of the parameter"))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
37 self.parser.add_argument(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
38 "-a", "--attribute", type=str, default="value",
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
39 help=_("name of the attribute to get"))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
40 self.parser.add_argument(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
41 "--security-limit", type=int, default=-1, help=_("security limit"))
1545
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
42
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
43 async def start(self):
1589
add1a6c8c594 jp: optional positional arguments on "param get" command
Goffi <goffi@goffi.org>
parents: 1545
diff changeset
44 if self.args.category is None:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
45 categories = await self.host.bridge.getParamsCategories()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
46 print("\n".join(categories))
1589
add1a6c8c594 jp: optional positional arguments on "param get" command
Goffi <goffi@goffi.org>
parents: 1545
diff changeset
47 elif self.args.name is None:
add1a6c8c594 jp: optional positional arguments on "param get" command
Goffi <goffi@goffi.org>
parents: 1545
diff changeset
48 try:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
49 values_dict = await self.host.bridge.asyncGetParamsValuesFromCategory(
3123
130f9cb6e0ab core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
50 self.args.category, self.args.security_limit, "", "", self.profile)
1589
add1a6c8c594 jp: optional positional arguments on "param get" command
Goffi <goffi@goffi.org>
parents: 1545
diff changeset
51 except Exception as e:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
52 self.disp(_(f"can't find requested parameters: {e}"), error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
53 self.host.quit(C.EXIT_NOT_FOUND)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
54 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
55 for name, value in values_dict.items():
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
56 print(f"{name}\t{value}")
1589
add1a6c8c594 jp: optional positional arguments on "param get" command
Goffi <goffi@goffi.org>
parents: 1545
diff changeset
57 else:
add1a6c8c594 jp: optional positional arguments on "param get" command
Goffi <goffi@goffi.org>
parents: 1545
diff changeset
58 try:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
59 value = await self.host.bridge.asyncGetParamA(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
60 self.args.name, self.args.category, self.args.attribute,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
61 self.args.security_limit, self.profile)
1589
add1a6c8c594 jp: optional positional arguments on "param get" command
Goffi <goffi@goffi.org>
parents: 1545
diff changeset
62 except Exception as e:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
63 self.disp(_(f"can't find requested parameter: {e}"), error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
64 self.host.quit(C.EXIT_NOT_FOUND)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
65 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
66 print(value)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
67 self.host.quit()
1589
add1a6c8c594 jp: optional positional arguments on "param get" command
Goffi <goffi@goffi.org>
parents: 1545
diff changeset
68
add1a6c8c594 jp: optional positional arguments on "param get" command
Goffi <goffi@goffi.org>
parents: 1545
diff changeset
69
1593
791c45ed8659 jp: added param/set command
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
70 class Set(base.CommandBase):
791c45ed8659 jp: added param/set command
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
71 def __init__(self, host):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
72 super(Set, self).__init__(host, 'set', need_connect=False, help=_('set a parameter value'))
1593
791c45ed8659 jp: added param/set command
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
73
791c45ed8659 jp: added param/set command
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
74 def add_parser_options(self):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
75 self.parser.add_argument("category", help=_("category of the parameter"))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
76 self.parser.add_argument("name", help=_("name of the parameter"))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
77 self.parser.add_argument("value", help=_("name of the parameter"))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
78 self.parser.add_argument("--security-limit", type=int, default=-1, help=_("security limit"))
1593
791c45ed8659 jp: added param/set command
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
79
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
80 async def start(self):
1545
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
81 try:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
82 await self.host.bridge.setParam(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
83 self.args.name, self.args.value, self.args.category,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
84 self.args.security_limit, self.profile)
1593
791c45ed8659 jp: added param/set command
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
85 except Exception as e:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
86 self.disp(_(f"can't set requested parameter: {e}"), error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
87 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
88 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
89 self.host.quit()
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
90
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
91
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
92 class SaveTemplate(base.CommandBase):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
93 # FIXME: this should probably be removed, it's not used and not useful for end-user
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
94
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
95 def __init__(self, host):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
96 super(SaveTemplate, self).__init__(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
97 host, 'save', use_profile=False,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
98 help=_('save parameters template to xml file'))
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
99
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
100 def add_parser_options(self):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
101 self.parser.add_argument("filename", type=str, help=_("output file"))
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
102
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
103 async def start(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
104 """Save parameters template to XML file"""
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
105 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
106 await self.host.bridge.saveParamsTemplate(self.args.filename)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
107 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
108 self.disp(_(f"can't save parameters to file: {e}"), error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
109 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
110 else:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
111 self.disp(_(f"parameters saved to file {self.args.filename}"))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
112 self.host.quit()
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
113
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
114
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
115 class LoadTemplate(base.CommandBase):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
116 # FIXME: this should probably be removed, it's not used and not useful for end-user
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
117
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
118 def __init__(self, host):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
119 super(LoadTemplate, self).__init__(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
120 host, 'load', use_profile=False,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
121 help=_('load parameters template from xml file'))
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
122
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
123 def add_parser_options(self):
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
124 self.parser.add_argument("filename", type=str, help=_("input file"))
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
125
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
126 async def start(self):
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
127 """Load parameters template from xml file"""
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
128 try:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
129 self.host.bridge.loadParamsTemplate(self.args.filename)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
130 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
131 self.disp(_(f"can't load parameters from file: {e}"), error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
132 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
133 else:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
134 self.disp(_(f"parameters loaded from file {self.args.filename}"))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
135 self.host.quit()
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
136
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
137
1545
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
138 class Param(base.CommandBase):
1593
791c45ed8659 jp: added param/set command
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
139 subcommands = (Get, Set, SaveTemplate, LoadTemplate)
1015
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
140
fee00f2e11c2 memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
diff changeset
141 def __init__(self, host):
1545
b8ee774c12c8 jp: renamed “params” command to “param” for consistency with other commands + added a “get” subcommand
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
142 super(Param, self).__init__(host, 'param', use_profile=False, help=_('Save/load parameters template'))