comparison sat_frontends/jp/cmd_param.py @ 3040:fee60f17ebac

jp: jp asyncio port: /!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\ This patch implements the port of jp to asyncio, so it is now correctly using the bridge asynchronously, and it can be used with bridges like `pb`. This also simplify the code, notably for things which were previously implemented with many callbacks (like pagination with RSM). During the process, some behaviours have been modified/fixed, in jp and backends, check diff for details.
author Goffi <goffi@goffi.org>
date Wed, 25 Sep 2019 08:56:41 +0200
parents ab2696e34d29
children 130f9cb6e0ab
comparison
equal deleted inserted replaced
3039:a1bc34f90fa5 3040:fee60f17ebac
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20
21 21
22 from . import base 22 from . import base
23 from sat.core.i18n import _ 23 from sat.core.i18n import _
24 from .constants import Const as C
24 __commands__ = ["Param"] 25 __commands__ = ["Param"]
25 26
26 27
27 class Get(base.CommandBase): 28 class Get(base.CommandBase):
28 def __init__(self, host): 29 def __init__(self, host):
29 super(Get, self).__init__(host, 'get', need_connect=False, help=_('Get a parameter value')) 30 super(Get, self).__init__(
31 host, 'get', need_connect=False, help=_('get a parameter value'))
30 32
31 def add_parser_options(self): 33 def add_parser_options(self):
32 self.parser.add_argument("category", nargs='?', help=_("Category of the parameter")) 34 self.parser.add_argument(
33 self.parser.add_argument("name", nargs='?', help=_("Name of the parameter")) 35 "category", nargs='?', help=_("category of the parameter"))
34 self.parser.add_argument("-a", "--attribute", type=str, default="value", help=_("Name of the attribute to get")) 36 self.parser.add_argument("name", nargs='?', help=_("name of the parameter"))
35 self.parser.add_argument("--security-limit", type=int, default=-1, help=_("Security limit")) 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"))
36 42
37 def start(self): 43 async def start(self):
38 if self.args.category is None: 44 if self.args.category is None:
39 categories = self.host.bridge.getParamsCategories() 45 categories = await self.host.bridge.getParamsCategories()
40 print("\n".join(categories)) 46 print("\n".join(categories))
41 elif self.args.name is None: 47 elif self.args.name is None:
42 try: 48 try:
43 values_dict = self.host.bridge.asyncGetParamsValuesFromCategory(self.args.category, self.args.security_limit, self.profile) 49 values_dict = await self.host.bridge.asyncGetParamsValuesFromCategory(
50 self.args.category, self.args.security_limit, self.profile)
44 except Exception as e: 51 except Exception as e:
45 print("Can't find requested parameters: {}".format(e)) 52 self.disp(_(f"can't find requested parameters: {e}"), error=True)
46 self.host.quit(1) 53 self.host.quit(C.EXIT_NOT_FOUND)
47 for name, value in values_dict.items(): 54 else:
48 print("{}\t{}".format(name, value)) 55 for name, value in values_dict.items():
56 print(f"{name}\t{value}")
49 else: 57 else:
50 try: 58 try:
51 value = self.host.bridge.asyncGetParamA(self.args.name, self.args.category, self.args.attribute, 59 value = await self.host.bridge.asyncGetParamA(
52 self.args.security_limit, self.profile) 60 self.args.name, self.args.category, self.args.attribute,
61 self.args.security_limit, self.profile)
53 except Exception as e: 62 except Exception as e:
54 print("Can't find requested parameter: {}".format(e)) 63 self.disp(_(f"can't find requested parameter: {e}"), error=True)
55 self.host.quit(1) 64 self.host.quit(C.EXIT_NOT_FOUND)
56 print(value) 65 else:
66 print(value)
67 self.host.quit()
57 68
58 69
59 class Set(base.CommandBase): 70 class Set(base.CommandBase):
60 def __init__(self, host): 71 def __init__(self, host):
61 super(Set, self).__init__(host, 'set', need_connect=False, help=_('Set a parameter value')) 72 super(Set, self).__init__(host, 'set', need_connect=False, help=_('set a parameter value'))
62 73
63 def add_parser_options(self): 74 def add_parser_options(self):
64 self.parser.add_argument("category", help=_("Category of the parameter")) 75 self.parser.add_argument("category", help=_("category of the parameter"))
65 self.parser.add_argument("name", help=_("Name of the parameter")) 76 self.parser.add_argument("name", help=_("name of the parameter"))
66 self.parser.add_argument("value", help=_("Name of the parameter")) 77 self.parser.add_argument("value", help=_("name of the parameter"))
67 self.parser.add_argument("--security-limit", type=int, default=-1, help=_("Security limit")) 78 self.parser.add_argument("--security-limit", type=int, default=-1, help=_("security limit"))
68 79
69 def start(self): 80 async def start(self):
70 try: 81 try:
71 self.host.bridge.setParam(self.args.name, self.args.value, self.args.category, self.args.security_limit, self.profile) 82 await self.host.bridge.setParam(
83 self.args.name, self.args.value, self.args.category,
84 self.args.security_limit, self.profile)
72 except Exception as e: 85 except Exception as e:
73 print("Can set requested parameter: {}".format(e)) 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()
74 90
75 91
76 class SaveTemplate(base.CommandBase): 92 class SaveTemplate(base.CommandBase):
93 # FIXME: this should probably be removed, it's not used and not useful for end-user
94
77 def __init__(self, host): 95 def __init__(self, host):
78 super(SaveTemplate, self).__init__(host, 'save', use_profile=False, help=_('Save parameters template to xml file')) 96 super(SaveTemplate, self).__init__(
97 host, 'save', use_profile=False,
98 help=_('save parameters template to xml file'))
79 99
80 def add_parser_options(self): 100 def add_parser_options(self):
81 self.parser.add_argument("filename", type=str, help=_("Output file")) 101 self.parser.add_argument("filename", type=str, help=_("output file"))
82 102
83 def start(self): 103 async def start(self):
84 """Save parameters template to xml file""" 104 """Save parameters template to XML file"""
85 if self.host.bridge.saveParamsTemplate(self.args.filename): 105 try:
86 print(_("Parameters saved to file %s") % self.args.filename) 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)
87 else: 110 else:
88 print(_("Can't save parameters to file %s") % self.args.filename) 111 self.disp(_(f"parameters saved to file {self.args.filename}"))
112 self.host.quit()
89 113
90 114
91 class LoadTemplate(base.CommandBase): 115 class LoadTemplate(base.CommandBase):
116 # FIXME: this should probably be removed, it's not used and not useful for end-user
92 117
93 def __init__(self, host): 118 def __init__(self, host):
94 super(LoadTemplate, self).__init__(host, 'load', use_profile=False, help=_('Load parameters template from xml file')) 119 super(LoadTemplate, self).__init__(
120 host, 'load', use_profile=False,
121 help=_('load parameters template from xml file'))
95 122
96 def add_parser_options(self): 123 def add_parser_options(self):
97 self.parser.add_argument("filename", type=str, help=_("Input file")) 124 self.parser.add_argument("filename", type=str, help=_("input file"))
98 125
99 def start(self): 126 async def start(self):
100 """Load parameters template from xml file""" 127 """Load parameters template from xml file"""
101 if self.host.bridge.loadParamsTemplate(self.args.filename): 128 try:
102 print(_("Parameters loaded from file %s") % self.args.filename) 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)
103 else: 133 else:
104 print(_("Can't load parameters from file %s") % self.args.filename) 134 self.disp(_(f"parameters loaded from file {self.args.filename}"))
135 self.host.quit()
105 136
106 137
107 class Param(base.CommandBase): 138 class Param(base.CommandBase):
108 subcommands = (Get, Set, SaveTemplate, LoadTemplate) 139 subcommands = (Get, Set, SaveTemplate, LoadTemplate)
109 140