Port federation/ to py3 (#3847)

This commit is contained in:
Amber Brown 2018-09-12 23:23:32 +10:00 committed by GitHub
parent 546aee7e52
commit 7ca097f77e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 19 deletions

1
changelog.d/3847.misc Normal file
View File

@ -0,0 +1 @@
federation/ is now ported to Python 3.

View File

@ -271,10 +271,10 @@ class FederationClient(FederationBase):
event_id, destination, e, event_id, destination, e,
) )
except NotRetryingDestination as e: except NotRetryingDestination as e:
logger.info(e.message) logger.info(str(e))
continue continue
except FederationDeniedError as e: except FederationDeniedError as e:
logger.info(e.message) logger.info(str(e))
continue continue
except Exception as e: except Exception as e:
pdu_attempts[destination] = now pdu_attempts[destination] = now
@ -510,7 +510,7 @@ class FederationClient(FederationBase):
else: else:
logger.warn( logger.warn(
"Failed to %s via %s: %i %s", "Failed to %s via %s: %i %s",
description, destination, e.code, e.message, description, destination, e.code, e.args[0],
) )
except Exception: except Exception:
logger.warn( logger.warn(
@ -875,7 +875,7 @@ class FederationClient(FederationBase):
except Exception as e: except Exception as e:
logger.exception( logger.exception(
"Failed to send_third_party_invite via %s: %s", "Failed to send_third_party_invite via %s: %s",
destination, e.message destination, str(e)
) )
raise RuntimeError("Failed to send to any server.") raise RuntimeError("Failed to send to any server.")

View File

@ -15,7 +15,8 @@
# limitations under the License. # limitations under the License.
import logging import logging
import urllib
from six.moves import urllib
from twisted.internet import defer from twisted.internet import defer
@ -951,4 +952,4 @@ def _create_path(prefix, path, *args):
Returns: Returns:
str str
""" """
return prefix + path % tuple(urllib.quote(arg, "") for arg in args) return prefix + path % tuple(urllib.parse.quote(arg, "") for arg in args)

View File

@ -90,8 +90,8 @@ class Authenticator(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def authenticate_request(self, request, content): def authenticate_request(self, request, content):
json_request = { json_request = {
"method": request.method, "method": request.method.decode('ascii'),
"uri": request.uri, "uri": request.uri.decode('ascii'),
"destination": self.server_name, "destination": self.server_name,
"signatures": {}, "signatures": {},
} }
@ -252,7 +252,7 @@ class BaseFederationServlet(object):
by the callback method. None if the request has already been handled. by the callback method. None if the request has already been handled.
""" """
content = None content = None
if request.method in ["PUT", "POST"]: if request.method in [b"PUT", b"POST"]:
# TODO: Handle other method types? other content types? # TODO: Handle other method types? other content types?
content = parse_json_object_from_request(request) content = parse_json_object_from_request(request)
@ -386,7 +386,7 @@ class FederationStateServlet(BaseFederationServlet):
return self.handler.on_context_state_request( return self.handler.on_context_state_request(
origin, origin,
context, context,
query.get("event_id", [None])[0], parse_string_from_args(query, "event_id", None),
) )
@ -397,7 +397,7 @@ class FederationStateIdsServlet(BaseFederationServlet):
return self.handler.on_state_ids_request( return self.handler.on_state_ids_request(
origin, origin,
room_id, room_id,
query.get("event_id", [None])[0], parse_string_from_args(query, "event_id", None),
) )
@ -405,14 +405,12 @@ class FederationBackfillServlet(BaseFederationServlet):
PATH = "/backfill/(?P<context>[^/]*)/" PATH = "/backfill/(?P<context>[^/]*)/"
def on_GET(self, origin, content, query, context): def on_GET(self, origin, content, query, context):
versions = query["v"] versions = [x.decode('ascii') for x in query[b"v"]]
limits = query["limit"] limit = parse_integer_from_args(query, "limit", None)
if not limits: if not limit:
return defer.succeed((400, {"error": "Did not include limit param"})) return defer.succeed((400, {"error": "Did not include limit param"}))
limit = int(limits[-1])
return self.handler.on_backfill_request(origin, context, versions, limit) return self.handler.on_backfill_request(origin, context, versions, limit)
@ -423,7 +421,7 @@ class FederationQueryServlet(BaseFederationServlet):
def on_GET(self, origin, content, query, query_type): def on_GET(self, origin, content, query, query_type):
return self.handler.on_query_request( return self.handler.on_query_request(
query_type, query_type,
{k: v[0].decode("utf-8") for k, v in query.items()} {k.decode('utf8'): v[0].decode("utf-8") for k, v in query.items()}
) )
@ -630,14 +628,14 @@ class OpenIdUserInfo(BaseFederationServlet):
@defer.inlineCallbacks @defer.inlineCallbacks
def on_GET(self, origin, content, query): def on_GET(self, origin, content, query):
token = query.get("access_token", [None])[0] token = query.get(b"access_token", [None])[0]
if token is None: if token is None:
defer.returnValue((401, { defer.returnValue((401, {
"errcode": "M_MISSING_TOKEN", "error": "Access Token required" "errcode": "M_MISSING_TOKEN", "error": "Access Token required"
})) }))
return return
user_id = yield self.handler.on_openid_userinfo(token) user_id = yield self.handler.on_openid_userinfo(token.decode('ascii'))
if user_id is None: if user_id is None:
defer.returnValue((401, { defer.returnValue((401, {