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

Commit bde5a6b

Browse files
committed
Fix seven contrib files with Python syntax errors (#5446)
2 parents 3c9b5d5 + 82d9d52 commit bde5a6b

File tree

7 files changed

+131
-119
lines changed

7 files changed

+131
-119
lines changed

changelog.d/5446.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update Python syntax in contrib/ to Python 3.

contrib/cmdclient/console.py

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616

1717
""" Starts a synapse client console. """
18+
from __future__ import print_function
1819

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

115116
try:
@@ -123,8 +124,8 @@ def do_config(self, line):
123124
]
124125
for key, valid_vals in config_rules:
125126
if key == args["key"] and args["val"] not in valid_vals:
126-
print "%s value must be one of %s" % (args["key"],
127-
valid_vals)
127+
print("%s value must be one of %s" % (args["key"],
128+
valid_vals))
128129
return
129130

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

134135
# assign the new config
135136
self.config[args["key"]] = args["val"]
136-
print json.dumps(self.config, indent=4)
137+
print(json.dumps(self.config, indent=4))
137138

138139
save_config(self.config)
139140
except Exception as e:
140-
print e
141+
print(e)
141142

142143
def do_register(self, line):
143144
"""Registers for a new account: "register <userid> <noupdate>"
@@ -153,7 +154,7 @@ def do_register(self, line):
153154
pwd = getpass.getpass("Type a password for this user: ")
154155
pwd2 = getpass.getpass("Retype the password: ")
155156
if pwd != pwd2 or len(pwd) == 0:
156-
print "Password mismatch."
157+
print("Password mismatch.")
157158
pwd = None
158159
else:
159160
password = pwd
@@ -174,12 +175,12 @@ def _do_register(self, data, update_config):
174175
# check the registration flows
175176
url = self._url() + "/register"
176177
json_res = yield self.http_client.do_request("GET", url)
177-
print json.dumps(json_res, indent=4)
178+
print(json.dumps(json_res, indent=4))
178179

179180
passwordFlow = None
180181
for flow in json_res["flows"]:
181182
if flow["type"] == "m.login.recaptcha" or ("stages" in flow and "m.login.recaptcha" in flow["stages"]):
182-
print "Unable to register: Home server requires captcha."
183+
print("Unable to register: Home server requires captcha.")
183184
return
184185
if flow["type"] == "m.login.password" and "stages" not in flow:
185186
passwordFlow = flow
@@ -189,7 +190,7 @@ def _do_register(self, data, update_config):
189190
return
190191

191192
json_res = yield self.http_client.do_request("POST", url, data=data)
192-
print json.dumps(json_res, indent=4)
193+
print(json.dumps(json_res, indent=4))
193194
if update_config and "user_id" in json_res:
194195
self.config["user"] = json_res["user_id"]
195196
self.config["token"] = json_res["access_token"]
@@ -215,7 +216,7 @@ def do_login(self, line):
215216
reactor.callFromThread(self._do_login, user, p)
216217
#print " got %s " % p
217218
except Exception as e:
218-
print e
219+
print(e)
219220

220221
@defer.inlineCallbacks
221222
def _do_login(self, user, password):
@@ -227,13 +228,13 @@ def _do_login(self, user, password):
227228
}
228229
url = self._url() + path
229230
json_res = yield self.http_client.do_request("POST", url, data=data)
230-
print json_res
231+
print(json_res)
231232

232233
if "access_token" in json_res:
233234
self.config["user"] = user
234235
self.config["token"] = json_res["access_token"]
235236
save_config(self.config)
236-
print "Login successful."
237+
print("Login successful.")
237238

238239
@defer.inlineCallbacks
239240
def _check_can_login(self):
@@ -242,10 +243,10 @@ def _check_can_login(self):
242243
# submitting!
243244
url = self._url() + path
244245
json_res = yield self.http_client.do_request("GET", url)
245-
print json_res
246+
print(json_res)
246247

247248
if "flows" not in json_res:
248-
print "Failed to find any login flows."
249+
print("Failed to find any login flows.")
249250
defer.returnValue(False)
250251

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

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

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

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

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

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

322323
def do_join(self, line):
323324
"""Joins a room: "join <roomid>" """
324325
try:
325326
args = self._parse(line, ["roomid"], force_keys=True)
326327
self._do_membership_change(args["roomid"], "join", self._usr())
327328
except Exception as e:
328-
print e
329+
print(e)
329330

330331
def do_joinalias(self, line):
331332
try:
332333
args = self._parse(line, ["roomname"], force_keys=True)
333334
path = "/join/%s" % urllib.quote(args["roomname"])
334335
reactor.callFromThread(self._run_and_pprint, "POST", path, {})
335336
except Exception as e:
336-
print e
337+
print(e)
337338

338339
def do_topic(self, line):
339340
""""topic [set|get] <roomid> [<newtopic>]"
@@ -343,17 +344,17 @@ def do_topic(self, line):
343344
try:
344345
args = self._parse(line, ["action", "roomid", "topic"])
345346
if "action" not in args or "roomid" not in args:
346-
print "Must specify set|get and a room ID."
347+
print("Must specify set|get and a room ID.")
347348
return
348349
if args["action"].lower() not in ["set", "get"]:
349-
print "Must specify set|get, not %s" % args["action"]
350+
print("Must specify set|get, not %s" % args["action"])
350351
return
351352

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

