
Printing
Printing on the AS/400 can be a little tougher than on other platforms, so this module was created to provide a standard interface for almost RPG level of control over the output:
import os,sys,time
import string,types
import locale
import db2
class Printer:
def __init__(self,\
pagelength = 59,\
pagewidth = 132,\
header = '',\
footer = '',\
padding = 7,\
output = 'PRINT',\
paginate = 'TOPRIGHT',\
programid = '',\
regionname = '',\
regionpos = 'TOPLEFT',\
branchname = '',\
branchpos = 'TOPLEFT'\
):
self.pagelength = pagelength
self.pagewidth = pagewidth
self.header = header
self.footer = footer
self.padding = padding
self.output = output
self.paginate = paginate
self.programid = programid
self.regionname = regionname
self.regionpos = regionpos
self.branchname = branchname
self.branchpos = branchpos
self.page = 1
self.line = 1
self.CheckHeaders()
self.linecount = []
locale.setlocale(locale.LC_ALL,'/QSYS.LIB/EN_US.LOCALE')
def DrawFields(self,fields):
#
# DRAW A SERIES OF FIELDS ON A
LINE
#
# EXPECTS TO SEE 2-LEVEL ARRAY
LIKE:
# [ [ 'PART NUMBER',20], ['QUANTITY',7]
]
#
# ALIGNMENT PARAMETER IS OPTIONAL:
# [ [ 'PART NUMBER',20], ['QUANTITY',7,'RIGHT']
]
# Valid values for alignment: LEFT,RIGHT,CENTER,CURRENCY
text = []
for field in fields:
val = field[0]
pad = field[1]
align =
'LEFT'
if len(field)
> 2:
align = string.upper(field[2])
if align == 'CURRENCY':
align = 'RIGHT'
try:
val = locale.format('%.02f',val,3)
except:
pass
if type(val)
!= types.StringType:
val = str(val)
align = 'RIGHT'
#
# DRAW
THE FIELD
#
if align
== 'LEFT':
nval = string.ljust(val,pad)
elif align
== 'CENTER':
nval = string.center(val,pad)
else:
nval = string.rjust(val,pad)
text.append(nval)
self.Print(string.join(text,' '))
def CheckHeaders(self):
self.headersize = 0
self.footersize = 0
if self.header:
if self.header[-1]
== '\n':
self.header = self.header[:-1]
self.headersize
= string.count(self.header,'\n') -1
if self.footer:
if self.footer[-1]
== '\n':
self.footer = self.footer[:-1]
self.footersize
= string.count(self.footer,'\n')
def PageBreak(self):
#
# SKIP THE REST OF THE EXISTING PAGE
#
leftofpage = (self.pagelength-(self.headersize+self.footersize+self.padding))-self.line
for row in range(0,leftofpage+4):
if self.line
>= (self.pagelength-self.headersize)-self.footersize:
break
self.Gen('')
self.DrawFooter()
def Print(self, text = ''):
#
# PRINT A LINE OF TEXT
#
if text:
if text[-1]
!= '\n':
text += '\n'
if self.line == 1:
self.DrawHeader()
elif self.line >= self.pagelength-(self.headersize+self.footersize+self.padding):
self.PageBreak()
self.DrawHeader()
self.Gen(text)
def SetHeader(self,header):
self.header = header
self.CheckHeaders()
def SetFooter(self,footer):
self.footer = footer
self.CheckFooters()
def DrawHeader(self):
x = 0
headers = string.split(self.header,'\n')
for h in headers:
if h != '\n':
th = h
#
# CENTER THE HEADER
#
if th == '-':
th = string.ljust(' ',self.pagewidth)
th = string.replace(th,' ','-')
else:
th = string.center(th,self.pagewidth-1)
if x == 0:
if self.paginate == 'TOPRIGHT':
pn = string.rjust(str(self.page),5)
th = th[:-10] + 'PAGE: ' + pn
if self.programid != '':
y = len(self.programid)
+ 1
th = self.programid
+ th[y:]
elif self.paginate == 'TOPLEFT':
pn = string.rjust(str(self.page),5)
th = 'PAGE: ' + pn + th[10:]
if self.programid != '':
y = -(len(self.programid)
+ 1)
th = th[:y] + self.programid
elif x == 1:
if self.regionname != '':
if self.regionpos == 'TOPLEFT':
y = len(self.regionname)
+ 1
th = self.regionname
+ th[y:]
elif self.regionpos == 'TOPRIGHT':
y = len(self.regionname)
+ 1
th = th[y:] + self.regionname
if self.branchname != '':
if self.branchpos == 'TOPLEFT':
y = len(self.branchname)
+ 1
th = self.branchname
+ th[y:]
elif self.branchpos == 'TOPRIGHT':
y = len(self.branchname)
+ 1
th = th[y:] + self.branchname
self.Gen(th)
x += 1
def DrawFooter(self):
x = 0
footers = string.split(self.footer,'\n')
for f in footers:
if f != '\n':
#
# CENTER THE FOOTER
#
th = string.center(f,self.pagewidth)
if x == 0:
if self.paginate == 'BOTTOMRIGHT':
pn = string.rjust(str(self.page),5)
th = th[:-10] + 'PAGE: '
+ pn
elif self.paginate == 'BOTTOMLEFT':
pn = string.rjust(str(self.page),5)
th = 'PAGE: ' + pn + th[10:]
self.Gen(th)
x += 1
self.linecount.append([self.page,self.line])
self.page += 1
self.line = 1
def Gen(self,text):
#
# GENERATE A SINGLE LINE OF OUTPUT
#
if text:
if text[-1] != '\n':
text += '\n'
if self.output == 'PRINT':
sys.stdout.write(text)
self.line += 1
return
Usage:
Let's say you save the above in a module called myPrinter.
Then you would have this in your application code:
import myPrinter
def PrintMyReport(results):
#
# CREATE THE PRINTER OBJECT
#
h = "This is my report"
p = "MYPROG.py"
b = "Calgary"
w = 176
Printer = myPrinter.Printer(header=h,programid=p,branchname=b,pagewidth=w)
Printer.DrawFields([[ 'LN'
, 3 , 'RIGHT'],\
[ 'Product Number'
, 24 ],\
[ 'Q/S'
, 5 , 'RIGHT'],\
[ 'Unit Cost'
, 10 , 'RIGHT'] ])
#
# EXAMPLE ONLY: By this point, your
report results would be generated,
# and placed in the "results" variable below.
#
for each in results:
ln = each[0]
pn = each[1]
qs = each[2]
uc = each[3]
Printer.DrawFields([[ ln ,
3,'RIGHT'],\
[ pn , 24 ],\
[ qs , 5,'RIGHT'],\
[ uc , 10,'RIGHT']])