Mercurial > libervia-backend
comparison frontends/src/jp/cmd_pubsub.py @ 2351:3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 06 Sep 2017 07:39:10 +0200 |
parents | d94e932be8b3 |
children | ebc0dfe9c0ca |
comparison
equal
deleted
inserted
replaced
2350:388226e9c3ff | 2351:3c0a3fae1862 |
---|---|
35 import sys | 35 import sys |
36 | 36 |
37 __commands__ = ["Pubsub"] | 37 __commands__ = ["Pubsub"] |
38 | 38 |
39 PUBSUB_TMP_DIR = u"pubsub" | 39 PUBSUB_TMP_DIR = u"pubsub" |
40 PUBSUB_SCHEMA_TMP_DIR = PUBSUB_TMP_DIR + "_schema" | |
40 ALLOWED_SUBSCRIPTIONS_OWNER = ('subscribed', 'pending', 'none') | 41 ALLOWED_SUBSCRIPTIONS_OWNER = ('subscribed', 'pending', 'none') |
41 | 42 |
42 # TODO: need to split this class in several modules, plugin should handle subcommands | 43 # TODO: need to split this class in several modules, plugin should handle subcommands |
43 | 44 |
44 | 45 |
360 | 361 |
361 def __init__(self, host): | 362 def __init__(self, host): |
362 super(NodeSubscriptions, self).__init__(host, 'subscriptions', use_profile=False, help=_(u'get or modify node subscriptions')) | 363 super(NodeSubscriptions, self).__init__(host, 'subscriptions', use_profile=False, help=_(u'get or modify node subscriptions')) |
363 | 364 |
364 | 365 |
366 class NodeSchemaSet(base.CommandBase): | |
367 | |
368 def __init__(self, host): | |
369 base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, use_verbose=True, help=_(u'set/replace a schema')) | |
370 self.need_loop = True | |
371 | |
372 def add_parser_options(self): | |
373 self.parser.add_argument('schema', help=_(u"schema to set (must be XML)")) | |
374 | |
375 def psSchemaSetCb(self): | |
376 self.disp(_(u'schema has been set'), 1) | |
377 self.host.quit() | |
378 | |
379 def start(self): | |
380 self.host.bridge.psSchemaSet( | |
381 self.args.service, | |
382 self.args.node, | |
383 self.args.schema, | |
384 self.profile, | |
385 callback=self.psSchemaSetCb, | |
386 errback=partial(self.errback, | |
387 msg=_(u"can't set schema: {}"), | |
388 exit_code=C.EXIT_BRIDGE_ERRBACK)) | |
389 | |
390 | |
391 class NodeSchemaEdit(base.CommandBase, common.BaseEdit): | |
392 use_items=False | |
393 | |
394 def __init__(self, host): | |
395 base.CommandBase.__init__(self, host, 'edit', use_pubsub_node_req=True, use_verbose=True, help=_(u'edit a schema')) | |
396 common.BaseEdit.__init__(self, self.host, PUBSUB_SCHEMA_TMP_DIR) | |
397 self.need_loop=True | |
398 | |
399 def add_parser_options(self): | |
400 common.BaseEdit.add_parser_options(self) | |
401 | |
402 def psSchemaSetCb(self): | |
403 self.disp(_(u'schema has been set'), 1) | |
404 self.host.quit() | |
405 | |
406 def publish(self, schema): | |
407 self.host.bridge.psSchemaSet( | |
408 self.args.service, | |
409 self.args.node, | |
410 schema, | |
411 self.profile, | |
412 callback=self.psSchemaSetCb, | |
413 errback=partial(self.errback, | |
414 msg=_(u"can't set schema: {}"), | |
415 exit_code=C.EXIT_BRIDGE_ERRBACK)) | |
416 | |
417 def psSchemaGetCb(self, schema): | |
418 try: | |
419 from lxml import etree | |
420 except ImportError: | |
421 self.disp(u"lxml module must be installed to use edit, please install it with \"pip install lxml\"", error=True) | |
422 self.host.quit(1) | |
423 parser = etree.XMLParser(remove_blank_text=True) | |
424 schema_elt = etree.fromstring(schema, parser) | |
425 content_file_obj, content_file_path = self.getTmpFile() | |
426 content_file_obj.write(etree.tostring(schema_elt, encoding="utf-8", pretty_print=True)) | |
427 content_file_obj.seek(0) | |
428 self.runEditor("pubsub_schema_editor_args", content_file_path, content_file_obj) | |
429 | |
430 def start(self): | |
431 common.checkURI(self.args) | |
432 self.host.bridge.psSchemaGet( | |
433 self.args.service, | |
434 self.args.node, | |
435 self.profile, | |
436 callback=self.psSchemaGetCb, | |
437 errback=partial(self.errback, | |
438 msg=_(u"can't edit schema: {}"), | |
439 exit_code=C.EXIT_BRIDGE_ERRBACK)) | |
440 | |
441 | |
442 class NodeSchemaGet(base.CommandBase): | |
443 | |
444 def __init__(self, host): | |
445 base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_XML, use_pubsub_node_req=True, use_verbose=True, help=_(u'get schema')) | |
446 self.need_loop=True | |
447 | |
448 def add_parser_options(self): | |
449 pass | |
450 | |
451 def psSchemaGetCb(self, schema): | |
452 if not schema: | |
453 self.disp(_(u'no schema found'), 1) | |
454 self.host.quit(1) | |
455 self.output(schema) | |
456 self.host.quit() | |
457 | |
458 def start(self): | |
459 common.checkURI(self.args) | |
460 self.host.bridge.psSchemaGet( | |
461 self.args.service, | |
462 self.args.node, | |
463 self.profile, | |
464 callback=self.psSchemaGetCb, | |
465 errback=partial(self.errback, | |
466 msg=_(u"can't get schema: {}"), | |
467 exit_code=C.EXIT_BRIDGE_ERRBACK)) | |
468 | |
469 | |
470 class NodeSchema(base.CommandBase): | |
471 subcommands = (NodeSchemaSet, NodeSchemaEdit, NodeSchemaGet) | |
472 | |
473 def __init__(self, host): | |
474 super(NodeSchema, self).__init__(host, 'schema', use_profile=False, help=_(u"data schema manipulation")) | |
475 | |
476 | |
365 class Node(base.CommandBase): | 477 class Node(base.CommandBase): |
366 subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations, NodeSubscriptions) | 478 subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations, NodeSubscriptions, NodeSchema) |
367 | 479 |
368 def __init__(self, host): | 480 def __init__(self, host): |
369 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling')) | 481 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling')) |
370 | 482 |
371 | 483 |
445 | 557 |
446 | 558 |
447 class Edit(base.CommandBase, common.BaseEdit): | 559 class Edit(base.CommandBase, common.BaseEdit): |
448 | 560 |
449 def __init__(self, host): | 561 def __init__(self, host): |
450 base.CommandBase.__init__(self, host, 'edit', use_verbose=True, use_pubsub=True, help=_(u'edit an existing or new pubsub item')) | 562 base.CommandBase.__init__(self, host, 'edit', use_verbose=True, use_pubsub_node_req=True, help=_(u'edit an existing or new pubsub item')) |
451 common.BaseEdit.__init__(self, self.host, PUBSUB_TMP_DIR) | 563 common.BaseEdit.__init__(self, self.host, PUBSUB_TMP_DIR) |
452 | 564 |
453 def add_parser_options(self): | 565 def add_parser_options(self): |
454 self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword")) | 566 self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword")) |
455 common.BaseEdit.add_parser_options(self) | 567 common.BaseEdit.add_parser_options(self) |
978 | 1090 |
979 class Hook(base.CommandBase): | 1091 class Hook(base.CommandBase): |
980 subcommands = (HookCreate, HookDelete, HookList) | 1092 subcommands = (HookCreate, HookDelete, HookList) |
981 | 1093 |
982 def __init__(self, host): | 1094 def __init__(self, host): |
983 super(Hook, self).__init__(host, 'hook', use_profile=False, help=_('trigger action on Pubsub notifications')) | 1095 super(Hook, self).__init__(host, 'hook', use_profile=False, use_verbose=True, help=_('trigger action on Pubsub notifications')) |
984 | 1096 |
985 | 1097 |
986 class Pubsub(base.CommandBase): | 1098 class Pubsub(base.CommandBase): |
987 subcommands = (Get, Delete, Edit, Node, Affiliations, Search, Hook, Uri) | 1099 subcommands = (Get, Delete, Edit, Node, Affiliations, Search, Hook, Uri) |
988 | 1100 |