Mercurial > libervia-backend
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 |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
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 | 5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
1766 | 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 | 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 | 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 | 30 super(Get, self).__init__( |
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 | 34 self.parser.add_argument( |
35 "category", nargs='?', help=_("category of the parameter")) | |
36 self.parser.add_argument("name", nargs='?', help=_("name of the parameter")) | |
37 self.parser.add_argument( | |
38 "-a", "--attribute", type=str, default="value", | |
39 help=_("name of the attribute to get")) | |
40 self.parser.add_argument( | |
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 | 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 | 45 categories = await self.host.bridge.getParamsCategories() |
3028 | 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 | 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 | 52 self.disp(_(f"can't find requested parameters: {e}"), error=True) |
53 self.host.quit(C.EXIT_NOT_FOUND) | |
54 else: | |
55 for name, value in values_dict.items(): | |
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 | 59 value = await self.host.bridge.asyncGetParamA( |
60 self.args.name, self.args.category, self.args.attribute, | |
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 | 63 self.disp(_(f"can't find requested parameter: {e}"), error=True) |
64 self.host.quit(C.EXIT_NOT_FOUND) | |
65 else: | |
66 print(value) | |
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 | 70 class Set(base.CommandBase): |
71 def __init__(self, host): | |
3040 | 72 super(Set, self).__init__(host, 'set', need_connect=False, help=_('set a parameter value')) |
1593 | 73 |
74 def add_parser_options(self): | |
3040 | 75 self.parser.add_argument("category", help=_("category of the parameter")) |
76 self.parser.add_argument("name", help=_("name of the parameter")) | |
77 self.parser.add_argument("value", help=_("name of the parameter")) | |
78 self.parser.add_argument("--security-limit", type=int, default=-1, help=_("security limit")) | |
1593 | 79 |
3040 | 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 | 82 await self.host.bridge.setParam( |
83 self.args.name, self.args.value, self.args.category, | |
84 self.args.security_limit, self.profile) | |
1593 | 85 except Exception as e: |
3040 | 86 self.disp(_(f"can't set requested parameter: {e}"), error=True) |
87 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
88 else: | |
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 | 93 # FIXME: this should probably be removed, it's not used and not useful for end-user |
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 | 96 super(SaveTemplate, self).__init__( |
97 host, 'save', use_profile=False, | |
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 | 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 | 103 async def start(self): |
104 """Save parameters template to XML file""" | |
105 try: | |
106 await self.host.bridge.saveParamsTemplate(self.args.filename) | |
107 except Exception as e: | |
108 self.disp(_(f"can't save parameters to file: {e}"), error=True) | |
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 | 111 self.disp(_(f"parameters saved to file {self.args.filename}")) |
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 | 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 | 119 super(LoadTemplate, self).__init__( |
120 host, 'load', use_profile=False, | |
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 | 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 | 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 | 128 try: |
129 self.host.bridge.loadParamsTemplate(self.args.filename) | |
130 except Exception as e: | |
131 self.disp(_(f"can't load parameters from file: {e}"), error=True) | |
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 | 134 self.disp(_(f"parameters loaded from file {self.args.filename}")) |
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 | 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')) |