comparison frontends/src/jp/common.py @ 2558:501b0f827f63

jp (merge-request,common): fixed URIFinder when metadata are not needed: new "meta_map" attribute which allows to select destination argument name, or to discard unused metadata.
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 08:23:22 +0200
parents 0062d3e79d12
children
comparison
equal deleted inserted replaced
2557:f2a829bbdbb5 2558:501b0f827f63
668 668
669 669
670 class URIFinder(object): 670 class URIFinder(object):
671 """Helper class to find URIs in well-known locations""" 671 """Helper class to find URIs in well-known locations"""
672 672
673 def __init__(self, command, path, key, callback): 673 def __init__(self, command, path, key, callback, meta_map=None):
674 """ 674 """
675 @param command(CommandBase): command instance 675 @param command(CommandBase): command instance
676 args of this instance will be updated with found values 676 args of this instance will be updated with found values
677 @param path(unicode): absolute path to use as a starting point to look for URIs 677 @param path(unicode): absolute path to use as a starting point to look for URIs
678 @param key(unicode): key to look for 678 @param key(unicode): key to look for
679 @param callback(callable): method to call once URIs are found (or not) 679 @param callback(callable): method to call once URIs are found (or not)
680 @param meta_map(dict, None): if not None, map metadata to arg name
681 key is metadata used attribute name
682 value is name to actually use, or None to ignore
683 use empty dict to only retrieve URI
684 possible keys are currently:
685 - labels
680 """ 686 """
681 if not command.args.service and not command.args.node: 687 if not command.args.service and not command.args.node:
682 self.host = command.host 688 self.host = command.host
683 self.args = command.args 689 self.args = command.args
684 self.key = key 690 self.key = key
685 self.callback = callback 691 self.callback = callback
692 self.meta_map = meta_map
686 self.host.bridge.URIFind(path, 693 self.host.bridge.URIFind(path,
687 [key], 694 [key],
688 callback=self.URIFindCb, 695 callback=self.URIFindCb,
689 errback=partial(command.errback, 696 errback=partial(command.errback,
690 msg=_(u"can't find " + key + u" URI: {}"), 697 msg=_(u"can't find " + key + u" URI: {}"),
691 exit_code=C.EXIT_BRIDGE_ERRBACK)) 698 exit_code=C.EXIT_BRIDGE_ERRBACK))
692 else: 699 else:
693 callback() 700 callback()
694 701
702 def setMetadataList(self, uri_data, key):
703 """Helper method to set list of values from metadata
704
705 @param uri_data(dict): data of the found URI
706 @param key(unicode): key of the value to retrieve
707 """
708 new_values_json = uri_data.get(key)
709 if uri_data is not None:
710 if self.meta_map is None:
711 dest = key
712 else:
713 dest = self.meta_map.get(key)
714 if dest is None:
715 return
716
717 try:
718 values = getattr(self.args, key)
719 except AttributeError:
720 raise exceptions.InternalError(u'there is no "{key}" arguments'.format(
721 key=key))
722 else:
723 if values is None:
724 values = []
725 values.extend(json.loads(new_values_json))
726 setattr(self.args, dest, values)
727
728
695 def URIFindCb(self, uris_data): 729 def URIFindCb(self, uris_data):
696 try: 730 try:
697 uri_data = uris_data[self.key] 731 uri_data = uris_data[self.key]
698 except KeyError: 732 except KeyError:
699 self.host.disp(_(u"No {key} URI specified for this project, please specify service and node").format(key=self.key), error=True) 733 self.host.disp(_(u"No {key} URI specified for this project, please specify service and node").format(key=self.key), error=True)
700 self.host.quit(C.EXIT_NOT_FOUND) 734 self.host.quit(C.EXIT_NOT_FOUND)
701 else: 735 else:
702 uri = uri_data[u'uri'] 736 uri = uri_data[u'uri']
703 labels_raw = uri_data.get(u'labels') 737
704 738 self.setMetadataList(uri_data, u'labels')
705 if labels_raw is not None:
706 try:
707 labels = self.args.labels
708 except AttributeError:
709 raise exceptions.InternalError(u'there is no "labels" arguments')
710 else:
711 if labels is None:
712 labels = []
713 labels.extend(json.loads(labels_raw))
714 self.args.labels = labels
715 parsed_uri = xmpp_uri.parseXMPPUri(uri) 739 parsed_uri = xmpp_uri.parseXMPPUri(uri)
716 try: 740 try:
717 self.args.service = parsed_uri[u'path'] 741 self.args.service = parsed_uri[u'path']
718 self.args.node = parsed_uri[u'node'] 742 self.args.node = parsed_uri[u'node']
719 except KeyError: 743 except KeyError: