Mercurial > libervia-backend
comparison sat/plugins/plugin_misc_uri_finder.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 003b8b4b56a7 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 # SAT plugin to find URIs | 4 # SAT plugin to find URIs |
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
6 | 6 |
34 C.PI_TYPE: "EXP", | 34 C.PI_TYPE: "EXP", |
35 C.PI_PROTOCOLS: [], | 35 C.PI_PROTOCOLS: [], |
36 C.PI_DEPENDENCIES: [], | 36 C.PI_DEPENDENCIES: [], |
37 C.PI_MAIN: "URIFinder", | 37 C.PI_MAIN: "URIFinder", |
38 C.PI_HANDLER: "no", | 38 C.PI_HANDLER: "no", |
39 C.PI_DESCRIPTION: textwrap.dedent(_(u"""\ | 39 C.PI_DESCRIPTION: textwrap.dedent(_("""\ |
40 Plugin to find URIs in well know location. | 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). | 41 This allows to retrieve settings to work with a project (e.g. pubsub node used for merge-requests). |
42 """)) | 42 """)) |
43 } | 43 } |
44 | 44 |
47 | 47 |
48 | 48 |
49 class URIFinder(object): | 49 class URIFinder(object): |
50 | 50 |
51 def __init__(self, host): | 51 def __init__(self, host): |
52 log.info(_(u"URI finder plugin initialization")) | 52 log.info(_("URI finder plugin initialization")) |
53 self.host = host | 53 self.host = host |
54 host.bridge.addMethod("URIFind", ".plugin", | 54 host.bridge.addMethod("URIFind", ".plugin", |
55 in_sign='sas', out_sign='a{sa{ss}}', | 55 in_sign='sas', out_sign='a{sa{ss}}', |
56 method=self.find, | 56 method=self.find, |
57 async=True) | 57 async_=True) |
58 | 58 |
59 def find(self, path, keys): | 59 def find(self, path, keys): |
60 """Look for URI in well known locations | 60 """Look for URI in well known locations |
61 | 61 |
62 @param path(unicode): path to start with | 62 @param path(unicode): path to start with |
63 @param keys(list[unicode]): keys lookeds after | 63 @param keys(list[unicode]): keys lookeds after |
64 e.g.: "tickets", "merge-requests" | 64 e.g.: "tickets", "merge-requests" |
65 @return (dict[unicode, unicode]): map from key to found uri | 65 @return (dict[unicode, unicode]): map from key to found uri |
66 """ | 66 """ |
67 keys_re = u'|'.join(keys) | 67 keys_re = '|'.join(keys) |
68 label_re = r'"(?P<label>[^"]+)"' | 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( | 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)) | 70 keys_re=keys_re, label_re = label_re)) |
71 path = os.path.normpath(path) | 71 path = os.path.normpath(path) |
72 if not os.path.isdir(path) or not os.path.isabs(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') | 73 raise ValueError('path must be an absolute path to a directory') |
74 | 74 |
75 found_uris = {} | 75 found_uris = {} |
76 while path != u'/': | 76 while path != '/': |
77 for filename in os.listdir(path): | 77 for filename in os.listdir(path): |
78 name, __ = os.path.splitext(filename) | 78 name, __ = os.path.splitext(filename) |
79 if name.lower() in SEARCH_FILES: | 79 if name.lower() in SEARCH_FILES: |
80 file_path = os.path.join(path, filename) | 80 file_path = os.path.join(path, filename) |
81 with open(file_path) as f: | 81 with open(file_path) as f: |
82 for m in uri_re.finditer(f.read()): | 82 for m in uri_re.finditer(f.read()): |
83 key = m.group(u'key') | 83 key = m.group('key') |
84 uri = m.group(u'uri') | 84 uri = m.group('uri') |
85 label = m.group(u'label') | 85 label = m.group('label') |
86 if key in found_uris: | 86 if key in found_uris: |
87 log.warning(_(u"Ignoring already found uri for key \"{key}\"").format(key=key)) | 87 log.warning(_("Ignoring already found uri for key \"{key}\"").format(key=key)) |
88 else: | 88 else: |
89 uri_data = found_uris[key] = {u'uri': uri} | 89 uri_data = found_uris[key] = {'uri': uri} |
90 if label is not None: | 90 if label is not None: |
91 uri_data[u'labels'] = json.dumps([label]) | 91 uri_data['labels'] = json.dumps([label]) |
92 if found_uris: | 92 if found_uris: |
93 break | 93 break |
94 path = os.path.dirname(path) | 94 path = os.path.dirname(path) |
95 | 95 |
96 return defer.succeed(found_uris) | 96 return defer.succeed(found_uris) |