[Zope-Checkins] CVS: Zope2 - util.py:1.6
shane@digicool.com
shane@digicool.com
Wed, 13 Jun 2001 10:22:59 -0400 (EDT)
Update of /cvs-repository/Zope2/lib/python/Products/PluginIndexes/common
In directory korak.digicool.com:/tmp/cvs-serv7819
Modified Files:
util.py
Log Message:
- Corrected issues with DateTime.
- Optimized slightly.
- Clarified by using more variable names.
--- Updated File util.py in package Zope2 --
--- util.py 2001/06/11 16:04:21 1.5
+++ util.py 2001/06/13 14:22:52 1.6
@@ -90,6 +90,8 @@
from types import StringType,ListType,TupleType,DictType,InstanceType
from DateTime import DateTime
+SequenceTypes = (TupleType, ListType)
+
class parseIndexRequest:
"""
This class provides functionality to hide the internals of a request
@@ -125,7 +127,7 @@
ParserException = 'IndexRequestParseError'
- def __init__(self,request,iid,options=[]):
+ def __init__(self, request, iid, options=[]):
""" parse a request from the ZPublisher and return a uniform
datastructure back to the _apply_index() method of the index
@@ -134,83 +136,78 @@
options -- a list of options the index is interested in
"""
- self.id = iid
- self.keys = None
+ self.id = iid
- if not request.has_key(iid): return
+ if not request.has_key(iid):
+ self.keys = None
+ return
# We keep this for backward compatility
- if request.has_key(iid+'_usage'):
- self.usage = request[iid+'_usage']
-
- keys = request[iid]
-
- if type(keys) == InstanceType:
-
- if isinstance(keys,DateTime):
- """query is a DateTime instance"""
+ usage_param = iid + '_usage'
+ if request.has_key(usage_param):
+ self.usage = request[usage_param]
+
+ param = request[iid]
+ keys = None
+ t = type(param)
+
+ if t is InstanceType and not isinstance(param, DateTime):
+ """ query is of type record """
+
+ record = param
+
+ if not hasattr(record, 'query'):
+ raise self.ParserException, (
+ "record for '%s' *must* contain a "
+ "'query' attribute" % self.id)
+ keys = record.query
- self.keys = keys
-
-
- else:
- """ query is of type record """
- record = keys
+ if type(keys) is StringType:
+ keys = [keys.strip()]
- if hasattr(record,'query'):
- keys = record.query
- else:
- raise self.ParserException,\
- "record for '%s' *must* contain a 'query' attribute" % self.id
-
- if type(keys)== StringType:
- self.keys = [keys.strip()]
- elif type(keys) == ListType:
- self.keys = keys
-
- for op in options:
- if op in ["query"]: continue
-
- if hasattr(record,op):
- setattr(self,k,getattr(record,k))
+ for op in options:
+ if op == "query": continue
+ if hasattr(record, op):
+ setattr(self, op, getattr(record, op))
- elif type(keys)==DictType:
+ elif t is DictType:
""" query is a dictionary containing all parameters """
- query = keys.get("query",[])
- if type(query) in [TupleType,ListType]:
- self.keys = query
+ query = param.get("query", ())
+ if type(query) in SequenceTypes:
+ keys = query
else:
- self.keys = [ query ]
+ keys = [ query ]
- for op in options:
- if op in ["query"]: continue
+ for op in options:
+ 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:
- """ query is tuple, list or string """
+ """ query is tuple, list, string, number, or something else """
- if type(keys) in [TupleType,ListType]:
- self.keys = keys
+ if t in SequenceTypes:
+ keys = param
else:
- self.keys = [keys]
+ keys = [param]
for op in options:
- if request.has_key(iid+"_"+op):
- setattr(self,op,request[iid+"_"+op])
-
+ field = iid + "_" + op
+ if request.has_key(field):
+ setattr(self, op, request[field])
+
+ if keys is not None:
+ # Filter out empty strings.
+ keys = filter(lambda key: key != '', keys)
+ if not keys:
+ keys = None
- if self.keys != None:
- self.keys = filter(lambda x: len(str(x))>0 , self.keys)
+ self.keys = keys
- if len(self.keys)==0: self.keys=None
-
-
def get(self,k,default_v=None):
if hasattr(self,k):