diff src/plugins/plugin_misc_uri_finder.py @ 2554:0062d3e79d12

plugin uri finder, jp (merge-request): labels handling: - a label can now be specified as metadata after specifing a "xmpp:" URI in doc (with « use "[label]" label" » after the URI) - updated jp to handle new signature of URIFind - jp (merge-request/set): labels can now be specified using --label
author Goffi <goffi@goffi.org>
date Sun, 01 Apr 2018 20:17:00 +0200
parents 54b3853b55c0
children
line wrap: on
line diff
--- a/src/plugins/plugin_misc_uri_finder.py	Sat Mar 31 18:42:01 2018 +0200
+++ b/src/plugins/plugin_misc_uri_finder.py	Sun Apr 01 20:17:00 2018 +0200
@@ -23,6 +23,7 @@
 from twisted.internet import defer
 import textwrap
 log = getLogger(__name__)
+import json
 import os.path
 import os
 import re
@@ -51,7 +52,7 @@
         log.info(_(u"URI finder plugin initialization"))
         self.host = host
         host.bridge.addMethod("URIFind", ".plugin",
-                              in_sign='sas', out_sign='a{ss}',
+                              in_sign='sas', out_sign='a{sa{ss}}',
                               method=self.find,
                               async=True)
 
@@ -64,7 +65,9 @@
         @return (dict[unicode, unicode]): map from key to found uri
         """
         keys_re = u'|'.join(keys)
-        uri_re = re.compile(ur'(?P<key>{keys_re})[ :]? +(?P<uri>xmpp:\S+)'.format(keys_re=keys_re))
+        label_re = r'"(?P<label>[^"]+)"'
+        uri_re = re.compile(ur'(?P<key>{keys_re})[ :]? +(?P<uri>xmpp:\S+)(?:.*use {label_re} label)?'.format(
+            keys_re=keys_re, label_re = label_re))
         path = os.path.normpath(path)
         if not os.path.isdir(path) or not os.path.isabs(path):
             raise ValueError(u'path must be an absolute path to a directory')
@@ -79,10 +82,13 @@
                         for m in uri_re.finditer(f.read()):
                             key = m.group(u'key')
                             uri = m.group(u'uri')
+                            label = m.group(u'label')
                             if key in found_uris:
                                 log.warning(_(u"Ignoring already found uri for key \"{key}\"").format(key=key))
                             else:
-                                found_uris[key] = uri
+                                uri_data = found_uris[key] = {u'uri': uri}
+                                if label is not None:
+                                    uri_data[u'labels'] = json.dumps([label])
             if found_uris:
                 break
             path = os.path.dirname(path)