# HG changeset patch # User Goffi # Date 1522650202 -7200 # Node ID 501b0f827f63d73a56f927591fbf9cba26b40d23 # Parent f2a829bbdbb5cb96829468b843dd1bd18127c3d5 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. diff -r f2a829bbdbb5 -r 501b0f827f63 frontends/src/jp/cmd_merge_request.py --- a/frontends/src/jp/cmd_merge_request.py Sun Apr 01 20:55:43 2018 +0200 +++ b/frontends/src/jp/cmd_merge_request.py Mon Apr 02 08:23:22 2018 +0200 @@ -121,7 +121,7 @@ exit_code=C.EXIT_BRIDGE_ERRBACK)) def start(self): - common.URIFinder(self, os.getcwd(), 'merge requests', self.getRequests) + common.URIFinder(self, os.getcwd(), 'merge requests', self.getRequests, meta_map={}) class Import(base.CommandBase): @@ -155,7 +155,7 @@ def start(self): self.repository = os.path.expanduser(os.path.abspath(self.args.repository)) - common.URIFinder(self, self.repository, 'merge requests', self.importRequest) + common.URIFinder(self, self.repository, 'merge requests', self.importRequest, meta_map={}) class MergeRequest(base.CommandBase): diff -r f2a829bbdbb5 -r 501b0f827f63 frontends/src/jp/common.py --- a/frontends/src/jp/common.py Sun Apr 01 20:55:43 2018 +0200 +++ b/frontends/src/jp/common.py Mon Apr 02 08:23:22 2018 +0200 @@ -670,19 +670,26 @@ class URIFinder(object): """Helper class to find URIs in well-known locations""" - def __init__(self, command, path, key, callback): + def __init__(self, command, path, key, callback, meta_map=None): """ @param command(CommandBase): command instance args of this instance will be updated with found values @param path(unicode): absolute path to use as a starting point to look for URIs @param key(unicode): key to look for @param callback(callable): method to call once URIs are found (or not) + @param meta_map(dict, None): if not None, map metadata to arg name + key is metadata used attribute name + value is name to actually use, or None to ignore + use empty dict to only retrieve URI + possible keys are currently: + - labels """ if not command.args.service and not command.args.node: self.host = command.host self.args = command.args self.key = key self.callback = callback + self.meta_map = meta_map self.host.bridge.URIFind(path, [key], callback=self.URIFindCb, @@ -692,6 +699,33 @@ else: callback() + def setMetadataList(self, uri_data, key): + """Helper method to set list of values from metadata + + @param uri_data(dict): data of the found URI + @param key(unicode): key of the value to retrieve + """ + new_values_json = uri_data.get(key) + if uri_data is not None: + if self.meta_map is None: + dest = key + else: + dest = self.meta_map.get(key) + if dest is None: + return + + try: + values = getattr(self.args, key) + except AttributeError: + raise exceptions.InternalError(u'there is no "{key}" arguments'.format( + key=key)) + else: + if values is None: + values = [] + values.extend(json.loads(new_values_json)) + setattr(self.args, dest, values) + + def URIFindCb(self, uris_data): try: uri_data = uris_data[self.key] @@ -700,18 +734,8 @@ self.host.quit(C.EXIT_NOT_FOUND) else: uri = uri_data[u'uri'] - labels_raw = uri_data.get(u'labels') - if labels_raw is not None: - try: - labels = self.args.labels - except AttributeError: - raise exceptions.InternalError(u'there is no "labels" arguments') - else: - if labels is None: - labels = [] - labels.extend(json.loads(labels_raw)) - self.args.labels = labels + self.setMetadataList(uri_data, u'labels') parsed_uri = xmpp_uri.parseXMPPUri(uri) try: self.args.service = parsed_uri[u'path']