Skip to content

Commit

Permalink
more pep8, modernize exception raising and avoid bare except statements
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Apr 8, 2012
1 parent f4eed14 commit 2f73a4a
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/Products/PluginIndexes/BooleanIndex/BooleanIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ def unindex_object(self, documentId):
self._length.change(-1)
except ConflictError:
raise
except:
except Exception:
LOG.debug('Attempt to unindex nonexistent document'
' with id %s' % documentId,exc_info=True)
' with id %s' % documentId, exc_info=True)

def _apply_index(self, request, resultset=None):
record = parseIndexRequest(request, self.id, self.query_options)
Expand Down
2 changes: 1 addition & 1 deletion src/Products/PluginIndexes/DateIndex/DateIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def index_object(self, documentId, obj, threshold=None):
del self._unindex[documentId]
except ConflictError:
raise
except:
except Exception:
LOG.error("Should not happen: ConvertedDate was there,"
" now it's not, for document with id %s" %
documentId)
Expand Down
10 changes: 4 additions & 6 deletions src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Date range index.
"""

import os
from datetime import datetime
Expand Down Expand Up @@ -157,7 +155,7 @@ def clear(self):
self._until_only = IOBTree()
self._since = IOBTree()
self._until = IOBTree()
self._unindex = IOBTree() # 'datum' will be a tuple of date ints
self._unindex = IOBTree() # 'datum' will be a tuple of date ints
self._length = Length()

#
Expand Down Expand Up @@ -196,7 +194,7 @@ def index_object(self, documentId, obj, threshold=None):
datum = (since, until)

old_datum = self._unindex.get(documentId, None)
if datum == old_datum: # No change? bail out!
if datum == old_datum: # No change? bail out!
return 0

if old_datum is not None:
Expand Down Expand Up @@ -396,9 +394,9 @@ def _convertDateTime(self, value):
return value
if isinstance(value, (str, datetime)):
dt_obj = DateTime(value)
value = dt_obj.millis() / 1000 / 60 # flatten to minutes
value = dt_obj.millis() / 1000 / 60 # flatten to minutes
elif isinstance(value, DateTime):
value = value.millis() / 1000 / 60 # flatten to minutes
value = value.millis() / 1000 / 60 # flatten to minutes
if value > MAX32 or value < -MAX32:
# t_val must be integer fitting in the 32bit range
raise OverflowError('%s is not within the range of dates allowed'
Expand Down
9 changes: 3 additions & 6 deletions src/Products/PluginIndexes/FieldIndex/FieldIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Simple column indices.
"""

from App.special_dtml import DTMLFile

from Products.PluginIndexes.common.UnIndex import UnIndex


class FieldIndex(UnIndex):

"""Index for simple fields.
"""
meta_type="FieldIndex"
meta_type = "FieldIndex"

