comparison 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
comparison
equal deleted inserted replaced
2553:39b10475f56b 2554:0062d3e79d12
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 from twisted.internet import defer 23 from twisted.internet import defer
24 import textwrap 24 import textwrap
25 log = getLogger(__name__) 25 log = getLogger(__name__)
26 import json
26 import os.path 27 import os.path
27 import os 28 import os
28 import re 29 import re
29 30
30 PLUGIN_INFO = { 31 PLUGIN_INFO = {
49 50
50 def __init__(self, host): 51 def __init__(self, host):
51 log.info(_(u"URI finder plugin initialization")) 52 log.info(_(u"URI finder plugin initialization"))
52 self.host = host 53 self.host = host
53 host.bridge.addMethod("URIFind", ".plugin", 54 host.bridge.addMethod("URIFind", ".plugin",
54 in_sign='sas', out_sign='a{ss}', 55 in_sign='sas', out_sign='a{sa{ss}}',
55 method=self.find, 56 method=self.find,
56 async=True) 57 async=True)
57 58
58 def find(self, path, keys): 59 def find(self, path, keys):
59 """Look for URI in well known locations 60 """Look for URI in well known locations
62 @param keys(list[unicode]): keys lookeds after 63 @param keys(list[unicode]): keys lookeds after
63 e.g.: "tickets", "merge-requests" 64 e.g.: "tickets", "merge-requests"
64 @return (dict[unicode, unicode]): map from key to found uri 65 @return (dict[unicode, unicode]): map from key to found uri
65 """ 66 """
66 keys_re = u'|'.join(keys) 67 keys_re = u'|'.join(keys)
67 uri_re = re.compile(ur'(?P<key>{keys_re})[ :]? +(?P<uri>xmpp:\S+)'.format(keys_re=keys_re)) 68 label_re = r'"(?P<label>[^"]+)"'
69 uri_re = re.compile(ur'(?P<key>{keys_re})[ :]? +(?P<uri>xmpp:\S+)(?:.*use {label_re} label)?'.format(
70 keys_re=keys_re, label_re = label_re))
68 path = os.path.normpath(path) 71 path = os.path.normpath(path)
69 if not os.path.isdir(path) or not os.path.isabs(path): 72 if not os.path.isdir(path) or not os.path.isabs(path):
70 raise ValueError(u'path must be an absolute path to a directory') 73 raise ValueError(u'path must be an absolute path to a directory')
71 74
72 found_uris = {} 75 found_uris = {}
77 file_path = os.path.join(path, filename) 80 file_path = os.path.join(path, filename)
78 with open(file_path) as f: 81 with open(file_path) as f:
79 for m in uri_re.finditer(f.read()): 82 for m in uri_re.finditer(f.read()):
80 key = m.group(u'key') 83 key = m.group(u'key')
81 uri = m.group(u'uri') 84 uri = m.group(u'uri')
85 label = m.group(u'label')
82 if key in found_uris: 86 if key in found_uris:
83 log.warning(_(u"Ignoring already found uri for key \"{key}\"").format(key=key)) 87 log.warning(_(u"Ignoring already found uri for key \"{key}\"").format(key=key))
84 else: 88 else:
85 found_uris[key] = uri 89 uri_data = found_uris[key] = {u'uri': uri}
90 if label is not None:
91 uri_data[u'labels'] = json.dumps([label])
86 if found_uris: 92 if found_uris:
87 break 93 break
88 path = os.path.dirname(path) 94 path = os.path.dirname(path)
89 95
90 return defer.succeed(found_uris) 96 return defer.succeed(found_uris)