Skip to content

Commit

Permalink
Prepare for py3k: print(), except as, and imports.
Browse files Browse the repository at this point in the history
 * pyrseas/dbconn.py, pyrseas/dbobject/constraint.py,
   pyrseas/dbobject/index.py, pyrseas/dbobject/language.py,
   pyrseas/dbobject/rule.py, pyrseas/dbobject/schema.py
   pyrseas/dbobject/table.py, pyrseas/dbobject/textsearch.py: Change
   except Xxx, exc to except Xxx as exc.
 * pyrseas/dbobject/table.py, pyrseas/dbobject/schema.py: Use full
   paths for imports.
 * pyrseas/dbtoyaml.py, pyrseas/yamltodb.py: Replace print statement
   by print() function.
  • Loading branch information
jmafc committed Jan 10, 2012
1 parent 6f458cf commit ddaf933
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pyrseas/dbconn.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _execute(self, query):
curs = self.conn.cursor() curs = self.conn.cursor()
try: try:
curs.execute(query) curs.execute(query)
except Exception, exc: except Exception as exc:
exc.args += (query, ) exc.args += (query, )
raise raise
return curs return curs
Expand Down
14 changes: 7 additions & 7 deletions pyrseas/dbobject/constraint.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def from_map(self, table, inconstrs, target=''):
val = chks[cns] val = chks[cns]
try: try:
check.expression = val['expression'] check.expression = val['expression']
except KeyError, exc: except KeyError as exc:
exc.args = ("Constraint '%s' is missing expression" exc.args = ("Constraint '%s' is missing expression"
% cns, ) % cns, )
raise raise
Expand All @@ -342,7 +342,7 @@ def from_map(self, table, inconstrs, target=''):
val = inconstrs['primary_key'][cns] val = inconstrs['primary_key'][cns]
try: try:
pkey.keycols = val['columns'] pkey.keycols = val['columns']
except KeyError, exc: except KeyError as exc:
exc.args = ("Constraint '%s' is missing columns" % cns, ) exc.args = ("Constraint '%s' is missing columns" % cns, )
raise raise
if 'access_method' in val: if 'access_method' in val:
Expand Down Expand Up @@ -374,23 +374,23 @@ def from_map(self, table, inconstrs, target=''):
fkey.deferred = True fkey.deferred = True
try: try:
fkey.keycols = val['columns'] fkey.keycols = val['columns']
except KeyError, exc: except KeyError as exc:
exc.args = ("Constraint '%s' is missing columns" % cns, ) exc.args = ("Constraint '%s' is missing columns" % cns, )
raise raise
try: try:
refs = val['references'] refs = val['references']
except KeyError, exc: except KeyError as exc:
exc.args = ("Constraint '%s' missing references" % cns, ) exc.args = ("Constraint '%s' missing references" % cns, )
raise raise
try: try:
fkey.ref_table = refs['table'] fkey.ref_table = refs['table']
except KeyError, exc: except KeyError as exc:
exc.args = ("Constraint '%s' missing table reference" exc.args = ("Constraint '%s' missing table reference"
% cns, ) % cns, )
raise raise
try: try:
fkey.ref_cols = refs['columns'] fkey.ref_cols = refs['columns']
except KeyError, exc: except KeyError as exc:
exc.args = ("Constraint '%s' missing reference columns" exc.args = ("Constraint '%s' missing reference columns"
% cns, ) % cns, )
raise raise
Expand All @@ -409,7 +409,7 @@ def from_map(self, table, inconstrs, target=''):
val = uconstrs[cns] val = uconstrs[cns]
try: try:
unq.keycols = val['columns'] unq.keycols = val['columns']
except KeyError, exc: except KeyError as exc:
exc.args = ("Constraint '%s' is missing columns" % cns, ) exc.args = ("Constraint '%s' is missing columns" % cns, )
raise raise
if 'access_method' in val: if 'access_method' in val:
Expand Down
2 changes: 1 addition & 1 deletion pyrseas/dbobject/index.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def diff_map(self, inindexes):
stmts.append(self[(sch, tbl, oldname)].rename( stmts.append(self[(sch, tbl, oldname)].rename(
inidx.name)) inidx.name))
del self[(sch, tbl, oldname)] del self[(sch, tbl, oldname)]
except KeyError, exc: except KeyError as exc:
exc.args = ("Previous name '%s' for index '%s' " exc.args = ("Previous name '%s' for index '%s' "
"not found" % (oldname, inidx.name), ) "not found" % (oldname, inidx.name), )
raise raise
Expand Down
2 changes: 1 addition & 1 deletion pyrseas/dbobject/language.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def diff_map(self, inlanguages, dbversion):
try: try:
stmts.append(self[oldname].rename(inlng.name)) stmts.append(self[oldname].rename(inlng.name))
del self[oldname] del self[oldname]
except KeyError, exc: except KeyError as exc:
exc.args = ("Previous name '%s' for language '%s' " exc.args = ("Previous name '%s' for language '%s' "
"not found" % (oldname, inlng.name), ) "not found" % (oldname, inlng.name), )
raise raise
Expand Down
2 changes: 1 addition & 1 deletion pyrseas/dbobject/rule.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def diff_map(self, inrules):
try: try:
stmts.append(self[oldname].rename(inrul.name)) stmts.append(self[oldname].rename(inrul.name))
del self[oldname] del self[oldname]
except KeyError, exc: except KeyError as exc:
exc.args = ("Previous name '%s' for rule '%s' " exc.args = ("Previous name '%s' for rule '%s' "
"not found" % (oldname, inrul.name), ) "not found" % (oldname, inrul.name), )
raise raise
Expand Down
6 changes: 3 additions & 3 deletions pyrseas/dbobject/schema.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
""" """
from pyrseas.dbobject import DbObjectDict, DbObject from pyrseas.dbobject import DbObjectDict, DbObject
from pyrseas.dbobject import quote_id, split_schema_obj from pyrseas.dbobject import quote_id, split_schema_obj
from dbtype import BaseType, Composite, Domain, Enum from pyrseas.dbobject.dbtype import BaseType, Composite, Domain, Enum
from table import Table, Sequence, View from pyrseas.dbobject.table import Table, Sequence, View




