Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix seven contrib files with Python syntax errors #5446

Merged
merged 3 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5446.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update Python syntax in contrib/ to Python 3.
109 changes: 55 additions & 54 deletions contrib/cmdclient/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.

""" Starts a synapse client console. """
from __future__ import print_function

from twisted.internet import reactor, defer, threads
from http import TwistedHttpClient
Expand Down Expand Up @@ -109,7 +110,7 @@ def do_config(self, line):
by using $. E.g. 'config roomid room1' then 'raw get /rooms/$roomid'.
"""
if len(line) == 0:
print json.dumps(self.config, indent=4)
print(json.dumps(self.config, indent=4))
return

try:
Expand All @@ -123,8 +124,8 @@ def do_config(self, line):
]
for key, valid_vals in config_rules:
if key == args["key"] and args["val"] not in valid_vals:
print "%s value must be one of %s" % (args["key"],
valid_vals)
print("%s value must be one of %s" % (args["key"],
valid_vals))
return

# toggle the http client verbosity
Expand All @@ -133,11 +134,11 @@ def do_config(self, line):

# assign the new config
self.config[args["key"]] = args["val"]
print json.dumps(self.config, indent=4)
print(json.dumps(self.config, indent=4))

save_config(self.config)
except Exception as e:
print e
print(e)

def do_register(self, line):
"""Registers for a new account: "register <userid> <noupdate>"
Expand All @@ -153,7 +154,7 @@ def do_register(self, line):
pwd = getpass.getpass("Type a password for this user: ")
pwd2 = getpass.getpass("Retype the password: ")
if pwd != pwd2 or len(pwd) == 0:
print "Password mismatch."
print("Password mismatch.")
pwd = None
else:
password = pwd
Expand All @@ -174,12 +175,12 @@ def _do_register(self, data, update_config):
# check the registration flows
url = self._url() + "/register"
json_res = yield self.http_client.do_request("GET", url)
print json.dumps(json_res, indent=4)
print(json.dumps(json_res, indent=4))

passwordFlow = None
for flow in json_res["flows"]:
if flow["type"] == "m.login.recaptcha" or ("stages" in flow and "m.login.recaptcha" in flow["stages"]):
print "Unable to register: Home server requires captcha."
print("Unable to register: Home server requires captcha.")
return
if flow["type"] == "m.login.password" and "stages" not in flow:
passwordFlow = flow
Expand All @@ -189,7 +190,7 @@ def _do_register(self, data, update_config):
return

json_res = yield self.http_client.do_request("POST", url, data=data)
print json.dumps(json_res, indent=4)
print(json.dumps(json_res, indent=4))
if update_config and "user_id" in json_res:
self.config["user"] = json_res["user_id"]
self.config["token"] = json_res["access_token"]
Expand All @@ -215,7 +216,7 @@ def do_login(self, line):
reactor.callFromThread(self._do_login, user, p)
#print " got %s " % p
except Exception as e:
print e
print(e)

@defer.inlineCallbacks
def _do_login(self, user, password):
Expand All @@ -227,13 +228,13 @@ def _do_login(self, user, password):
}
url = self._url() + path
json_res = yield self.http_client.do_request("POST", url, data=data)
print json_res
print(json_res)

if "access_token" in json_res:
self.config["user"] = user
self.config["token"] = json_res["access_token"]
save_config(self.config)
print "Login successful."
print("Login successful.")

@defer.inlineCallbacks
def _check_can_login(self):
Expand All @@ -242,10 +243,10 @@ def _check_can_login(self):
# submitting!
url = self._url() + path
json_res = yield self.http_client.do_request("GET", url)
print json_res
print(json_res)

if "flows" not in json_res:
print "Failed to find any login flows."
print("Failed to find any login flows.")
defer.returnValue(False)

flow = json_res["flows"][0] # assume first is the one we want.
Expand Down Expand Up @@ -275,9 +276,9 @@ def _do_emailrequest(self, args):

json_res = yield self.http_client.do_request("POST", url, data=urllib.urlencode(args), jsonreq=False,
headers={'Content-Type': ['application/x-www-form-urlencoded']})
print json_res
print(json_res)
if 'sid' in json_res:
print "Token sent. Your session ID is %s" % (json_res['sid'])
print("Token sent. Your session ID is %s" % (json_res['sid']))

