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

@ -23,7 +23,6 @@ from sat.core.i18n import _
from sat_frontends.jp.constants import Const as C
from sat_frontends.jp import common
from sat.tools.common.ansi import ANSI as A
from functools import partial
import codecs
import json
@ -46,7 +45,6 @@ class Edit(base.CommandBase, common.BaseEdit):
help=_("edit forums"),
)
common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR)
self.need_loop = True
def add_parser_options(self):
self.parser.add_argument(
@ -60,26 +58,37 @@ class Edit(base.CommandBase, common.BaseEdit):
"""return suffix used for content file"""
return "json"
def forumsSetCb(self):
self.disp(_("forums have been edited"), 1)
self.host.quit()
async def publish(self, forums_raw):
try:
await self.host.bridge.forumsSet(
forums_raw,
self.args.service,
self.args.node,
self.args.key,
self.profile,
)
except Exception as e:
self.disp(f"can't set forums: {e}", error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
self.disp(_("forums have been edited"), 1)
self.host.quit()
def publish(self, forums_raw):
self.host.bridge.forumsSet(
forums_raw,
self.args.service,
self.args.node,
self.args.key,
self.profile,
callback=self.forumsSetCb,
errback=partial(
self.errback,
msg=_("can't set forums: {}"),
exit_code=C.EXIT_BRIDGE_ERRBACK,
),
)
async def start(self):
try:
forums_json = await self.host.bridge.forumsGet(
self.args.service,
self.args.node,
self.args.key,
self.profile,
)
except Exception as e:
if e.classname == "NotFound":
forums_json = ""
else:
self.disp(f"can't get node configuration: {e}", error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
def forumsGetCb(self, forums_json):
content_file_obj, content_file_path = self.getTmpFile()
forums_json = forums_json.strip()
if forums_json:
@ -89,28 +98,7 @@ class Edit(base.CommandBase, common.BaseEdit):
f = codecs.getwriter("utf-8")(content_file_obj)
json.dump(forums, f, ensure_ascii=False, indent=4)
content_file_obj.seek(0)
self.runEditor("forums_editor_args", content_file_path, content_file_obj)
def forumsGetEb(self, failure_):
# FIXME: error handling with bridge is broken, need to be properly fixed
if failure_.condition == "item-not-found":
self.forumsGetCb("")
else:
self.errback(
failure_,
msg=_("can't get forums structure: {}"),
exit_code=C.EXIT_BRIDGE_ERRBACK,
)
def start(self):
self.host.bridge.forumsGet(
self.args.service,
self.args.node,
self.args.key,
self.profile,
callback=self.forumsGetCb,
errback=self.forumsGetEb,
)
await self.runEditor("forums_editor_args", content_file_path, content_file_obj)
class Get(base.CommandBase):
@ -126,7 +114,6 @@ class Get(base.CommandBase):
use_verbose=True,
help=_("get forums structure"),
)
self.need_loop = True
def add_parser_options(self):
self.parser.add_argument(
@ -165,27 +152,24 @@ class Get(base.CommandBase):
A.color(level * 4 * " ", head_color, key, A.RESET, ": ", value)
)
def forumsGetCb(self, forums_raw):
if not forums_raw:
self.disp(_("no schema found"), 1)
self.host.quit(1)
forums = json.loads(forums_raw)
self.output(forums)
self.host.quit()
def start(self):
self.host.bridge.forumsGet(
self.args.service,
self.args.node,
self.args.key,
self.profile,
callback=self.forumsGetCb,
errback=partial(
self.errback,
msg=_("can't get forums: {}"),
exit_code=C.EXIT_BRIDGE_ERRBACK,
),
)
async def start(self):
try:
forums_raw = await self.host.bridge.forumsGet(
self.args.service,
self.args.node,
self.args.key,
self.profile,
)
except Exception as e:
self.disp(f"can't get forums: {e}", error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
if not forums_raw:
self.disp(_("no schema found"), 1)
self.host.quit(1)
forums = json.loads(forums_raw)
await self.output(forums)
self.host.quit()
class Forums(base.CommandBase):