comparison src/plugins/plugin_misc_uri_finder.py @ 2548:54b3853b55c0

plugin uri finder: plugin to find URIs in well-known locations
author Goffi <goffi@goffi.org>
date Sat, 31 Mar 2018 17:20:14 +0200
parents
children 0062d3e79d12
comparison
equal deleted inserted replaced
2547:2d69a0afe039 2548:54b3853b55c0
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin to find URIs
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger
23 from twisted.internet import defer
24 import textwrap
25 log = getLogger(__name__)
26 import os.path
27 import os
28 import re
29
30 PLUGIN_INFO = {
31 C.PI_NAME: _("URI finder"),
32 C.PI_IMPORT_NAME: "uri_finder",
33 C.PI_TYPE: "EXP",
34 C.PI_PROTOCOLS: [],
35 C.PI_DEPENDENCIES: [],
36 C.PI_MAIN: "URIFinder",
37 C.PI_HANDLER: "no",
38 C.PI_DESCRIPTION: textwrap.dedent(_(u"""\
39 Plugin to find URIs in well know location.
40 This allows to retrieve settings to work with a project (e.g. pubsub node used for merge-requests).
41 """))
42 }
43
44
45 SEARCH_FILES = ('readme', 'contributing')
46
47
48 class URIFinder(object):
49
50 def __init__(self, host):
51 log.info(_(u"URI finder plugin initialization"))
52 self.host = host
53 host.bridge.addMethod("URIFind", ".plugin",
54 in_sign='sas', out_sign='a{ss}',
55 method=self.find,
56 async=True)
57
58 def find(self, path, keys):
59 """Look for URI in well known locations
60
61 @param path(unicode): path to start with
62 @param keys(list[unicode]): keys lookeds after
63 e.g.: "tickets", "merge-requests"
64 @return (dict[unicode, unicode]): map from key to found uri
65 """
66 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 path = os.path.normpath(path)
69 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')
71
72 found_uris = {}
73 while path != u'/':
74 for filename in os.listdir(path):
75 name, __ = os.path.splitext(filename)
76 if name.lower() in SEARCH_FILES:
77 file_path = os.path.join(path, filename)
78 with open(file_path) as f:
79 for m in uri_re.finditer(f.read()):
80 key = m.group(u'key')
81 uri = m.group(u'uri')
82 if key in found_uris:
83 log.warning(_(u"Ignoring already found uri for key \"{key}\"").format(key=key))
84 else:
85 found_uris[key] = uri
86 if found_uris:
87 break
88 path = os.path.dirname(path)
89
90 return defer.succeed(found_uris)