Commit aa2e2e54 authored by Shane Hathaway's avatar Shane Hathaway

- Corrected issues with DateTime.

- Optimized slightly.

- Clarified by using more variable names.
parent 89de4892
...@@ -83,13 +83,15 @@ ...@@ -83,13 +83,15 @@
# #
############################################################################# #############################################################################
__version__ = '$Id: util.py,v 1.5 2001/06/11 16:04:21 andreas Exp $' __version__ = '$Id: util.py,v 1.6 2001/06/13 14:22:52 shane Exp $'
import re import re
from types import StringType,ListType,TupleType,DictType,InstanceType from types import StringType,ListType,TupleType,DictType,InstanceType
from DateTime import DateTime from DateTime import DateTime
SequenceTypes = (TupleType, ListType)
class parseIndexRequest: class parseIndexRequest:
""" """
This class provides functionality to hide the internals of a request This class provides functionality to hide the internals of a request
...@@ -125,7 +127,7 @@ class parseIndexRequest: ...@@ -125,7 +127,7 @@ class parseIndexRequest:
ParserException = 'IndexRequestParseError' ParserException = 'IndexRequestParseError'
def __init__(self,request,iid,options=[]): def __init__(self, request, iid, options=[]):
""" parse a request from the ZPublisher and return a uniform """ parse a request from the ZPublisher and return a uniform
datastructure back to the _apply_index() method of the index datastructure back to the _apply_index() method of the index
...@@ -135,80 +137,75 @@ class parseIndexRequest: ...@@ -135,80 +137,75 @@ class parseIndexRequest:
""" """
self.id = iid self.id = iid
self.keys = None
if not request.has_key(iid): return if not request.has_key(iid):
self.keys = None
return
# We keep this for backward compatility # We keep this for backward compatility
if request.has_key(iid+'_usage'): usage_param = iid + '_usage'
self.usage = request[iid+'_usage'] if request.has_key(usage_param):
self.usage = request[usage_param]
keys = request[iid]
if type(keys) == InstanceType:
if isinstance(keys,DateTime): param = request[iid]
"""query is a DateTime instance""" keys = None
t = type(param)
self.keys = keys if t is InstanceType and not isinstance(param, DateTime):
else:
""" query is of type record """ """ query is of type record """
record = keys
if hasattr(record,'query'): record = param
if not hasattr(record, 'query'):
raise self.ParserException, (
"record for '%s' *must* contain a "
"'query' attribute" % self.id)
keys = record.query keys = record.query
else:
raise self.ParserException,\
"record for '%s' *must* contain a 'query' attribute" % self.id
if type(keys)== StringType: if type(keys) is StringType:
self.keys = [keys.strip()] keys = [keys.strip()]
elif type(keys) == ListType:
self.keys = keys
for op in options: for op in options:
if op in ["query"]: continue if op == "query": continue
if hasattr(record,op): if hasattr(record, op):
setattr(self,k,getattr(record,k)) setattr(self, op, getattr(record, op))
elif t is DictType:
elif type(keys)==DictType:
""" query is a dictionary containing all parameters """ """ query is a dictionary containing all parameters """
query = keys.get("query",[]) query = param.get("query", ())
if type(query) in [TupleType,ListType]: if type(query) in SequenceTypes:
self.keys = query keys = query
else: else:
self.keys = [ query ] keys = [ query ]
for op in options: for op in options:
if op in ["query"]: continue if op == "query": continue
if keys.has_key(op):
setattr(self,op,keys[op])
if param.has_key(op):
setattr(self, op, param[op])
else: else:
""" query is tuple, list or string """ """ query is tuple, list, string, number, or something else """
if type(keys) in [TupleType,ListType]: if t in SequenceTypes:
self.keys = keys keys = param
else: else:
self.keys = [keys] keys = [param]
for op in options: for op in options:
if request.has_key(iid+"_"+op): field = iid + "_" + op
setattr(self,op,request[iid+"_"+op]) if request.has_key(field):
setattr(self, op, request[field])
if self.keys != None: if keys is not None:
self.keys = filter(lambda x: len(str(x))>0 , self.keys) # Filter out empty strings.
keys = filter(lambda key: key != '', keys)
if len(self.keys)==0: self.keys=None if not keys:
keys = None
self.keys = keys
def get(self,k,default_v=None): def get(self,k,default_v=None):
......
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