def do_emailvalidate(self, line):
"""Validate and associate a third party ID
Expand All @@ -297,7 +298,7 @@ def _do_emailvalidate(self, args):

json_res = yield self.http_client.do_request("POST", url, data=urllib.urlencode(args), jsonreq=False,
headers={'Content-Type': ['application/x-www-form-urlencoded']})
print json_res
print(json_res)

def do_3pidbind(self, line):
"""Validate and associate a third party ID
Expand All @@ -317,23 +318,23 @@ def _do_3pidbind(self, args):

json_res = yield self.http_client.do_request("POST", url, data=urllib.urlencode(args), jsonreq=False,
headers={'Content-Type': ['application/x-www-form-urlencoded']})
print json_res
print(json_res)

def do_join(self, line):
"""Joins a room: "join <roomid>" """
try:
args = self._parse(line, ["roomid"], force_keys=True)
self._do_membership_change(args["roomid"], "join", self._usr())
except Exception as e:
print e
print(e)

def do_joinalias(self, line):
try:
args = self._parse(line, ["roomname"], force_keys=True)
path = "/join/%s" % urllib.quote(args["roomname"])
reactor.callFromThread(self._run_and_pprint, "POST", path, {})
except Exception as e:
print e
print(e)

def do_topic(self, line):
""""topic [set|get] <roomid> [<newtopic>]"
Expand All @@ -343,17 +344,17 @@ def do_topic(self, line):
try:
args = self._parse(line, ["action", "roomid", "topic"])
if "action" not in args or "roomid" not in args:
print "Must specify set|get and a room ID."
print("Must specify set|get and a room ID.")
return
if args["action"].lower() not in ["set", "get"]:
print "Must specify set|get, not %s" % args["action"]
print("Must specify set|get, not %s" % args["action"])
return

path = "/rooms/%s/topic" % urllib.quote(args["roomid"])

if args["action"].lower() == "set":
if "topic" not in args:
print "Must specify a new topic."
print("Must specify a new topic.")
return
body = {
"topic": args["topic"]
Expand All @@ -362,7 +363,7 @@ def do_topic(self, line):
elif args["action"].lower() == "get":
reactor.callFromThread(self._run_and_pprint, "GET", path)
except Exception as e:
print e
print(e)

def do_invite(self, line):
"""Invite a user to a room: "invite <userid> <roomid>" """
Expand All @@ -373,7 +374,7 @@ def do_invite(self, line):

reactor.callFromThread(self._do_invite, args["roomid"], user_id)
except Exception as e:
print e
print(e)

@defer.inlineCallbacks
def _do_invite(self, roomid, userstring):
Expand All @@ -393,29 +394,29 @@ def _do_invite(self, roomid, userstring):
if 'public_key' in pubKeyObj:
pubKey = nacl.signing.VerifyKey(pubKeyObj['public_key'], encoder=nacl.encoding.HexEncoder)
else:
print "No public key found in pubkey response!"
print("No public key found in pubkey response!")

sigValid = False

if pubKey:
for signame in json_res['signatures']:
if signame not in TRUSTED_ID_SERVERS:
print "Ignoring signature from untrusted server %s" % (signame)
print("Ignoring signature from untrusted server %s" % (signame))
else:
try:
verify_signed_json(json_res, signame, pubKey)
sigValid = True
print "Mapping %s -> %s correctly signed by %s" % (userstring, json_res['mxid'], signame)
print("Mapping %s -> %s correctly signed by %s" % (userstring, json_res['mxid'], signame))
break
except SignatureVerifyException as e:
print "Invalid signature from %s" % (signame)
print e
print("Invalid signature from %s" % (signame))
print(e)

if sigValid:
print "Resolved 3pid %s to %s" % (userstring, json_res['mxid'])
print("Resolved 3pid %s to %s" % (userstring, json_res['mxid']))
mxid = json_res['mxid']
else:
print "Got association for %s but couldn't verify signature" % (userstring)
print("Got association for %s but couldn't verify signature" % (userstring))

if not mxid:
mxid = "@" + userstring + ":" + self._domain()
Expand All @@ -428,7 +429,7 @@ def do_leave(self, line):
args = self._parse(line, ["roomid"], force_keys=True)
self._do_membership_change(args["roomid"], "leave", self._usr())
except Exception as e:
print e
print(e)

def do_send(self, line):
"""Sends a message. "send <roomid> <body>" """
Expand All @@ -453,10 +454,10 @@ def do_list(self, line):
"""
args = self._parse(line, ["type", "roomid", "qp"])
if not "type" in args or not "roomid" in args:
print "Must specify type and room ID."
print("Must specify type and room ID.")
return
if args["type"] not in ["members", "messages"]:
print "Unrecognised type: %s" % args["type"]
print("Unrecognised type: %s" % args["type"])
return
room_id = args["roomid"]
path = "/rooms/%s/%s" % (urllib.quote(room_id), args["type"])
Expand All @@ -468,7 +469,7 @@ def do_list(self, line):
key_value = key_value_str.split("=")
qp[key_value[0]] = key_value[1]
except:
print "Bad query param: %s" % key_value
print("Bad query param: %s" % key_value)
return

