comparison sat/plugins/plugin_misc_uri_finder.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_misc_uri_finder.py@0062d3e79d12
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
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 json
27 import os.path
28 import os
29 import re
30
31 PLUGIN_INFO = {
32 C.PI_NAME: _("URI finder"),
33 C.PI_IMPORT_NAME: "uri_finder",
34 C.PI_TYPE: "EXP",
35 C.PI_PROTOCOLS: [],
36 C.PI_DEPENDENCIES: [],
37 C.PI_MAIN: "URIFinder",
38 C.PI_HANDLER: "no",
39 C.PI_DESCRIPTION: textwrap.dedent(_(u"""\
40 Plugin to find URIs in well know location.
41 This allows to retrieve settings to work with a project (e.g. pubsub node used for merge-requests).
42 """))
43 }
44
45
46 SEARCH_FILES = ('readme', 'contributing')
47
48
49 class URIFinder(object):
50
51 def __init__(self, host):
52 log.info(_(u"URI finder plugin initialization"))
53 self.host = host
54 host.bridge.addMethod("URIFind", ".plugin",
55 in_sign='sas', out_sign='a{sa{ss}}',
56 method=self.find,
57 async=True)
58
59 def find(self, path, keys):
60 """Look for URI in well known locations
61
62 @param path(unicode): path to start with
63 @param keys(list[unicode]): keys lookeds after
64 e.g.: "tickets", "merge-requests"
65 @return (dict[unicode, unicode]): map from key to found uri
66 """
67 keys_re = u'|'.join(keys)
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))
71 path = os.path.normpath(path)
72 if not os.path.isdir(path) or not os.path.isabs(path):
73 raise ValueError(u'path must be an absolute path to a directory')
74
75 found_uris = {}
76 while path != u'/':
77 for filename in os.listdir(path):
78 name, __ = os.path.splitext(filename)
79 if name.lower() in SEARCH_FILES:
80 file_path = os.path.join(path, filename)
81 with open(file_path) as f:
82 for m in uri_re.finditer(f.read()):
83 key = m.group(u'key')
84 uri = m.group(u'uri')
85 label = m.group(u'label')
86 if key in found_uris:
87 log.warning(_(u"Ignoring already found uri for key \"{key}\"").format(key=key))
88 else:
89 uri_data = found_uris[key] = {u'uri': uri}
90 if label is not None:
91 uri_data[u'labels'] = json.dumps([label])
92 if found_uris:
93 break
94 path = os.path.dirname(path)
95
96 return defer.succeed(found_uris)