comparison libervia/cli/cmd_param.py @ 4075:47401850dec6

refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:54:26 +0200
parents libervia/frontends/jp/cmd_param.py@26b7ed2817da
children
comparison
equal deleted inserted replaced
4074:26b7ed2817da 4075:47401850dec6
1 #!/usr/bin/env python3
2
3
4 # Libervia CLI
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
22 from . import base
23 from libervia.backend.core.i18n import _
24 from .constants import Const as C
25
26 __commands__ = ["Param"]
27
28
29 class Get(base.CommandBase):
30 def __init__(self, host):
31 super(Get, self).__init__(
32 host, "get", need_connect=False, help=_("get a parameter value")
33 )
34
35 def add_parser_options(self):
36 self.parser.add_argument(
37 "category", nargs="?", help=_("category of the parameter")
38 )
39 self.parser.add_argument("name", nargs="?", help=_("name of the parameter"))
40 self.parser.add_argument(
41 "-a",
42 "--attribute",
43 type=str,
44 default="value",
45 help=_("name of the attribute to get"),
46 )
47 self.parser.add_argument(
48 "--security-limit", type=int, default=-1, help=_("security limit")
49 )
50
51 async def start(self):
52 if self.args.category is None:
53 categories = await self.host.bridge.params_categories_get()
54 print("\n".join(categories))
55 elif self.args.name is None:
56 try:
57 values_dict = await self.host.bridge.params_values_from_category_get_async(
58 self.args.category, self.args.security_limit, "", "", self.profile
59 )
60 except Exception as e:
61 self.disp(
62 _("can't find requested parameters: {e}").format(e=e), error=True
63 )
64 self.host.quit(C.EXIT_NOT_FOUND)
65 else:
66 for name, value in values_dict.items():
67 print(f"{name}\t{value}")
68 else:
69 try:
70 value = await self.host.bridge.param_get_a_async(
71 self.args.name,
72 self.args.category,
73 self.args.attribute,
74 self.args.security_limit,
75 self.profile,
76 )
77 except Exception as e:
78 self.disp(
79 _("can't find requested parameter: {e}").format(e=e), error=True
80 )
81 self.host.quit(C.EXIT_NOT_FOUND)
82 else:
83 print(value)
84 self.host.quit()
85
86
87 class Set(base.CommandBase):
88 def __init__(self, host):
89 super(Set, self).__init__(
90 host, "set", need_connect=False, help=_("set a parameter value")
91 )
92
93 def add_parser_options(self):
94 self.parser.add_argument("category", help=_("category of the parameter"))
95 self.parser.add_argument("name", help=_("name of the parameter"))
96 self.parser.add_argument("value", help=_("name of the parameter"))
97 self.parser.add_argument(
98 "--security-limit", type=int, default=-1, help=_("security limit")
99 )
100
101 async def start(self):
102 try:
103 await self.host.bridge.param_set(
104 self.args.name,
105 self.args.value,
106 self.args.category,
107 self.args.security_limit,
108 self.profile,
109 )
110 except Exception as e:
111 self.disp(_("can't set requested parameter: {e}").format(e=e), error=True)
112 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
113 else:
114 self.host.quit()
115
116
117 class SaveTemplate(base.CommandBase):
118 # FIXME: this should probably be removed, it's not used and not useful for end-user
119
120 def __init__(self, host):
121 super(SaveTemplate, self).__init__(
122 host,
123 "save",
124 use_profile=False,
125 help=_("save parameters template to xml file"),
126 )
127
128 def add_parser_options(self):
129 self.parser.add_argument("filename", type=str, help=_("output file"))
130
131 async def start(self):
132 """Save parameters template to XML file"""
133 try:
134 await self.host.bridge.params_template_save(self.args.filename)
135 except Exception as e:
136 self.disp(_("can't save parameters to file: {e}").format(e=e), error=True)
137 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
138 else:
139 self.disp(
140 _("parameters saved to file {filename}").format(
141 filename=self.args.filename
142 )
143 )
144 self.host.quit()
145
146
147 class LoadTemplate(base.CommandBase):
148 # FIXME: this should probably be removed, it's not used and not useful for end-user
149
150 def __init__(self, host):
151 super(LoadTemplate, self).__init__(
152 host,
153 "load",
154 use_profile=False,
155 help=_("load parameters template from xml file"),
156 )
157
158 def add_parser_options(self):
159 self.parser.add_argument("filename", type=str, help=_("input file"))
160
161 async def start(self):
162 """Load parameters template from xml file"""
163 try:
164 self.host.bridge.params_template_load(self.args.filename)
165 except Exception as e:
166 self.disp(_("can't load parameters from file: {e}").format(e=e), error=True)
167 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
168 else:
169 self.disp(
170 _("parameters loaded from file {filename}").format(
171 filename=self.args.filename
172 )
173 )
174 self.host.quit()
175
176
177 class Param(base.CommandBase):
178 subcommands = (Get, Set, SaveTemplate, LoadTemplate)
179
180 def __init__(self, host):
181 super(Param, self).__init__(
182 host, "param", use_profile=False, help=_("Save/load parameters template")
183 )