reactor.callFromThread(self._run_and_pprint, "GET", path,
Expand Down Expand Up @@ -508,14 +509,14 @@ def do_raw(self, line):
args = self._parse(line, ["method", "path", "data"])
# sanity check
if "method" not in args or "path" not in args:
print "Must specify path and method."
print("Must specify path and method.")
return

args["method"] = args["method"].upper()
valid_methods = ["PUT", "GET", "POST", "DELETE",
"XPUT", "XGET", "XPOST", "XDELETE"]
if args["method"] not in valid_methods:
print "Unsupported method: %s" % args["method"]
print("Unsupported method: %s" % args["method"])
return

if "data" not in args:
Expand All @@ -524,7 +525,7 @@ def do_raw(self, line):
try:
args["data"] = json.loads(args["data"])
except Exception as e:
print "Data is not valid JSON. %s" % e
print("Data is not valid JSON. %s" % e)
return

qp = {"access_token": self._tok()}
Expand Down Expand Up @@ -553,7 +554,7 @@ def do_stream(self, line):
try:
timeout = int(args["timeout"])
except ValueError:
print "Timeout must be in milliseconds."
print("Timeout must be in milliseconds.")
return
reactor.callFromThread(self._do_event_stream, timeout)

Expand All @@ -566,7 +567,7 @@ def _do_event_stream(self, timeout):
"timeout": str(timeout),
"from": self.event_stream_token
})
print json.dumps(res, indent=4)
print(json.dumps(res, indent=4))

if "chunk" in res:
for event in res["chunk"]:
Expand Down Expand Up @@ -669,9 +670,9 @@ def _run_and_pprint(self, method, path, data=None,
data=data,
qparams=query_params)
if alt_text:
print alt_text
print(alt_text)
else:
print json.dumps(json_res, indent=4)
print(json.dumps(json_res, indent=4))


def save_config(config):
Expand All @@ -680,16 +681,16 @@ def save_config(config):


def main(server_url, identity_server_url, username, token, config_path):
print "Synapse command line client"
print "==========================="
print "Server: %s" % server_url
print "Type 'help' to get started."
print "Close this console with CTRL+C then CTRL+D."
print("Synapse command line client")
print("===========================")
print("Server: %s" % server_url)
print("Type 'help' to get started.")
print("Close this console with CTRL+C then CTRL+D.")
if not username or not token:
print "- 'register <username>' - Register an account"
print "- 'stream' - Connect to the event stream"
print "- 'create <roomid>' - Create a room"
print "- 'send <roomid> <message>' - Send a message"
print("- 'register <username>' - Register an account")
print("- 'stream' - Connect to the event stream")
print("- 'create <roomid>' - Create a room")
print("- 'send <roomid> <message>' - Send a message")
http_client = TwistedHttpClient()

# the command line client
Expand All @@ -705,7 +706,7 @@ def main(server_url, identity_server_url, username, token, config_path):
http_client.verbose = "on" == syn_cmd.config["verbose"]
except:
pass
print "Loaded config from %s" % config_path
print("Loaded config from %s" % config_path)
except:
pass

Expand Down Expand Up @@ -736,7 +737,7 @@ def main(server_url, identity_server_url, username, token, config_path):
args = parser.parse_args()

if not args.server:
print "You must supply a server URL to communicate with."
print("You must supply a server URL to communicate with.")
parser.print_help()
sys.exit(1)

Expand Down
Loading