changeset 48:3d5adf5a59d0

Fix python2.7 syntax errors.
author David A. Holland
date Sat, 02 Apr 2022 18:05:27 -0400
parents bcd1d06838fd
children 4b7f0ee35994
files shelltools/query-pr/query.py
diffstat 1 files changed, 66 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/shelltools/query-pr/query.py	Wed Aug 13 03:03:30 2014 -0400
+++ b/shelltools/query-pr/query.py	Sat Apr 02 18:05:27 2022 -0400
@@ -1,5 +1,6 @@
 #!@PYTHON@
 
+import sys
 import argparse
 import psycopg2
 
@@ -40,8 +41,8 @@
 
 	# these fields are aliases for others
 	alias_fields = {
-		"number" : "id"
-		"date" : "arrival_date"
+		"number" : "id",
+		"date" : "arrival_date",
 	}
 
 	def __init__(self):
@@ -60,10 +61,10 @@
 		return name
 
 	# add a join item (once only) (internal)
-	def addjoin(self, table, as = None):
-		if as is not None:
-			key = table + "-" + as
-			val = table + " AS " + as
+	def addjoin(self, table, as_ = None):
+		if as_ is not None:
+			key = table + "-" + as_
+			val = table + " AS " + as_
 		else:
 			key = table
 			val = table
@@ -127,14 +128,14 @@
 			return self.makepresent(field, "patches.body")
 
 		if field == "mimetype":
-			subquery = "((SELECT mtmessages1.pr as pr, " +
-				"mtmessages1.mimetype as mimetype " +
-				"FROM messages as mtmessages1) " +
-				"UNION " +
-				"(SELECT mtmessages2.pr as pr, " +
-				"mtattach2.mimetype as mimetype " +
-				"FROM messages as mtmessages2, " +
-				"     attachments as mtattach2 " +
+			subquery = "((SELECT mtmessages1.pr as pr, " + \
+				"mtmessages1.mimetype as mimetype " + \
+				"FROM messages as mtmessages1) " + \
+				"UNION " + \
+				"(SELECT mtmessages2.pr as pr, " + \
+				"mtattach2.mimetype as mimetype " + \
+				"FROM messages as mtmessages2, " + \
+				"     attachments as mtattach2 " + \
 				"WHERE mtmessages2.id = mtattach2.msgid))"
 			self.addjoin("PRs")
 			self.addjoin(subquery, "mimetypes")
@@ -262,7 +263,7 @@
 # XXX: obsolete, remove
 
 class Query:
-	__init__(self):
+	def __init__(self):
 		self.selections = []
 		self.tables = []
 		self.constraints = []
@@ -396,7 +397,7 @@
 		checkprtable = True
 		regexp_constraint(prq, "PRs.environment", args.environment)
 
-	if args.originator_name is not None or 
+	if args.originator_name is not None or \
 			args.originator_email is not None:
 		prq.addtable("usermail as originator")
 		prq.constrain("PRs.originator = originator.id")
@@ -487,66 +488,88 @@
 		self.lines = (output == "RAW" or output == "LIST")
 	def printheader(self, row):
 		# nothing
+                pass
 	def printrow(self, row):
 		# XXX
 		print row
 	def printfooter(self, row):
 		# nothing
+                pass
 # end class PrintText
 
 class PrintCsv:
 	def __init__(self, output):
 		# nothing
+                pass
 	def printheader(self, row):
 		# XXX
+                pass
 	def printrow(self, row):
 		# XXX
+                pass
 	def printfooter(self, row):
 		# nothing
+                pass
 # end class PrintCsv
 
 class PrintXml:
 	def __init__(self, output):
 		# nothing
+                pass
 	def printheader(self, row):
 		# XXX
+                pass
 	def printrow(self, row):
 		# XXX
+                pass
 	def printfooter(self, row):
 		# XXX
+                pass
 # end class PrintXml
 
 class PrintJson:
 	def __init__(self, output):
 		# nothing
+                pass
 	def printheader(self, row):
 		# XXX
+                pass
 	def printrow(self, row):
 		# XXX
+                pass
 	def printfooter(self, row):
 		# XXX
+                pass
 # end class PrintJson
 
 class PrintRdf:
 	def __init__(self, output):
 		# nothing
+                pass
 	def printheader(self, row):
 		# XXX
+                pass
 	def printrow(self, row):
 		# XXX
+                pass
 	def printfooter(self, row):
 		# XXX
+                pass
 # end class PrintRdf
 
 class PrintRdflike:
 	def __init__(self, output):
 		# nothing
+                pass
 	def printheader(self, row):
 		# XXX
+                pass
 	def printrow(self, row):
 		# XXX
+                pass
 	def printfooter(self, row):
 		# XXX
+                pass
 # end class PrintRdflike
 
 def print_prs(ids):
@@ -577,8 +600,8 @@
 	elif sel.output == "LIST":
 		# XXX is there a clean way to do this passing the
 		# whole list of ids at once?
-		query = "SELECT id, synopsis\n" +
-			"FROM PRs\n" +
+		query = "SELECT id, synopsis\n" + \
+			"FROM PRs\n" + \
 			"WHERE id = $1"
 	elif sel.output == "HEADERS":
 		query = None # XXX
@@ -602,13 +625,13 @@
 
 # XXX if in public mode we need to check if the PR is public
 def print_message(pr, msgnum):
