diff 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
line wrap: on
line diff
--- 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']