class Schema(DbObject): class Schema(DbObject):
Expand Down Expand Up @@ -312,7 +312,7 @@ def diff_map(self, inschemas):
try: try:
stmts.append(self[oldname].rename(insch.name)) stmts.append(self[oldname].rename(insch.name))
del self[oldname] del self[oldname]
except KeyError, exc: except KeyError as exc:
exc.args = ("Previous name '%s' for schema '%s' " exc.args = ("Previous name '%s' for schema '%s' "
"not found" % (oldname, insch.name), ) "not found" % (oldname, insch.name), )
raise raise
Expand Down
8 changes: 4 additions & 4 deletions pyrseas/dbobject/table.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@


from pyrseas.dbobject import DbObjectDict, DbSchemaObject from pyrseas.dbobject import DbObjectDict, DbSchemaObject
from pyrseas.dbobject import quote_id, split_schema_obj from pyrseas.dbobject import quote_id, split_schema_obj
from constraint import CheckConstraint, PrimaryKey, ForeignKey, \ from pyrseas.dbobject.constraint import CheckConstraint, PrimaryKey
UniqueConstraint from pyrseas.dbobject.constraint import ForeignKey, UniqueConstraint


MAX_BIGINT = 9223372036854775807L MAX_BIGINT = 9223372036854775807L