-	query = "SELECT users.username AS username,\n" +
-		"       users.realname AS realname,\n" +
-		"       messages.id AS id, parent_id,\n" +
-		"       posttime, mimetype, body\n" +
-		"FROM messages, users\n" +
-		"WHERE messages.who = users.id\n" +
-		"  AND messages.pr = $1\n" +
+	query = "SELECT users.username AS username,\n" + \
+		"       users.realname AS realname,\n" + \
+		"       messages.id AS id, parent_id,\n" + \
+		"       posttime, mimetype, body\n" + \
+		"FROM messages, users\n" + \
+		"WHERE messages.who = users.id\n" + \
+		"  AND messages.pr = $1\n" + \
 		"  AND messages.number_in_pr = $2\n"
 	# Note that while pr is safe, msgnum came from the commandline
 	# and may not be.
@@ -628,10 +651,10 @@
 
 # XXX if in public mode we need to check if the PR is public
 def print_attachment(pr, attachnum):
-	query = "SELECT a.mimetype as mimetype, a.body as body\n" +
-		"FROM messages, attachments as a\n" +
-		"WHERE messages.pr = $1\n" +
-		"  AND messages.id = a.msgid\n" +
+	query = "SELECT a.mimetype as mimetype, a.body as body\n" + \
+		"FROM messages, attachments as a\n" + \
+		"WHERE messages.pr = $1\n" + \
+		"  AND messages.id = a.msgid\n" + \
 		"  AND a.number_in_pr = $2\n"
 	# Note that while pr is safe, attachnum came from the
 	# commandline and may not be.
@@ -702,7 +725,7 @@
 	# end class Order
 
 	class Search:
-		def __init__(self, qs, openonly, publiconly, os)
+		def __init__(self, qs, openonly, publiconly, os):
 			self.queries = qs
 			self.openonly = openonly
 			self.publiconly = publiconly
@@ -765,8 +788,6 @@
 ############################################################
 # run (eval the SQL and print the results)
 
-def 
-
 def run_sel(sel, ids):
 	if sel.type == S_PR:
 		if ids == []:
@@ -817,6 +838,10 @@
 # on return from compile_query.
 #
 
+def matches(s, rx):
+        # XXX
+        return True
+
 def compile_query(q):
 	if q.type == Q_QSTRING:
 		# XXX should use a split that honors quotes
@@ -826,27 +851,27 @@
 	if q.type == Q_TSTRING:
 		qb = QueryBuilder()
 		s = q.string
-		if s ~ "^[0-9]+$":
+		if matches(s, "^[0-9]+$"):
 			f = qb.getfield("number")
 			# Note: s is user-supplied but clean to insert directly
 			qb.addwhere("%s = %s" % (f, s))
-		elif s ~ "^[0-9]+-[0-9]+$":
+		elif matches(s, "^[0-9]+-[0-9]+$"):
 			f = qb.getfield("number")
 			ss = s.split("-")
 			# Note: ss[] is user-supplied but clean
 			qb.addwhere("%s >= %s" % (f, ss[0]))
 			qb.addwhere("%s <= %s" % (f, ss[1]))
-		elif s ~ "^[0-9]+-$":
+		elif matches(s, "^[0-9]+-$"):
 			f = qb.getfield("number")
 			ss = s.split("-")
 			# Note: ss[] is user-supplied but clean
 			qb.addwhere("%s >= %s" % (f, ss[0]))
-		elif s ~ "^-[0-9]+$":
+		elif matches(s, "^-[0-9]+$"):
 			f = qb.getfield("number")
 			ss = s.split("-")
 			# Note: ss[] is user-supplied but clean
 			qb.addwhere("%s <= %s" % (f, ss[1]))
-		elif s ~ "^[^:]+:[^:]+$":
+		elif matches(s, "^[^:]+:[^:]+$"):
 			# XXX honor quoted terms
 			# XXX = or LIKE?
 			ss = s.split(":")
@@ -855,7 +880,7 @@
 			# ss[1] is not clean, so intern it for safety
 			s = qb.intern(ss[1])
 			qb.addwhere("%s = %s" % (f, s))
-		elif s ~ "^-[^:]+:[^:]+$"
+		elif matches(s, "^-[^:]+:[^:]+$"):
 			# XXX honor quoted terms
 			# XXX <> or NOT LIKE?
 			ss = s.split(":")
@@ -864,7 +889,7 @@
 			# ss[1] is not clean, so intern it for safety
 			s = qb.intern(ss[1])
 			qb.addwhere("%s <> %s" % (f, s))
-		elif s ~ "^-":
+		elif matches(s, "^-"):
 			# XXX <> or NOT LIKE?
 			f = qb.getfield("alltext")
 			# s is not clean, so intern it for safety
@@ -978,11 +1003,11 @@
 
 	p.add_argument("--show", nargs=1,
 			action=CtorAppend, dest='ops',
-			const=Invocation.Op.doshow
+			const=Invocation.Op.doshow,
 			help="Show description of field")
 	p.add_argument("--range", nargs=1,
 			action=CtorAppend, dest='ops',
-			const=Invocation.Op.dorange
+			const=Invocation.Op.dorange,
 			help="Show range of extant values for field")
 
 	p.add_argument("--search", nargs=1,