comparison libervia/backend/plugins/plugin_misc_uri_finder.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
20 from libervia.backend.core.i18n import _ 20 from libervia.backend.core.i18n import _
21 from libervia.backend.core.constants import Const as C 21 from libervia.backend.core.constants import Const as C
22 from libervia.backend.core.log import getLogger 22 from libervia.backend.core.log import getLogger
23 from twisted.internet import defer 23 from twisted.internet import defer
24 import textwrap 24 import textwrap
25
25 log = getLogger(__name__) 26 log = getLogger(__name__)
26 import json 27 import json
27 import os.path 28 import os.path
28 import os 29 import os
29 import re 30 import re
34 C.PI_TYPE: "EXP", 35 C.PI_TYPE: "EXP",
35 C.PI_PROTOCOLS: [], 36 C.PI_PROTOCOLS: [],
36 C.PI_DEPENDENCIES: [], 37 C.PI_DEPENDENCIES: [],
37 C.PI_MAIN: "URIFinder", 38 C.PI_MAIN: "URIFinder",
38 C.PI_HANDLER: "no", 39 C.PI_HANDLER: "no",
39 C.PI_DESCRIPTION: textwrap.dedent(_("""\ 40 C.PI_DESCRIPTION: textwrap.dedent(
41 _(
42 """\
40 Plugin to find URIs in well know location. 43 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). 44 This allows to retrieve settings to work with a project (e.g. pubsub node used for merge-requests).
42 """)) 45 """
46 )
47 ),
43 } 48 }
44 49
45 50
46 SEARCH_FILES = ('readme', 'contributing') 51 SEARCH_FILES = ("readme", "contributing")
47 52
48 53
49 class URIFinder(object): 54 class URIFinder(object):
50 55
51 def __init__(self, host): 56 def __init__(self, host):
52 log.info(_("URI finder plugin initialization")) 57 log.info(_("URI finder plugin initialization"))
53 self.host = host 58 self.host = host
54 host.bridge.add_method("uri_find", ".plugin", 59 host.bridge.add_method(
55 in_sign='sas', out_sign='a{sa{ss}}', 60 "uri_find",
56 method=self.find, 61 ".plugin",
57 async_=True) 62 in_sign="sas",
63 out_sign="a{sa{ss}}",
64 method=self.find,
65 async_=True,
66 )
58 67
59 def find(self, path, keys): 68 def find(self, path, keys):
60 """Look for URI in well known locations 69 """Look for URI in well known locations
61 70
62 @param path(unicode): path to start with 71 @param path(unicode): path to start with
63 @param keys(list[unicode]): keys lookeds after 72 @param keys(list[unicode]): keys lookeds after
64 e.g.: "tickets", "merge-requests" 73 e.g.: "tickets", "merge-requests"
65 @return (dict[unicode, unicode]): map from key to found uri 74 @return (dict[unicode, unicode]): map from key to found uri
66 """ 75 """
67 keys_re = '|'.join(keys) 76 keys_re = "|".join(keys)
68 label_re = r'"(?P<label>[^"]+)"' 77 label_re = r'"(?P<label>[^"]+)"'
69 uri_re = re.compile(r'(?P<key>{keys_re})[ :]? +(?P<uri>xmpp:\S+)(?:.*use {label_re} label)?'.format( 78 uri_re = re.compile(
70 keys_re=keys_re, label_re = label_re)) 79 r"(?P<key>{keys_re})[ :]? +(?P<uri>xmpp:\S+)(?:.*use {label_re} label)?".format(
80 keys_re=keys_re, label_re=label_re
81 )
82 )
71 path = os.path.normpath(path) 83 path = os.path.normpath(path)
72 if not os.path.isdir(path) or not os.path.isabs(path): 84 if not os.path.isdir(path) or not os.path.isabs(path):
73 raise ValueError('path must be an absolute path to a directory') 85 raise ValueError("path must be an absolute path to a directory")
74 86
75 found_uris = {} 87 found_uris = {}
76 while path != '/': 88 while path != "/":
77 for filename in os.listdir(path): 89 for filename in os.listdir(path):
78 name, __ = os.path.splitext(filename) 90 name, __ = os.path.splitext(filename)
79 if name.lower() in SEARCH_FILES: 91 if name.lower() in SEARCH_FILES:
80 file_path = os.path.join(path, filename) 92 file_path = os.path.join(path, filename)
81 with open(file_path) as f: 93 with open(file_path) as f:
82 for m in uri_re.finditer(f.read()): 94 for m in uri_re.finditer(f.read()):
83 key = m.group('key') 95 key = m.group("key")
84 uri = m.group('uri') 96 uri = m.group("uri")
85 label = m.group('label') 97 label = m.group("label")
86 if key in found_uris: 98 if key in found_uris:
87 log.warning(_("Ignoring already found uri for key \"{key}\"").format(key=key)) 99 log.warning(
100 _(
101 'Ignoring already found uri for key "{key}"'
102 ).format(key=key)
103 )
88 else: 104 else:
89 uri_data = found_uris[key] = {'uri': uri} 105 uri_data = found_uris[key] = {"uri": uri}
90 if label is not None: 106 if label is not None:
91 uri_data['labels'] = json.dumps([label]) 107 uri_data["labels"] = json.dumps([label])
92 if found_uris: 108 if found_uris:
93 break 109 break
94 path = os.path.dirname(path) 110 path = os.path.dirname(path)
95 111
96 return defer.succeed(found_uris) 112 return defer.succeed(found_uris)