annotate src/plugins/plugin_misc_uri_finder.py @ 2550:1d754bc14381

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