354355
if args["action"].lower() == "set":
355356
if "topic" not in args:
356-
print "Must specify a new topic."
357+
print("Must specify a new topic.")
357358
return
358359
body = {
359360
"topic": args["topic"]
@@ -362,7 +363,7 @@ def do_topic(self, line):
362363
elif args["action"].lower() == "get":
363364
reactor.callFromThread(self._run_and_pprint, "GET", path)
364365
except Exception as e:
365-
print e
366+
print(e)
366367

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

374375
reactor.callFromThread(self._do_invite, args["roomid"], user_id)
375376
except Exception as e:
376-
print e
377+
print(e)
377378

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

398399
sigValid = False
399400

400401
if pubKey:
401402
for signame in json_res['signatures']:
402403
if signame not in TRUSTED_ID_SERVERS:
403-
print "Ignoring signature from untrusted server %s" % (signame)
404+
print("Ignoring signature from untrusted server %s" % (signame))
404405
else:
405406
try:
406407
verify_signed_json(json_res, signame, pubKey)
407408
sigValid = True
408-
print "Mapping %s -> %s correctly signed by %s" % (userstring, json_res['mxid'], signame)
409+
print("Mapping %s -> %s correctly signed by %s" % (userstring, json_res['mxid'], signame))
409410
break
410411
except SignatureVerifyException as e:
411-
print "Invalid signature from %s" % (signame)
412-
print e
412+
print("Invalid signature from %s" % (signame))
413+
print(e)
413414

414415
if sigValid:
415-
print "Resolved 3pid %s to %s" % (userstring, json_res['mxid'])
416+
print("Resolved 3pid %s to %s" % (userstring, json_res['mxid']))
416417
mxid = json_res['mxid']
417418
else:
418-
print "Got association for %s but couldn't verify signature" % (userstring)
419+
print("Got association for %s but couldn't verify signature" % (userstring))
419420

420421
if not mxid:
421422
mxid = "@" + userstring + ":" + self._domain()
@@ -428,7 +429,7 @@ def do_leave(self, line):
428429
args = self._parse(line, ["roomid"], force_keys=True)
429430
self._do_membership_change(args["roomid"], "leave", self._usr())
430431
except Exception as e:
431-
print e
432+
print(e)
432433

433434
def do_send(self, line):
434435
"""Sends a message. "send <roomid> <body>" """
@@ -453,10 +454,10 @@ def do_list(self, line):
453454
"""
454455
args = self._parse(line, ["type", "roomid", "qp"])
455456
if not "type" in args or not "roomid" in args:
456-
print "Must specify type and room ID."
457+
print("Must specify type and room ID.")
457458
return
458459
if args["type"] not in ["members", "messages"]:
459-
print "Unrecognised type: %s" % args["type"]
460+
print("Unrecognised type: %s" % args["type"])
460461
return
461462
room_id = args["roomid"]
462463
path = "/rooms/%s/%s" % (urllib.quote(room_id), args["type"])
@@ -468,7 +469,7 @@ def do_list(self, line):
468469
key_value = key_value_str.split("=")
469470
qp[key_value[0]] = key_value[1]
470471
except:
471-
print "Bad query param: %s" % key_value
472+
print("Bad query param: %s" % key_value)
472473
return
473474

474475
reactor.callFromThread(self._run_and_pprint, "GET", path,
@@ -508,14 +509,14 @@ def do_raw(self, line):
508509
args = self._parse(line, ["method", "path", "data"])
509510
# sanity check
510511
if "method" not in args or "path" not in args:
511-
print "Must specify path and method."
512+
print("Must specify path and method.")
512513
return
513514

514515
args["method"] = args["method"].upper()
515516
valid_methods = ["PUT", "GET", "POST", "DELETE",
516517
"XPUT", "XGET", "XPOST", "XDELETE"]
517518
if args["method"] not in valid_methods:
518-
print "Unsupported method: %s" % args["method"]
519+
print("Unsupported method: %s" % args["method"])
519520
return
520521

521522
if "data" not in args:
@@ -524,7 +525,7 @@ def do_raw(self, line):
524525
try:
525526
args["data"] = json.loads(args["data"])
526527
except Exception as e:
527-
print "Data is not valid JSON. %s" % e
528+
print("Data is not valid JSON. %s" % e)
528529
return
529530

530531
qp = {"access_token": self._tok()}
@@ -553,7 +554,7 @@ def do_stream(self, line):
553554
try:
554555
timeout = int(args["timeout"])
555556
except ValueError:
556-
print "Timeout must be in milliseconds."
557+
print("Timeout must be in milliseconds.")
557558
return
558559
reactor.callFromThread(self._do_event_stream, timeout)
559560

@@ -566,7 +567,7 @@ def _do_event_stream(self, timeout):
566567
"timeout": str(timeout),
567568
"from": self.event_stream_token
568569
})
569-
print json.dumps(res, indent=4)
570+
print(json.dumps(res, indent=4))
570571

571572
if "chunk" in res:
572573
for event in res["chunk"]:
@@ -669,9 +670,9 @@ def _run_and_pprint(self, method, path, data=None,
669670
data=data,
670671
qparams=query_params)
671672
if alt_text:
672-
print alt_text
673+
print(alt_text)
673674
else:
674-
print json.dumps(json_res, indent=4)
675+
print(json.dumps(json_res, indent=4))
675676

676677

677678
def save_config(config):
@@ -680,16 +681,16 @@ def save_config(config):
680681

681682

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

695696
# the command line client
@@ -705,7 +706,7 @@ def main(server_url, identity_server_url, username, token, config_path):
705706
http_client.verbose = "on" == syn_cmd.config["verbose"]
706707
except:
707708
pass
708-
print "Loaded config from %s" % config_path
709+
print("Loaded config from %s" % config_path)
709710
except:
710711
pass
711712

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

738739
if not args.server:
739-
print "You must supply a server URL to communicate with."
740+
print("You must supply a server URL to communicate with.")
740741
parser.print_help()
741742
sys.exit(1)
742743

0 commit comments

Comments
 (0)