Commit 6e03dd44 authored by Jim Fulton's avatar Jim Fulton

initial

parent 15f09953
#!/bin/env python
##############################################################################
#
# Copyright
#
# Copyright 1996 Digital Creations, L.C., 910 Princess Anne
# Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
# rights reserved.
#
##############################################################################
__doc__='''Shared Aqueduct classes and functions
$Id: Aqueduct.py,v 1.1 1997/07/25 16:07:18 jim Exp $'''
__version__='$Revision: 1.1 $'[11:-2]
from Globals import ManageHTMLFile
import DocumentTemplate, DateTime, regex, regsub, string, urllib, rotor
DateTime.now=DateTime.DateTime
dtml_dir="%s/Aqueduct/" % SOFTWARE_HOME
default_report_src=open(dtml_dir+'defaultReport.dtml').read()
class BaseQuery:
def query_year(self): return self.query_date.year()
def query_month(self): return self.query_date.month()
def query_day(self): return self.query_date.day()
query_date=DateTime.now()
def quoted_input(self): return quotedHTML(self.input_src)
def quoted_report(self): return quotedHTML(self.report_src)
MissingArgumentError='Bad Request'
def _argdata(self,REQUEST,raw=0,return_missing_keys=0):
args=self.arguments
argdata={}
name=self.name
missing_keys=[]
for arg in args.keys():
a=arg
l=string.find(arg,':')
if l > 0: arg=arg[:l]
v=REQUEST
try:
try: v=REQUEST[arg]
except (KeyError, AttributeError): pass
if v is REQUEST:
try: v=REQUEST["%s.%s" % (name,arg)]
except (KeyError, AttributeError): pass
if v is REQUEST:
l=string.find(arg,'.')
if l > 0 and raw:
arg=arg[l+1:]
try: v=REQUEST[arg]
except (KeyError, AttributeError): pass
if v is REQUEST:
try: v=REQUEST["%s.%s" % (name,arg)]
except (KeyError, AttributeError): pass
except:
# Hm, we got another error, must have been an invalid
# input.
raise 'Bad Request', (
'The value entered for <em>%s</em> was invalid' % arg)
if v is REQUEST:
v=args[a]
if v is None:
if return_missing_keys:
missing_keys.append(arg)
else:
raise self.MissingArgumentError, (
'''The required value <em>%s</em> was
ommitted''' % arg)
if raw:
argdata[a]=v
else:
argdata[arg]=v
if return_missing_keys and missing_keys:
raise self.MissingArgumentError, missing_keys
return argdata
def _query_string(self,argdata,query_method='query'):
return "%s?%s" % (
query_method,
string.joinfields(
map(lambda k, d=argdata:
"%s=%s" % (k, urllib.quote(str(d[k])))
, argdata.keys())
)
)
def default_input_form(name,arguments,action='query'):
name=nicify(name)
if arguments:
return (
"%s\n%s%s" % (
'<html><head><title>%s Input Data</title></head><body>\n'
'<form action="<!--#var URL2-->/<!--#var name-->/%s" '
'method="post">\n'
'<h2>%s Input Data</h2>\n'
'Enter query parameters:<br>'
'<table>\n'
% (name,action,name),
string.joinfields(
map(
lambda a:
('<tr>\t<td><strong>%s</strong>:</td>\n'
'\t<td><input name="%s" width=30></td></tr>'
% (nicify(detypify(a)),a))
, arguments.keys()
),
'\n'),
'\n<tr><td></td><td>\n'
'<input type="SUBMIT" name="SUBMIT" value="Submit Query">\n'
'<!--#if HTTP_REFERER-->\n'
' <input type="SUBMIT" name="SUBMIT" value="Cancel">\n'
' <INPUT NAME="CANCEL_ACTION" TYPE="HIDDEN"\n'
' VALUE="<!--#var HTTP_REFERER-->">\n'
'<!--#/if HTTP_REFERER-->\n'
'</td></tr>\n</table>\n</form>\n</body>\n</html>\n'
)
)
else:
return (
'<html><head><title>%s Input Data</title></head><body>\n'
'<form action="<!--#var URL2-->/<!--#var name-->/%s" '
'method="post">\n'
'<h2>%s Input Data</h2>\n'
'This query requires no input.<p>\n'
'<input type="SUBMIT" name="SUBMIT" value="Submit Query">\n'
'<!--#if HTTP_REFERER-->\n'
' <input type="SUBMIT" name="SUBMIT" value="Cancel">\n'
' <INPUT NAME="CANCEL_ACTION" TYPE="HIDDEN"\n'
' VALUE="<!--#var HTTP_REFERER-->">\n'
'<!--#/if HTTP_REFERER-->\n'
'</td></tr>\n</table>\n</form>\n</body>\n</html>\n'
% (name, action, name)
)
custom_default_report_src=DocumentTemplate.File(
dtml_dir+'customDefaultReport.dtml')
def custom_default_report(result, action=''):
names=result.names()
heading=('<tr>\n%s</tr>' %
string.joinfields(
map(lambda name:
'\t<th>%s</th>\n' % nicify(name),
names),
''
)
)
row=('<tr>\n%s</tr>' %
string.joinfields(
map(lambda name, meta:
'\t\t<td><!--#var %s%s--></td>\n'
% (urllib.quote(name),
meta['type']!='s' and ' null=""' or '',
),
names, result.__items__),
''
)
)
return custom_default_report_src(heading=heading,row=row,action=action)
def detypify(arg):
l=string.find(arg,':')
if l > 0: arg=arg[:l]
return arg
def decode(input,output):
while 1:
line = input.readline()
if not line: break
s = binascii.a2b_base64(line[:-1])
output.write(s)
def decodestring(s):
f = StringIO(s)
g = StringIO()
decode(f, g)
return g.getvalue()
def parse(text,
prefix=None,
result=None,
unparmre=regex.compile(
'\([\0- ]*\([^\0- =\"]+\)\)'),
parmre=regex.compile(
'\([\0- ]*\([^\0- =\"]+\)=\([^\0- =\"]+\)\)'),
qparmre=regex.compile(
'\([\0- ]*\([^\0- =\"]+\)="\([^"]+\)\"\)'),
):
if result is None: result = {}
__traceback_info__=text
if parmre.match(text) >= 0:
name=parmre.group(2)
value=parmre.group(3)
l=len(parmre.group(1))
elif qparmre.match(text) >= 0:
name=qparmre.group(2)
value=qparmre.group(3)
l=len(qparmre.group(1))
elif unparmre.match(text) >= 0:
name=unparmre.group(2)
l=len(unparmre.group(1))
if prefix: name="%s.%s" % (prefix,name)
result[name]=None
return parse(text[l:],prefix,result)
else:
if not text or not strip(text): return result
raise InvalidParameter, text
if prefix: name="%s.%s" % (prefix,name)
result[name]=value
return parse(text[l:],prefix,result)
def quotedHTML(text,
character_entities=(
(regex.compile('&'), '&amp;'),
(regex.compile("<"), '&lt;' ),
(regex.compile(">"), '&gt;' ),
(regex.compile('"'), '&quot;'))): #"
import regsub
for re,name in character_entities:
text=regsub.gsub(re,name,text)
return text
def nicify(name, under=regex.compile('_')):
name=regsub.gsub(under,' ',string.strip(name))
return string.upper(name[:1])+name[1:]
class Rotor:
def __init__(self, key, numrotors=6):
self.a=key, numrotors
r=rotor.newrotor(key, numrotors)
self.encrypt=r.encrypt
self.decrypt=r.decrypt
self.decryptmore=r.decryptmore
def __getinitargs__(self): return self.a
def __getstate__(self,v={}): return v
def decapitate(html, RESPONSE=None,
header_re=regex.compile(
'\(\('
'[^\0- <>:]+:[^\n]*\n'
'\|'
'[ \t]+[^\0- ][^\n]*\n'
'\)+\)[ \t]*\n\([\0-\377]+\)'
),
space_re=regex.compile('\([ \t]+\)'),
name_re=regex.compile('\([^\0- <>:]+\):\([^\n]*\)'),
):
if header_re.match(html) < 0: return html
headers, html = header_re.group(1,3)
headers=string.split(headers,'\n')
i=1
while i < len(headers):
if not headers[i]:
del headers[i]
elif space_re.match(headers[i]) >= 0:
headers[i-1]="%s %s" % (headers[i-1],
headers[i][len(space_re.group(1)):])
del headers[i]
else:
i=i+1
for i in range(len(headers)):
if name_re.match(headers[i]) >= 0:
k, v = name_re.group(1,2)
v=string.strip(v)
else:
raise ValueError, 'Invalid Header (%d): %s ' % (i,headers[i])
RESPONSE.setHeader(k,v)
return html
def delimited_output(results,REQUEST,RESPONSE):
delim=REQUEST['output-delimiter']
try: output_type=REQUEST['output-type']
except: output_type='text/plain'
RESPONSE.setHeader('content-type', output_type)
join=string.join
return "%s\n%s\n" % (
join(results.names(),delim),
join(map(lambda row, delim=delim, join=join:
join(map(str,row),delim),
results),
'\n')
)
##############################################################################
# Test functions:
#
def main():
# The "main" program for this module
import sys
print sys.argv[0]+" is a pure module and doesn't do anything by itself."
if __name__ == "__main__": main()
##############################################################################
#
# $Log: Aqueduct.py,v $
# Revision 1.1 1997/07/25 16:07:18 jim
# initial
#
#
#!/bin/env python
##############################################################################
#
# Copyright
#
# Copyright 1997 Digital Creations, L.C., 910 Princess Anne
# Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
# rights reserved.
#
##############################################################################
__doc__='''Simple RDB data-file reader
$Id: RDB.py,v 1.1 1997/07/25 16:07:19 jim Exp $'''
__version__='$Revision: 1.1 $'[11:-2]
import regex, regsub
from string import split, strip, lower, atof, atoi, atol
Parsers={'n': atof,
'i': atoi,
'l': atol,
}
try:
import DateTime
Parsers['d']=DateTime
except: pass
class Record:
def __init__(self, data, schema, names):
'''Create a record with data'''
self._r_data=data
self._r_schema=schema
self._r_names=names
def __len__(self):
'''Return the record length'''
return len(self._r_data)
def __getattr__(self,name):
'''Get the value of the column with name, \'name\' '''
return self._r_data[self._r_schema[name]]
def __getitem__(self,key):
'''Get a value by key
The key may be either an integer column index or a string
column name.
'''
data=self._r_data
if type(key) is type(0): return data[key]
else: return self._r_data[self._r_schema[key]]
def update(self, *args, **name_value_pairs):
'''Update a record
The update method supports a number of syntaxes:
Multiple positional arguments --
In this case, the number of positional arguments must equal
the number of columns in the record.
A single positional argument with optional keyword parameters --
The single argument may be a list, a tuple, or a mapping
object.
If the argument is a list or a tuple, its length must be
equal to the length of the record. The items in the record
are replaced by the values in the argument.
If the argument is a mapping object, it must have a 'keys'
method that returns a sequence of keys. For those keys that
are record item names, the corresponding values are used to
update the record items. The argument may have keys that
are not item names, and it need not have keys for all item
names.
If keyword parameters are provided, they will be used to
update items in the record. All keyword names must match
item names, although keyword arguments need not be provided
for all items.
Keyword arguments with no positional arguments--
The keyword arguments are used to update items in the
record. All keyword names must match item names, although
keyword arguments need not be provided for all items.
For example, if a record, 'r' has items 'col1', 'col2', and 'col2',
then the following expressions are equivalent::
r.update('spam',1,2)
r.update({'col1':'spam', 'col3':2, 'col2': 1})
r.update(col2=1, col1='spam', col3=2)
Note that because records are mapping objects, one record can be
used to update another, even if the records have different schema.
'''
data=self._r_data
schema=self._r_schema
l=len(data)
la=len(args)
if la > 1:
if la != l: raise TypeError, (
'number of arguments does not match record length')
data[:]=list(args)
elif la==1:
args=args[0]
if type(args) is type(()) or type(args) is type([]):
if len(args) != l: raise TypeError, (
'number of arguments does not match record length')
data[:]=list(args)
else:
for k in args.keys():
try: data[schema[k]]=args[k]
except KeyError: pass
for k in name_value_pairs.keys():
data[schema[k]]=name_value_pairs[k]
def keys(self):
'''Return the item names in item order'''
# In this implementation, our class has this information handy
return self._r_item_names
def values(self):
'''Return our data values, in item order'''
return self._r_data[:]
def items(self):
'''Return a sequence of item name and item pairs
The sequence is returned in item order.'''
return map(None, self._r_names, self._r_data)
def has_key(self,key):
'''Return whether the given key is an item name'''
return self._schema.has_key(key)
class RDB:
"""Class for reading RDB files
"""
def __init__(self,file,RecordClass=Record):
self._file=file
line=file.readline()
self._record_positions=[]
self._Record=RecordClass
comment_pattern=regex.compile('#')
while line and comment_pattern.match(line) >= 0: line=file.readline()
line=line[:-1]
if line[-1:] in '\r\n': line=line[:-1]
self._names=names=split(line,'\t')
if not names: raise ValueError, 'No column names'
self._schema=schema={}
i=0
for name in names:
name=strip(name)
if not name:
raise ValueError, 'Empty column name, %s' % name
if schema.has_key(name):
raise ValueError, 'Duplicate column name, %s' % name
schema[name]=i
i=i+1
self._nv=nv=len(names)
line=file.readline()
line=line[:-1]
if line[-1:] in '\r\n': line=line[:-1]
self._defs=defs=split(line,'\t')
if not defs: raise ValueError, 'No column definitions'
if len(defs) != nv:
raise ValueError, (
"""The number of column names and the number of column
definitions are different.""")
i=0
self._parsers=parsers=[]
self.__items__=__items__=[]
defre=regex.compile('\([0-9]*\)\([a-zA-Z]\)?[^\0- ]*[\0- ]*\(.+\)')
for _def in defs:
_def=strip(_def)
if not _def:
raise ValueError, ('Empty column definition for %s' % names[i])
if defre.match(_def) < 0:
raise ValueError, (
'Invalid column definition for, %s, for %s'
% _def, names[i])
width, type, remark = defre.group(1,2,3)
try: width=atoi(width)
except: width=8
type=lower(type)
try: parsers.append(i,Parsers[type])
except: pass
__items__.append({'type':type, 'width': width, 'name': names[i],
'remark':remark})
i=i+1
self._index=-1
self._pos=file.tell()
def names(self): return self._names
def parse(self, line):
line=line[:-1]
while line[-1:] in '\r\n': line=line[:-1]
fields=split(line,'\t')
if len(fields) != self._nv: raise ValueError, (
"""The number of items in record %s is invalid
<pre>%s\n%s\n%s\n%s</pre>
"""
% (_index, ('='*40), line, ('='*40), fields))
for i, parser in self._parsers:
try: v=parser(fields[i])
except:
if fields[i]:
raise ValueError, (
"""Invalid value, %s, for %s in record %s"""
% (fields[i], self._names[i], _index))
else: v=None
fields[i]=v
return fields
def __len__(self):
_index=self._index
try: pos=self._pos
except: return _index+1
file=self._file
readline=file.readline
tell=file.tell
if pos != tell(): file.seek(pos)
save_position=self._record_positions.append
while 1:
line=readline()
if not line:
del self._pos
self._index=_index
return _index+1
_index=_index+1
save_position(pos)
pos=tell()
def __getitem__(self,input_index):
index=input_index
if index < 0:
index=len(self)-index
if index < 0: raise IndexError, input_index
_index=self._index
if index==_index:
try: return self._row
except: pass
if index > _index:
try: pos=self._pos
except: raise IndexError, input_index
file=self._file
readline=file.readline
tell=file.tell
if pos != tell(): file.seek(pos)
save_position=self._record_positions.append
while index > _index:
line=readline()
if not line:
del self._pos
self._index=_index
raise IndexError, input_index
_index=_index+1
save_position(pos)
pos=tell()
if index == _index:
self._pos=pos
self._index=_index
self._row=line=self._Record(self.parse(line),
self._schema, self._names)
return line
else:
file=self._file
file.seek(self._record_positions[index])
return self._Record(self.parse(file.readline()),
self._schema, self._names)
raise IndexError, input_index
class rdb:
def __init__(self, file):
self._file=file
##############################################################################
# Test functions:
#
def main():
# The "main" program for this module
import sys
print sys.argv[0]+" is a pure module and doesn't do anything by itself."
if __name__ == "__main__": main()
##############################################################################
#
# $Log: RDB.py,v $
# Revision 1.1 1997/07/25 16:07:19 jim
# initial
#
#
<html> <head>
<title><!--#var name capitalize spacify--> Results</title>
</head>
<body>
<h2><!--#var name capitalize spacify--> Results</h2>
<em>As of <!--#var query_date-->
</em><br><br>
<!--#if query_results-->
<p>Results for this
<a href="<!--#var URL2-->/<!--#var name-->%(action)s">
<!--#var name capitalize spacify--> query</a>:</p>
<!--#in query_results size=50 start=query_start-->
<!--#if sequence-start-->
<!--#if previous-sequence-->
<a href="<!--#var
PARENT_URL-->/<!--#var
query_string-->&query_start=<!--#var
previous-sequence-start-number-->">
(Previous <!--#var previous-sequence-size--> results)
</a>
<!--#/if previous-sequence-->
<table border>
%(heading)s
<!--#/if sequence-start-->
%(row)s
<!--#if sequence-end-->
</table>
<!--#if next-sequence-->
<a href="<!--#var
PARENT_URL-->/<!--#var
query_string-->&query_start=<!--#var
next-sequence-start-number-->">
(Next <!--#var next-sequence-size--> results)
</a>
<!--#/if next-sequence-->
<!--#/if sequence-end-->
<!--#/in query_results-->
<!--#else query_results-->
There was no data matching this.
<a href="<!--#var URL2-->/<!--#var name-->%(action)s">
<!--#var name capitalize spacify--> query</a>.
<!--#/if query_results-->
</body> </html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment