annotate sqlpython.py @ 154:4680d0629b82

before proper packaging
author catherine@dellzilla
date Mon, 29 Sep 2008 09:42:35 -0400
parents 802d8df993da
children
rev   line source
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
1 #
151
802d8df993da midway through making plots saveable
catherine@dellzilla
parents: 150
diff changeset
2 # SqlPython V1.5.0
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
3 # Author: Luca.Canali@cern.ch, Apr 2006
103
4aa28dffe658 command-line args working well
catherine@localhost
parents: 102
diff changeset
4 # Rev 29-May-08
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
5 #
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
6 # A python module to reproduce Oracle's command line 'sqlplus-like' within python
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
7 # Intended to allow easy customizations and extentions
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
8 # Best used with the companion modules sqlpyPlus and mysqlpy
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
9 # See also http://twiki.cern.ch/twiki/bin/view/PSSGroup/SqlPython
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
10
102
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
11 import cmd2,getpass,binascii,cx_Oracle,re,os
80
83de0cb04f12 prevent crash on lone backslash
catherine@localhost
parents: 67
diff changeset
12 import pexpecter, sqlpyPlus
151
802d8df993da midway through making plots saveable
catherine@dellzilla
parents: 150
diff changeset
13 __version__ = '1.5.0'
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
14 # complication! separate sessions ->
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
15 # separate transactions !!!!!
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
16 # also: timeouts, other session failures
40
1fb9f7dee7d8 tearing out cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 16
diff changeset
17
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 9
diff changeset
18 class sqlpython(cmd2.Cmd):
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
19 '''A python module to reproduce Oracle's command line with focus on customization and extention'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
20
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
21 def __init__(self):
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 9
diff changeset
22 cmd2.Cmd.__init__(self)
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
23 self.prompt = 'SQL.No_Connection> '
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
24 self.maxfetch = 1000
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
25 self.failoverSessions = []
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
26 self.terminator = ';'
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
27 self.timeout = 30
123
898ed97bec38 fixed bug in setting parameters
catherine@Elli.myhome.westell.com
parents: 120
diff changeset
28 self.commit_on_exit = True
102
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
29
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
30 connection_modes = {re.compile(' AS SYSDBA', re.IGNORECASE): cx_Oracle.SYSDBA,
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
31 re.compile(' AS SYSOPER', re.IGNORECASE): cx_Oracle.SYSOPER}
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
32 def do_connect(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
33 '''Opens the DB connection'''
102
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
34 modeval = 0
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
35 for modere, modevalue in self.connection_modes.items():
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
36 if modere.search(arg):
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
37 arg = modere.sub('', arg)
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
38 modeval = modevalue
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
39 try:
102
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
40 orauser, oraserv = arg.split('@')
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
41 except ValueError:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
42 try:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
43 oraserv = os.environ['ORACLE_SID']
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
44 except KeyError:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
45 print 'instance not specified and environment variable ORACLE_SID not set'
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
46 return
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
47 orauser = arg
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
48 sid = oraserv
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
49 try:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
50 host, sid = oraserv.split('/')
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
51 try:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
52 host, port = host.split(':')
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
53 port = int(port)
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
54 except ValueError:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
55 port = 1521
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
56 oraserv = cx_Oracle.makedsn(host, port, sid)
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
57 except ValueError:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
58 pass
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
59 try:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
60 orauser, orapass = orauser.split('/')
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
61 except ValueError:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
62 orapass = getpass.getpass('Password: ')
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
63 if orauser.upper() == 'SYS' and not modeval:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
64 print 'Privilege not specified for SYS, assuming SYSOPER'
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
65 modeval = cx_Oracle.SYSOPER
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
66 try:
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
67 self.orcl = cx_Oracle.connect(orauser,orapass,oraserv,modeval)
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
68 self.curs = self.orcl.cursor()
102
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
69 self.prompt = '%s@%s> ' % (orauser, sid)
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
70 self.failoverSessions = [f for f in [fbs(arg) for fbs in pexpecter.available] if f.available]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
71 except Exception, e:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
72 print e
102
c5f601abc993 login with AS SYSDBA, easy connect
catherine@localhost
parents: 92
diff changeset
73
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
74
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
75 def emptyline(self):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
76 pass
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
77
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
78 def fail(self, arg, do_everywhere=False):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
79 if self.failover:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
80 success, result = False, ''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
81 for fbs in self.failoverSessions:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
82 success, result = fbs.attempt(arg)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
83 if success:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
84 print result
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
85 if not do_everywhere:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
86 return True
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
87 print result
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
88 return False
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
89
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
90 def designated_session(self, arg, sesstype):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
91 for fbs in self.failoverSessions:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
92 if fbs.valid and fbs.__class__ == sesstype:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
93 success, result = fbs.attempt(arg)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
94 print result
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
95 return
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
96 print 'Valid %s not found' % (sesstype.__name__)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
97
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
98 def do_terminators(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
99 """; standard Oracle format
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
100 \\c CSV (with headings)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
101 \\C CSV (no headings)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
102 \\g list
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
103 \\G aligned list
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
104 \\h HTML table
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
105 \\i INSERT statements
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
106 \\s CSV (with headings)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
107 \\S CSV (no headings)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
108 \\t transposed
151
802d8df993da midway through making plots saveable
catherine@dellzilla
parents: 150
diff changeset
109 \\x XML
802d8df993da midway through making plots saveable
catherine@dellzilla
parents: 150
diff changeset
110 \\p plot, with markers
802d8df993da midway through making plots saveable
catherine@dellzilla
parents: 150
diff changeset
111 \\P plot, continuous lines"""
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
112 print self.do_terminators.__doc__
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
113
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
114 terminatorSearchString = '|'.join('\\' + d.split()[0] for d in do_terminators.__doc__.splitlines())
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
115
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
116 def do_yasql(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
117 '''Sends a command to a YASQL session (http://sourceforge.net/projects/yasql/)'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
118 self.designated_session(arg, pexpecter.YASQLSession)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
119 do_y = do_yasql
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
120 def do_sqlplus(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
121 '''Sends a command to a SQL*Plus session'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
122 self.designated_session(arg, pexpecter.SqlPlusSession)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
123 do_sqlp = do_sqlplus
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
124 def do_senora(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
125 '''Sends a command to a Senora session (http://senora.sourceforge.net/)'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
126 self.designated_session(arg, pexpecter.SenoraSession)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
127 do_sen = do_senora
9
f70cc3be6377 reporting rowcount
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 3
diff changeset
128
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
129 def default(self, arg, do_everywhere = False):
117
dfb71885dd41 everything seems working
catherine@Elli.myhome.westell.com
parents: 109
diff changeset
130 statement = self.parsed(arg)
dfb71885dd41 everything seems working
catherine@Elli.myhome.westell.com
parents: 109
diff changeset
131 self.query = statement.unterminated
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
132 try:
80
83de0cb04f12 prevent crash on lone backslash
catherine@localhost
parents: 67
diff changeset
133 self.varsUsed = sqlpyPlus.findBinds(self.query, self.binds, givenBindVars={})
83de0cb04f12 prevent crash on lone backslash
catherine@localhost
parents: 67
diff changeset
134 self.curs.execute(self.query, self.varsUsed)
9
f70cc3be6377 reporting rowcount
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 3
diff changeset
135 print '\nExecuted%s\n' % ((self.curs.rowcount > 0) and ' (%d rows)' % self.curs.rowcount or '')
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
136 if do_everywhere:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
137 self.fail(arg, do_everywhere = True )
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
138 except Exception, e:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
139 result = self.fail(arg)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
140 if not result:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
141 print str(e)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
142
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
143 def do_commit(self, arg):
67
d0bf9e40ba8d eliminate double-semicolon on commit
catherine@DellZilla.myhome.westell.com
parents: 55
diff changeset
144 self.default('commit %s;' % (arg), do_everywhere=True)
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
145 def do_rollback(self, arg):
67
d0bf9e40ba8d eliminate double-semicolon on commit
catherine@DellZilla.myhome.westell.com
parents: 55
diff changeset
146 self.default('rollback %s;' % (arg), do_everywhere=True)
123
898ed97bec38 fixed bug in setting parameters
catherine@Elli.myhome.westell.com
parents: 120
diff changeset
147 def do_quit(self, arg):
124
2b42b7bc29cb commit only if cursor
catherine@localhost
parents: 123
diff changeset
148 if self.commit_on_exit and hasattr(self, 'curs'):
123
898ed97bec38 fixed bug in setting parameters
catherine@Elli.myhome.westell.com
parents: 120
diff changeset
149 self.default('commit;')
898ed97bec38 fixed bug in setting parameters
catherine@Elli.myhome.westell.com
parents: 120
diff changeset
150 cmd2.Cmd.do_quit()
898ed97bec38 fixed bug in setting parameters
catherine@Elli.myhome.westell.com
parents: 120
diff changeset
151 do_exit = do_quit
898ed97bec38 fixed bug in setting parameters
catherine@Elli.myhome.westell.com
parents: 120
diff changeset
152 do_q = do_quit
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
153
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
154 def pmatrix(rows,desc,maxlen=30):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
155 '''prints a matrix, used by sqlpython to print queries' result sets'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
156 names = []
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
157 maxen = []
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
158 toprint = []
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
159 for d in desc:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
160 n = d[0]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
161 names.append(n) # list col names
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
162 maxen.append(len(n)) # col length
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
163 rcols = range(len(desc))
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
164 rrows = range(len(rows))
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
165 for i in rrows: # loops for all rows
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
166 rowsi = map(str, rows[i]) # current row to process
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
167 split = [] # service var is row split is needed
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
168 mustsplit = 0 # flag
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
169 for j in rcols:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
170 if str(desc[j][1]) == "<type 'cx_Oracle.BINARY'>": # handles RAW columns
80
83de0cb04f12 prevent crash on lone backslash
catherine@localhost
parents: 67
diff changeset
171 rowsi[j] = binascii.b2a_hex(rowsi[j])
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
172 maxen[j] = max(maxen[j], len(rowsi[j])) # computes max field length
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
173 if maxen[j] <= maxlen:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
174 split.append('')
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
175 else: # split the line is 2 because field is too long
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
176 mustsplit = 1
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
177 maxen[j] = maxlen
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
178 split.append(rowsi[j][maxlen-1:2*maxlen-1])
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
179 rowsi[j] = rowsi[j][0:maxlen-1] # this implem. truncates after maxlen*2
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
180 toprint.append(rowsi) # 'toprint' is a printable copy of rows
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
181 if mustsplit != 0:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
182 toprint.append(split)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
183 sepcols = []
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
184 for i in rcols:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
185 maxcol = maxen[i]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
186 name = names[i]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
187 sepcols.append("-" * maxcol) # formats column names (header)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
188 names[i] = name + (" " * (maxcol-len(name))) # formats separ line with --
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
189 rrows2 = range(len(toprint))
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
190 for j in rrows2:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
191 val = toprint[j][i]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
192 if str(desc[i][1]) == "<type 'cx_Oracle.NUMBER'>": # right align numbers
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
193 toprint[j][i] = (" " * (maxcol-len(val))) + val
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
194 else:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
195 toprint[j][i] = val + (" " * (maxcol-len(val)))
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
196 for j in rrows2:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
197 toprint[j] = ' '.join(toprint[j])
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
198 names = ' '.join(names)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
199 sepcols = ' '.join(sepcols)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
200 toprint.insert(0, sepcols)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
201 toprint.insert(0, names)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
202 return '\n'.join(toprint)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
203