manage_options= (
manage_options = (
{'label': 'Settings', 'action': 'manage_main'},
{'label': 'Browse', 'action': 'manage_browse'},
)
Expand All @@ -35,9 +32,9 @@ class FieldIndex(UnIndex):
manage_main._setName('manage_main')
manage_browse = DTMLFile('../dtml/browseIndex', globals())


manage_addFieldIndexForm = DTMLFile('dtml/addFieldIndex', globals())


def manage_addFieldIndex(self, id, extra=None,
REQUEST=None, RESPONSE=None, URL3=None):
"""Add a field index"""
Expand Down
28 changes: 13 additions & 15 deletions src/Products/PluginIndexes/TopicIndex/FilteredSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Filtered set.
"""

from logging import getLogger
import sys
Expand All @@ -33,19 +31,21 @@ class FilteredSetBase(Persistent):
implements(IFilteredSet)

def __init__(self, id, expr):
self.id = id
self.id = id
self.expr = expr
self.clear()

def clear(self):
self.ids = IITreeSet()
self.ids = IITreeSet()

def index_object(self, documentId, obj):
raise RuntimeError,'index_object not defined'
raise RuntimeError('index_object not defined')

def unindex_object(self,documentId):
try: self.ids.remove(documentId)
except KeyError: pass
def unindex_object(self, documentId):
try:
self.ids.remove(documentId)
except KeyError:
pass

def getId(self):
return self.id
Expand All @@ -66,7 +66,7 @@ def setExpression(self, expr):
self.expr = expr

def __repr__(self):
return '%s: (%s) %s' % (self.id,self.expr,map(None,self.ids))
return '%s: (%s) %s' % (self.id, self.expr, map(None, self.ids))

__str__ = __repr__

Expand All @@ -86,16 +86,14 @@ def index_object(self, documentId, o):
pass
except ConflictError:
raise
except:
except Exception:
LOG.warn('eval() failed Object: %s, expr: %s' %\
(o.getId(),self.expr), exc_info=sys.exc_info())
(o.getId(), self.expr), exc_info=sys.exc_info())


def factory(f_id, f_type, expr):
""" factory function for FilteredSets """

if f_type=='PythonFilteredSet':
if f_type == 'PythonFilteredSet':
return PythonFilteredSet(f_id, expr)

else:
raise TypeError,'unknown type for FilteredSets: %s' % f_type
raise TypeError('unknown type for FilteredSets: %s' % f_type)
81 changes: 41 additions & 40 deletions src/Products/PluginIndexes/TopicIndex/TopicIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Topic index.
"""

from logging import getLogger

Expand Down Expand Up @@ -43,17 +41,17 @@ class TopicIndex(Persistent, SimpleItem):
"""
implements(ITopicIndex, IPluggableIndex)

meta_type="TopicIndex"
meta_type = "TopicIndex"
query_options = ('query', 'operator')

manage_options= (
manage_options = (
{'label': 'FilteredSets', 'action': 'manage_main'},
)

def __init__(self,id,caller=None):
def __init__(self, id, caller=None):
self.id = id
self.filteredSets = OOBTree()
self.operators = ('or','and')
self.filteredSets = OOBTree()
self.operators = ('or', 'and')
self.defaultOperator = 'or'

def getId(self):
Expand All @@ -63,15 +61,14 @@ def clear(self):
for fs in self.filteredSets.values():
fs.clear()

def index_object(self, docid, obj ,threshold=100):
def index_object(self, docid, obj, threshold=100):
""" hook for (Z)Catalog """
for fid, filteredSet in self.filteredSets.items():
filteredSet.index_object(docid,obj)
filteredSet.index_object(docid, obj)
return 1

def unindex_object(self,docid):
def unindex_object(self, docid):
""" hook for (Z)Catalog """

for fs in self.filteredSets.values():
try:
fs.unindex_object(docid)
Expand All @@ -88,7 +85,7 @@ def indexSize(self):
"""Return the size of the index in terms of distinct values."""
return "n/a"

def search(self,filter_id):
def search(self, filter_id):
if self.filteredSets.has_key(filter_id):
return self.filteredSets[filter_id].getIds()

Expand All @@ -101,24 +98,26 @@ def _apply_index(self, request):
return None

operator = record.get('operator', self.defaultOperator).lower()
if operator == 'or': set_func = union
else: set_func = intersection
if operator == 'or':
set_func = union
else:
set_func = intersection

res = None
for filter_id in record.keys:
rows = self.search(filter_id)
res = set_func(res,rows)
res = set_func(res, rows)

if res:
return res, (self.id,)
else:
return IITreeSet(), (self.id,)

def uniqueValues(self,name=None, withLength=0):
def uniqueValues(self, name=None, withLength=0):
""" needed to be consistent with the interface """
return self.filteredSets.keys()

def getEntryForObject(self,docid, default=_marker):
def getEntryForObject(self, docid, default=_marker):
""" Takes a document ID and returns all the information we have
on that specific object.
"""
Expand All @@ -127,8 +126,8 @@ def getEntryForObject(self,docid, default=_marker):
def addFilteredSet(self, filter_id, typeFilteredSet, expr):
# Add a FilteredSet object.
if self.filteredSets.has_key(filter_id):
raise KeyError,\
'A FilteredSet with this name already exists: %s' % filter_id
raise KeyError(
'A FilteredSet with this name already exists: %s' % filter_id)
self.filteredSets[filter_id] = factory(filter_id,
typeFilteredSet,
expr,
Expand All @@ -137,73 +136,75 @@ def addFilteredSet(self, filter_id, typeFilteredSet, expr):
def delFilteredSet(self, filter_id):
# Delete the FilteredSet object specified by 'filter_id'.
if not self.filteredSets.has_key(filter_id):
raise KeyError,\
'no such FilteredSet: %s' % filter_id
raise KeyError(
'no such FilteredSet: %s' % filter_id)
del self.filteredSets[filter_id]

def clearFilteredSet(self, filter_id):
# Clear the FilteredSet object specified by 'filter_id'.
if not self.filteredSets.has_key(filter_id):
raise KeyError,\
'no such FilteredSet: %s' % filter_id
raise KeyError(
'no such FilteredSet: %s' % filter_id)
self.filteredSets[filter_id].clear()

def manage_addFilteredSet(self, filter_id, typeFilteredSet, expr, URL1, \
REQUEST=None,RESPONSE=None):
REQUEST=None, RESPONSE=None):
""" add a new filtered set """

if len(filter_id) == 0: raise RuntimeError,'Length of ID too short'
if len(expr) == 0: raise RuntimeError,'Length of expression too short'
if len(filter_id) == 0:
raise RuntimeError('Length of ID too short')
if len(expr) == 0:
raise RuntimeError('Length of expression too short')

self.addFilteredSet(filter_id, typeFilteredSet, expr)

if RESPONSE:
RESPONSE.redirect(URL1+'/manage_workspace?'
RESPONSE.redirect(URL1 + '/manage_workspace?'
'manage_tabs_message=FilteredSet%20added')

def manage_delFilteredSet(self, filter_ids=[], URL1=None, \
REQUEST=None,RESPONSE=None):
def manage_delFilteredSet(self, filter_ids=[], URL1=None,
REQUEST=None, RESPONSE=None):
""" delete a list of FilteredSets"""

for filter_id in filter_ids:
self.delFilteredSet(filter_id)

if RESPONSE:
RESPONSE.redirect(URL1+'/manage_workspace?'
RESPONSE.redirect(URL1 + '/manage_workspace?'
'manage_tabs_message=FilteredSet(s)%20deleted')

def manage_saveFilteredSet(self,filter_id, expr, URL1=None,\
REQUEST=None,RESPONSE=None):
def manage_saveFilteredSet(self, filter_id, expr, URL1=None,
REQUEST=None, RESPONSE=None):
""" save expression for a FilteredSet """

self.filteredSets[filter_id].setExpression(expr)

if RESPONSE:
RESPONSE.redirect(URL1+'/manage_workspace?'
RESPONSE.redirect(URL1 + '/manage_workspace?'
'manage_tabs_message=FilteredSet(s)%20updated')

def getIndexSourceNames(self):
""" return names of indexed attributes """
return ('n/a',)
return ('n/a', )

def manage_clearFilteredSet(self, filter_ids=[], URL1=None, \
REQUEST=None,RESPONSE=None):
def manage_clearFilteredSet(self, filter_ids=[], URL1=None,
REQUEST=None, RESPONSE=None):
""" clear a list of FilteredSets"""

for filter_id in filter_ids:
self.clearFilteredSet(filter_id)

if RESPONSE:
RESPONSE.redirect(URL1+'/manage_workspace?'
RESPONSE.redirect(URL1 + '/manage_workspace?'
'manage_tabs_message=FilteredSet(s)%20cleared')

manage = manage_main = DTMLFile('dtml/manageTopicIndex',globals())
manage = manage_main = DTMLFile('dtml/manageTopicIndex', globals())
manage_main._setName('manage_main')
editFilteredSet = DTMLFile('dtml/editFilteredSet',globals())

editFilteredSet = DTMLFile('dtml/editFilteredSet', globals())

manage_addTopicIndexForm = DTMLFile('dtml/addTopicIndex', globals())


def manage_addTopicIndex(self, id, REQUEST=None, RESPONSE=None, URL3=None):
"""Add a TopicIndex"""
return self.manage_addIndex(id, 'TopicIndex', extra=None, \
Expand Down
5 changes: 2 additions & 3 deletions src/Products/PluginIndexes/UUIDIndex/UUIDIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class UUIDIndex(UnIndex):
For each datum only one documentId can exist.
"""

meta_type= "UUIDIndex"
meta_type = "UUIDIndex"

manage_options= (
manage_options = (
{'label': 'Settings', 'action': 'manage_main'},
{'label': 'Browse', 'action': 'manage_browse'},
)
Expand Down Expand Up @@ -108,7 +108,6 @@ def _get_object_datum(self, obj, attr):
return _marker
return super(UUIDIndex, self)._get_object_datum(obj, attr)


manage_addUUIDIndexForm = DTMLFile('dtml/addUUIDIndex', globals())


Expand Down
Loading

0 comments on commit 2f73a4a

Please sign in to comment.