Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_misc_uri_finder.py @ 4071:4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 11:49:51 +0200 |
parents | sat/plugins/plugin_misc_uri_finder.py@524856bd7b19 |
children | 0d7bb4df2343 |
comparison
equal
deleted
inserted
replaced
4070:d10748475025 | 4071:4b842c1fb686 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # SAT plugin to find URIs | |
5 # Copyright (C) 2009-2021 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 libervia.backend.core.i18n import _ | |
21 from libervia.backend.core.constants import Const as C | |
22 from libervia.backend.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(_("""\ | |
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(_("URI finder plugin initialization")) | |
53 self.host = host | |
54 host.bridge.add_method("uri_find", ".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 = '|'.join(keys) | |
68 label_re = r'"(?P<label>[^"]+)"' | |
69 uri_re = re.compile(r'(?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('path must be an absolute path to a directory') | |
74 | |
75 found_uris = {} | |
76 while path != '/': | |
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('key') | |
84 uri = m.group('uri') | |
85 label = m.group('label') | |
86 if key in found_uris: | |
87 log.warning(_("Ignoring already found uri for key \"{key}\"").format(key=key)) | |
88 else: | |
89 uri_data = found_uris[key] = {'uri': uri} | |
90 if label is not None: | |
91 uri_data['labels'] = json.dumps([label]) | |
92 if found_uris: | |
93 break | |
94 path = os.path.dirname(path) | |
95 | |
96 return defer.succeed(found_uris) |