annotate sqlpython.py @ 21:8b55aaa52ce9

working on load, and preserving stdin/out
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Wed, 19 Dec 2007 13:40:39 -0500
parents 2776755a3a7e
children 1fb9f7dee7d8
rev   line source
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
1 #
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
2 # SqlPython V1.3
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
3 # Author: Luca.Canali@cern.ch, Apr 2006
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
4 # Rev 18-Oct-07
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
16
2776755a3a7e beginning separation of cmd2
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 9
diff changeset
11 import cmd2,getpass,binascii,cx_Oracle,re
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
12 import pexpecter
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
13
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
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
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
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
28
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
29 def do_connect(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
30 '''Opens the DB connection'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
31 try:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
32 if arg.find('/') == -1:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
33 orapass = getpass.getpass('Password: ')
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
34 orauser = arg.split('@')[0]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
35 oraserv = arg.split('@')[1]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
36 self.orcl = cx_Oracle.connect(orauser,orapass,oraserv)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
37 arg = '%s/%s@%s' % (orauser, orapass, oraserv)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
38 else:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
39 self.orcl = cx_Oracle.connect(arg)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
40 self.curs = self.orcl.cursor()
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
41 self.prompt = 'SQL.'+self.orcl.tnsentry+'> '
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
42 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
43
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
44 except Exception, e:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
45 print e
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
46
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
47 def emptyline(self):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
48 pass
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
49
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
50 def do_quit(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
51 return 1
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
52
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
53 def fail(self, arg, do_everywhere=False):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
54 if self.failover:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
55 success, result = False, ''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
56 for fbs in self.failoverSessions:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
57 success, result = fbs.attempt(arg)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
58 if success:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
59 print result
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
60 if not do_everywhere:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
61 return True
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
62 print result
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
63 return False
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
64
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
65 def designated_session(self, arg, sesstype):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
66 for fbs in self.failoverSessions:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
67 if fbs.valid and fbs.__class__ == sesstype:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
68 success, result = fbs.attempt(arg)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
69 print result
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
70 return
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
71 print 'Valid %s not found' % (sesstype.__name__)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
72
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
73 def do_terminators(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
74 """; standard Oracle format
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
75 \\c CSV (with headings)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
76 \\C CSV (no headings)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
77 \\g list
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
78 \\G aligned list
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
79 \\h HTML table
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
80 \\i INSERT statements
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
81 \\s CSV (with headings)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
82 \\S CSV (no headings)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
83 \\t transposed
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
84 \\x XML"""
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
85 print self.do_terminators.__doc__
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
86
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
87 terminatorSearchString = '|'.join('\\' + d.split()[0] for d in do_terminators.__doc__.splitlines())
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
88
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
89 def do_yasql(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
90 '''Sends a command to a YASQL session (http://sourceforge.net/projects/yasql/)'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
91 self.designated_session(arg, pexpecter.YASQLSession)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
92 do_y = do_yasql
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
93 def do_sqlplus(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
94 '''Sends a command to a SQL*Plus session'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
95 self.designated_session(arg, pexpecter.SqlPlusSession)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
96 do_sqlp = do_sqlplus
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
97 def do_senora(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
98 '''Sends a command to a Senora session (http://senora.sourceforge.net/)'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
99 self.designated_session(arg, pexpecter.SenoraSession)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
100 do_sen = do_senora
9
f70cc3be6377 reporting rowcount
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 3
diff changeset
101
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
102 def default(self, arg, do_everywhere = False):
9
f70cc3be6377 reporting rowcount
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 3
diff changeset
103 self.query = finishStatement(arg).strip().rstrip(';')
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
104 try:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
105 self.curs.execute(self.query)
9
f70cc3be6377 reporting rowcount
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 3
diff changeset
106 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
107 if do_everywhere:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
108 self.fail(arg, do_everywhere = True )
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
109 except Exception, e:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
110 result = self.fail(arg)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
111 if not result:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
112 print str(e)
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 def do_commit(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
115 self.default('commit %s' % (arg), do_everywhere=True)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
116 def do_rollback(self, arg):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
117 self.default('rollback %s' % (arg), do_everywhere=True)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
118
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
119 # shortcuts
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
120 do_q = do_quit
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
121 do_exit = do_quit
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
122
2
59903dcaf327 working on history
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 1
diff changeset
123 stmtEndSearchString = r'(.*)(%s)\s*(\d+)?\s*$' % sqlpython.terminatorSearchString
1
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
124 stmtEndFinder = re.compile(stmtEndSearchString, re.MULTILINE | re.DOTALL)
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
125 prompt2 = ' > '
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
126
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
127 def finishStatement(firstline):
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
128 lines = [firstline]
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
129 while 1:
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
130 m = stmtEndFinder.search(lines[-1])
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
131 if m:
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
132 return '\n'.join(lines)
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
133 lines.append(raw_input(prompt2))
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
134
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
135 def findTerminator(statement):
8fa146b9a2d7 reworking multiline
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 0
diff changeset
136 m = stmtEndFinder.search(statement)
3
cd23cd62de3c history working pretty well
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 2
diff changeset
137 if m:
cd23cd62de3c history working pretty well
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 2
diff changeset
138 return m.groups()
cd23cd62de3c history working pretty well
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 2
diff changeset
139 else:
cd23cd62de3c history working pretty well
devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
parents: 2
diff changeset
140 return statement, None, None
0
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
141
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
142 def pmatrix(rows,desc,maxlen=30):
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
143 '''prints a matrix, used by sqlpython to print queries' result sets'''
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
144 names = []
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
145 maxen = []
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
146 toprint = []
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
147 for d in desc:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
148 n = d[0]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
149 names.append(n) # list col names
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
150 maxen.append(len(n)) # col length
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
151 rcols = range(len(desc))
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
152 rrows = range(len(rows))
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
153 for i in rrows: # loops for all rows
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
154 rowsi = map(str, rows[i]) # current row to process
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
155 split = [] # service var is row split is needed
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
156 mustsplit = 0 # flag
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
157 for j in rcols:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
158 if str(desc[j][1]) == "<type 'cx_Oracle.BINARY'>": # handles RAW columns
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
159 rowsi[j] = binascii.b2a_hex(rowsi[j])
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
160 maxen[j] = max(maxen[j], len(rowsi[j])) # computes max field length
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
161 if maxen[j] <= maxlen:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
162 split.append('')
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
163 else: # split the line is 2 because field is too long
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
164 mustsplit = 1
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
165 maxen[j] = maxlen
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
166 split.append(rowsi[j][maxlen-1:2*maxlen-1])
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
167 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
168 toprint.append(rowsi) # 'toprint' is a printable copy of rows
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
169 if mustsplit != 0:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
170 toprint.append(split)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
171 sepcols = []
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
172 for i in rcols:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
173 maxcol = maxen[i]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
174 name = names[i]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
175 sepcols.append("-" * maxcol) # formats column names (header)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
176 names[i] = name + (" " * (maxcol-len(name))) # formats separ line with --
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
177 rrows2 = range(len(toprint))
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
178 for j in rrows2:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
179 val = toprint[j][i]
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
180 if str(desc[i][1]) == "<type 'cx_Oracle.NUMBER'>": # right align numbers
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
181 toprint[j][i] = (" " * (maxcol-len(val))) + val
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
182 else:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
183 toprint[j][i] = val + (" " * (maxcol-len(val)))
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
184 for j in rrows2:
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
185 toprint[j] = ' '.join(toprint[j])
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
186 names = ' '.join(names)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
187 sepcols = ' '.join(sepcols)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
188 toprint.insert(0, sepcols)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
189 toprint.insert(0, names)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
190 return '\n'.join(toprint)
9c87fa772ec1 before big refactor
catherine@serenity.wpafb.af.mil
parents:
diff changeset
191