Expand Down Expand Up @@ -422,7 +422,7 @@ def from_map(self, schema, inobjs, newdb):
raise ValueError("Table '%s' has no specification" % k) raise ValueError("Table '%s' has no specification" % k)
try: try:
newdb.columns.from_map(table, intable['columns']) newdb.columns.from_map(table, intable['columns'])
except KeyError, exc: except KeyError as exc:
exc.args = ("Table '%s' has no columns" % key, ) exc.args = ("Table '%s' has no columns" % key, )
raise raise
if 'inherits' in intable: if 'inherits' in intable:
Expand Down Expand Up @@ -551,7 +551,7 @@ def _rename(self, obj, objtype):
stmt = self[(obj.schema, oldname)].rename(obj.name) stmt = self[(obj.schema, oldname)].rename(obj.name)
self[(obj.schema, obj.name)] = self[(obj.schema, oldname)] self[(obj.schema, obj.name)] = self[(obj.schema, oldname)]
del self[(obj.schema, oldname)] del self[(obj.schema, oldname)]
except KeyError, exc: except KeyError as exc:
exc.args = ("Previous name '%s' for %s '%s' not found" % ( exc.args = ("Previous name '%s' for %s '%s' not found" % (
oldname, objtype, obj.name), ) oldname, objtype, obj.name), )
raise raise
Expand Down
8 changes: 4 additions & 4 deletions pyrseas/dbobject/textsearch.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def diff_map(self, inconfigs):
try: try:
stmts.append(self[oldname].rename(intsc.name)) stmts.append(self[oldname].rename(intsc.name))
del self[oldname] del self[oldname]
except KeyError, exc: except KeyError as exc:
exc.args = ("Previous name '%s' for configuration " exc.args = ("Previous name '%s' for configuration "
"'%s' not found" % (oldname, intsc.name), ) "'%s' not found" % (oldname, intsc.name), )
raise raise
Expand Down Expand Up @@ -201,7 +201,7 @@ def diff_map(self, indicts):
try: try:
stmts.append(self[oldname].rename(intsd.name)) stmts.append(self[oldname].rename(intsd.name))
del self[oldname] del self[oldname]
except KeyError, exc: except KeyError as exc:
exc.args = ("Previous name '%s' for dictionary '%s' " exc.args = ("Previous name '%s' for dictionary '%s' "
"not found" % (oldname, intsd.name), ) "not found" % (oldname, intsd.name), )
raise raise
Expand Down Expand Up @@ -300,7 +300,7 @@ def diff_map(self, inparsers):
try: try:
stmts.append(self[oldname].rename(intsp.name)) stmts.append(self[oldname].rename(intsp.name))
del self[oldname] del self[oldname]
except KeyError, exc: except KeyError as exc:
exc.args = ("Previous name '%s' for parser '%s' " exc.args = ("Previous name '%s' for parser '%s' "
"not found" % (oldname, intsp.name), ) "not found" % (oldname, intsp.name), )
raise raise
Expand Down Expand Up @@ -396,7 +396,7 @@ def diff_map(self, intemplates):
try: try:
stmts.append(self[oldname].rename(intst.name)) stmts.append(self[oldname].rename(intst.name))
del self[oldname] del self[oldname]
except KeyError, exc: except KeyError as exc:
exc.args = ("Previous name '%s' for template '%s' " exc.args = ("Previous name '%s' for template '%s' "
"not found" % (oldname, intst.name), ) "not found" % (oldname, intst.name), )
raise raise
Expand Down
3 changes: 2 additions & 1 deletion pyrseas/dbtoyaml.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""dbtoyaml - extract the schema of a PostgreSQL database in YAML format""" """dbtoyaml - extract the schema of a PostgreSQL database in YAML format"""


from __future__ import print_function
import os import os
from optparse import OptionParser from optparse import OptionParser


Expand Down Expand Up @@ -54,7 +55,7 @@ def main(host='localhost', port=5432, schema=None):
if not dbmap[sch]: if not dbmap[sch]:
del dbmap[sch] del dbmap[sch]


print yaml.dump(dbmap, default_flow_style=False) print(yaml.dump(dbmap, default_flow_style=False))


if __name__ == '__main__': if __name__ == '__main__':
main() main()
9 changes: 5 additions & 4 deletions pyrseas/yamltodb.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""yamltodb - generate SQL statements to update a PostgreSQL database """yamltodb - generate SQL statements to update a PostgreSQL database
to match the schema specified in a YAML file""" to match the schema specified in a YAML file"""


from __future__ import print_function
import os import os
import sys import sys
from optparse import OptionParser from optparse import OptionParser
Expand Down Expand Up @@ -51,10 +52,10 @@ def main(host='localhost', port=5432):
stmts = db.diff_map(inmap, options.schlist) stmts = db.diff_map(inmap, options.schlist)
if stmts: if stmts:
if options.onetrans or options.update: if options.onetrans or options.update:
print "BEGIN;" print("BEGIN;")
print ";\n".join(stmts) + ';' print(";\n".join(stmts) + ';')
if options.onetrans or options.update: if options.onetrans or options.update:
print "COMMIT;" print("COMMIT;")
if options.update: if options.update:
dbconn.connect() dbconn.connect()
try: try:
Expand All @@ -65,7 +66,7 @@ def main(host='localhost', port=5432):
raise raise
else: else:
dbconn.conn.commit() dbconn.conn.commit()
print >> sys.stderr, "Changes applied" print("Changes applied", file=sys.stderr)


if __name__ == '__main__': if __name__ == '__main__':
main() main()

0 comments on commit ddaf933

Please sign in to comment.