Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0300.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | 40d47cc29ea4 |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
66 ) | 66 ) |
67 ALGO_DEFAULT = ALGO_DEFAULT | 67 ALGO_DEFAULT = ALGO_DEFAULT |
68 | 68 |
69 def __init__(self, host): | 69 def __init__(self, host): |
70 log.info(_("plugin Hashes initialization")) | 70 log.info(_("plugin Hashes initialization")) |
71 host.registerNamespace("hashes", NS_HASHES) | 71 host.register_namespace("hashes", NS_HASHES) |
72 | 72 |
73 def getHandler(self, client): | 73 def get_handler(self, client): |
74 return XEP_0300_handler() | 74 return XEP_0300_handler() |
75 | 75 |
76 def getHasher(self, algo=ALGO_DEFAULT): | 76 def get_hasher(self, algo=ALGO_DEFAULT): |
77 """Return hasher instance | 77 """Return hasher instance |
78 | 78 |
79 @param algo(unicode): one of the XEP_300.ALGOS keys | 79 @param algo(unicode): one of the XEP_300.ALGOS keys |
80 @return (hash object): same object s in hashlib. | 80 @return (hash object): same object s in hashlib. |
81 update method need to be called for each chunh | 81 update method need to be called for each chunh |
82 diget or hexdigest can be used at the end | 82 diget or hexdigest can be used at the end |
83 """ | 83 """ |
84 return self.ALGOS[algo]() | 84 return self.ALGOS[algo]() |
85 | 85 |
86 def getDefaultAlgo(self): | 86 def get_default_algo(self): |
87 return ALGO_DEFAULT | 87 return ALGO_DEFAULT |
88 | 88 |
89 @defer.inlineCallbacks | 89 @defer.inlineCallbacks |
90 def getBestPeerAlgo(self, to_jid, profile): | 90 def get_best_peer_algo(self, to_jid, profile): |
91 """Return the best available hashing algorith of other peer | 91 """Return the best available hashing algorith of other peer |
92 | 92 |
93 @param to_jid(jid.JID): peer jid | 93 @param to_jid(jid.JID): peer jid |
94 @parm profile: %(doc_profile)s | 94 @parm profile: %(doc_profile)s |
95 @return (D(unicode, None)): best available algorithm, | 95 @return (D(unicode, None)): best available algorithm, |
96 or None if hashing is not possible | 96 or None if hashing is not possible |
97 """ | 97 """ |
98 client = self.host.getClient(profile) | 98 client = self.host.get_client(profile) |
99 for algo in reversed(XEP_0300.ALGOS): | 99 for algo in reversed(XEP_0300.ALGOS): |
100 has_feature = yield self.host.hasFeature( | 100 has_feature = yield self.host.hasFeature( |
101 client, NS_HASHES_FUNCTIONS.format(algo), to_jid | 101 client, NS_HASHES_FUNCTIONS.format(algo), to_jid |
102 ) | 102 ) |
103 if has_feature: | 103 if has_feature: |
106 jid=to_jid.full(), algo=algo | 106 jid=to_jid.full(), algo=algo |
107 ) | 107 ) |
108 ) | 108 ) |
109 defer.returnValue(algo) | 109 defer.returnValue(algo) |
110 | 110 |
111 def _calculateHashBlocking(self, file_obj, hasher): | 111 def _calculate_hash_blocking(self, file_obj, hasher): |
112 """Calculate hash in a blocking way | 112 """Calculate hash in a blocking way |
113 | 113 |
114 /!\\ blocking method, please use calculateHash instead | 114 /!\\ blocking method, please use calculate_hash instead |
115 @param file_obj(file): a file-like object | 115 @param file_obj(file): a file-like object |
116 @param hasher(hash object): the method to call to initialise hash object | 116 @param hasher(hash object): the method to call to initialise hash object |
117 @return (str): the hex digest of the hash | 117 @return (str): the hex digest of the hash |
118 """ | 118 """ |
119 while True: | 119 while True: |
121 if not buf: | 121 if not buf: |
122 break | 122 break |
123 hasher.update(buf) | 123 hasher.update(buf) |
124 return hasher.hexdigest() | 124 return hasher.hexdigest() |
125 | 125 |
126 def calculateHash(self, file_obj, hasher): | 126 def calculate_hash(self, file_obj, hasher): |
127 return threads.deferToThread(self._calculateHashBlocking, file_obj, hasher) | 127 return threads.deferToThread(self._calculate_hash_blocking, file_obj, hasher) |
128 | 128 |
129 def calculateHashElt(self, file_obj=None, algo=ALGO_DEFAULT): | 129 def calculate_hash_elt(self, file_obj=None, algo=ALGO_DEFAULT): |
130 """Compute hash and build hash element | 130 """Compute hash and build hash element |
131 | 131 |
132 @param file_obj(file, None): file-like object to use to calculate the hash | 132 @param file_obj(file, None): file-like object to use to calculate the hash |
133 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS | 133 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS |
134 @return (D(domish.Element)): hash element | 134 @return (D(domish.Element)): hash element |
135 """ | 135 """ |
136 | 136 |
137 def hashCalculated(hash_): | 137 def hash_calculated(hash_): |
138 return self.buildHashElt(hash_, algo) | 138 return self.build_hash_elt(hash_, algo) |
139 | 139 |
140 hasher = self.getHasher(algo) | 140 hasher = self.get_hasher(algo) |
141 hash_d = self.calculateHash(file_obj, hasher) | 141 hash_d = self.calculate_hash(file_obj, hasher) |
142 hash_d.addCallback(hashCalculated) | 142 hash_d.addCallback(hash_calculated) |
143 return hash_d | 143 return hash_d |
144 | 144 |
145 def buildHashUsedElt(self, algo=ALGO_DEFAULT): | 145 def build_hash_used_elt(self, algo=ALGO_DEFAULT): |
146 hash_used_elt = domish.Element((NS_HASHES, "hash-used")) | 146 hash_used_elt = domish.Element((NS_HASHES, "hash-used")) |
147 hash_used_elt["algo"] = algo | 147 hash_used_elt["algo"] = algo |
148 return hash_used_elt | 148 return hash_used_elt |
149 | 149 |
150 def parseHashUsedElt(self, parent): | 150 def parse_hash_used_elt(self, parent): |
151 """Find and parse a hash-used element | 151 """Find and parse a hash-used element |
152 | 152 |
153 @param (domish.Element): parent of <hash/> element | 153 @param (domish.Element): parent of <hash/> element |
154 @return (unicode): hash algorithm used | 154 @return (unicode): hash algorithm used |
155 @raise exceptions.NotFound: the element is not present | 155 @raise exceptions.NotFound: the element is not present |
162 algo = hash_used_elt["algo"] | 162 algo = hash_used_elt["algo"] |
163 if not algo: | 163 if not algo: |
164 raise exceptions.DataError | 164 raise exceptions.DataError |
165 return algo | 165 return algo |
166 | 166 |
167 def buildHashElt(self, hash_, algo=ALGO_DEFAULT): | 167 def build_hash_elt(self, hash_, algo=ALGO_DEFAULT): |
168 """Compute hash and build hash element | 168 """Compute hash and build hash element |
169 | 169 |
170 @param hash_(str): hash to use | 170 @param hash_(str): hash to use |
171 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS | 171 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS |
172 @return (domish.Element): computed hash | 172 @return (domish.Element): computed hash |
178 b64_hash = base64.b64encode(hash_.encode('utf-8')).decode('utf-8') | 178 b64_hash = base64.b64encode(hash_.encode('utf-8')).decode('utf-8') |
179 hash_elt.addContent(b64_hash) | 179 hash_elt.addContent(b64_hash) |
180 hash_elt["algo"] = algo | 180 hash_elt["algo"] = algo |
181 return hash_elt | 181 return hash_elt |
182 | 182 |
183 def parseHashElt(self, parent: domish.Element) -> Tuple[str, bytes]: | 183 def parse_hash_elt(self, parent: domish.Element) -> Tuple[str, bytes]: |
184 """Find and parse a hash element | 184 """Find and parse a hash element |
185 | 185 |
186 if multiple elements are found, the strongest managed one is returned | 186 if multiple elements are found, the strongest managed one is returned |
187 @param parent: parent of <hash/> element | 187 @param parent: parent of <hash/> element |
188 @return: (algo, hash) tuple | 188 @return: (algo, hash) tuple |