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.
This commit is contained in:
Goffi 2019-09-25 08:56:41 +02:00
commit 76aadbb3fb

View file

@ -21,87 +21,118 @@
from . import base
from sat.core.i18n import _
from .constants import Const as C
__commands__ = ["Param"]
class Get(base.CommandBase):
def __init__(self, host):
super(Get, self).__init__(host, 'get', need_connect=False, help=_('Get a parameter value'))
super(Get, self).__init__(
host, 'get', need_connect=False, help=_('get a parameter value'))
def add_parser_options(self):
self.parser.add_argument("category", nargs='?', help=_("Category of the parameter"))
self.parser.add_argument("name", nargs='?', help=_("Name of the parameter"))
self.parser.add_argument("-a", "--attribute", type=str, default="value", help=_("Name of the attribute to get"))
self.parser.add_argument("--security-limit", type=int, default=-1, help=_("Security limit"))
self.parser.add_argument(
"category", nargs='?', help=_("category of the parameter"))
self.parser.add_argument("name", nargs='?', help=_("name of the parameter"))
self.parser.add_argument(
"-a", "--attribute", type=str, default="value",
help=_("name of the attribute to get"))
self.parser.add_argument(
"--security-limit", type=int, default=-1, help=_("security limit"))
def start(self):
async def start(self):
if self.args.category is None:
categories = self.host.bridge.getParamsCategories()
categories = await self.host.bridge.getParamsCategories()
print("\n".join(categories))
elif self.args.name is None:
try:
values_dict = self.host.bridge.asyncGetParamsValuesFromCategory(self.args.category, self.args.security_limit, self.profile)
values_dict = await self.host.bridge.asyncGetParamsValuesFromCategory(
self.args.category, self.args.security_limit, self.profile)
except Exception as e:
print("Can't find requested parameters: {}".format(e))
self.host.quit(1)
for name, value in values_dict.items():
print("{}\t{}".format(name, value))
self.disp(_(f"can't find requested parameters: {e}"), error=True)
self.host.quit(C.EXIT_NOT_FOUND)
else:
for name, value in values_dict.items():
print(f"{name}\t{value}")
else:
try:
value = self.host.bridge.asyncGetParamA(self.args.name, self.args.category, self.args.attribute,
self.args.security_limit, self.profile)
value = await self.host.bridge.asyncGetParamA(
self.args.name, self.args.category, self.args.attribute,
self.args.security_limit, self.profile)
except Exception as e:
print("Can't find requested parameter: {}".format(e))
self.host.quit(1)
print(value)
self.disp(_(f"can't find requested parameter: {e}"), error=True)
self.host.quit(C.EXIT_NOT_FOUND)
else:
print(value)
self.host.quit()
class Set(base.CommandBase):
def __init__(self, host):
super(Set, self).__init__(host, 'set', need_connect=False, help=_('Set a parameter value'))
super(Set, self).__init__(host, 'set', need_connect=False, help=_('set a parameter value'))
def add_parser_options(self):
self.parser.add_argument("category", help=_("Category of the parameter"))
self.parser.add_argument("name", help=_("Name of the parameter"))
self.parser.add_argument("value", help=_("Name of the parameter"))
self.parser.add_argument("--security-limit", type=int, default=-1, help=_("Security limit"))
self.parser.add_argument("category", help=_("category of the parameter"))
self.parser.add_argument("name", help=_("name of the parameter"))
self.parser.add_argument("value", help=_("name of the parameter"))
self.parser.add_argument("--security-limit", type=int, default=-1, help=_("security limit"))
def start(self):
async def start(self):
try:
self.host.bridge.setParam(self.args.name, self.args.value, self.args.category, self.args.security_limit, self.profile)
await self.host.bridge.setParam(
self.args.name, self.args.value, self.args.category,
self.args.security_limit, self.profile)
except Exception as e:
print("Can set requested parameter: {}".format(e))
self.disp(_(f"can't set requested parameter: {e}"), error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
self.host.quit()
class SaveTemplate(base.CommandBase):
# FIXME: this should probably be removed, it's not used and not useful for end-user
def __init__(self, host):
super(SaveTemplate, self).__init__(host, 'save', use_profile=False, help=_('Save parameters template to xml file'))
super(SaveTemplate, self).__init__(
host, 'save', use_profile=False,
help=_('save parameters template to xml file'))
def add_parser_options(self):
self.parser.add_argument("filename", type=str, help=_("Output file"))
self.parser.add_argument("filename", type=str, help=_("output file"))
def start(self):
"""Save parameters template to xml file"""
if self.host.bridge.saveParamsTemplate(self.args.filename):
print(_("Parameters saved to file %s") % self.args.filename)
async def start(self):
"""Save parameters template to XML file"""
try:
await self.host.bridge.saveParamsTemplate(self.args.filename)
except Exception as e:
self.disp(_(f"can't save parameters to file: {e}"), error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
print(_("Can't save parameters to file %s") % self.args.filename)
self.disp(_(f"parameters saved to file {self.args.filename}"))
self.host.quit()
class LoadTemplate(base.CommandBase):
# FIXME: this should probably be removed, it's not used and not useful for end-user
def __init__(self, host):
super(LoadTemplate, self).__init__(host, 'load', use_profile=False, help=_('Load parameters template from xml file'))
super(LoadTemplate, self).__init__(
host, 'load', use_profile=False,
help=_('load parameters template from xml file'))
def add_parser_options(self):
self.parser.add_argument("filename", type=str, help=_("Input file"))
self.parser.add_argument("filename", type=str, help=_("input file"))
def start(self):
async def start(self):
"""Load parameters template from xml file"""
if self.host.bridge.loadParamsTemplate(self.args.filename):
print(_("Parameters loaded from file %s") % self.args.filename)
try:
self.host.bridge.loadParamsTemplate(self.args.filename)
except Exception as e:
self.disp(_(f"can't load parameters from file: {e}"), error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
print(_("Can't load parameters from file %s") % self.args.filename)
self.disp(_(f"parameters loaded from file {self.args.filename}"))
self.host.quit()
class Param(base.CommandBase):