Commit 74261752 authored by Hanno Schlichting's avatar Hanno Schlichting

Final PEP8 fixes

parent f7bdb451
...@@ -54,6 +54,7 @@ except ImportError: ...@@ -54,6 +54,7 @@ except ImportError:
class CatalogError(Exception): class CatalogError(Exception):
pass pass
class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
""" An Object Catalog """ An Object Catalog
...@@ -80,7 +81,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -80,7 +81,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
# convenient display on result pages. meta_data attributes # convenient display on result pages. meta_data attributes
# are turned into brain objects and returned by # are turned into brain objects and returned by
# searchResults. The indexing machinery indexes all records # searchResults. The indexing machinery indexes all records
# by an integer id (rid). self.data is a mapping from the # by an integer id (rid). self.data is a mapping from the
# integer id to the meta_data, self.uids is a mapping of the # integer id to the meta_data, self.uids is a mapping of the
# object unique identifier to the rid, and self.paths is a # object unique identifier to the rid, and self.paths is a
# mapping of the rid to the unique identifier. # mapping of the rid to the unique identifier.
...@@ -106,8 +107,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -106,8 +107,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
def clear(self): def clear(self):
""" clear catalog """ """ clear catalog """
self.data = IOBTree() # mapping of rid to meta_data self.data = IOBTree() # mapping of rid to meta_data
self.uids = OIBTree() # mapping of uid to rid self.uids = OIBTree() # mapping of uid to rid
self.paths = IOBTree() # mapping of rid to uid self.paths = IOBTree() # mapping of rid to uid
self._length = BTrees.Length.Length() self._length = BTrees.Length.Length()
...@@ -171,25 +172,24 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -171,25 +172,24 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
schema = self.schema schema = self.schema
names = list(self.names) names = list(self.names)
if schema.has_key(name): if name in schema:
raise CatalogError, 'The column %s already exists' % name raise CatalogError('The column %s already exists' % name)
if name[0] == '_': if name[0] == '_':
raise CatalogError, \ raise CatalogError('Cannot cache fields beginning with "_"')
'Cannot cache fields beginning with "_"'
if not schema.has_key(name): values = schema.values()
if schema.values(): if values:
schema[name] = max(schema.values())+1 schema[name] = max(values) + 1
else: else:
schema[name] = 0 schema[name] = 0
names.append(name) names.append(name)
if default_value is None or default_value == '': if default_value in (None, ''):
default_value = MV default_value = MV
for key in self.data.keys(): for key, value in self.data.items():
rec = list(self.data[key]) rec = list(value)
rec.append(default_value) rec.append(default_value)
self.data[key] = tuple(rec) self.data[key] = tuple(rec)
...@@ -208,14 +208,16 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -208,14 +208,16 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
names = list(self.names) names = list(self.names)
_index = names.index(name) _index = names.index(name)
if not self.schema.has_key(name): if not name in self.schema:
LOG.error('delColumn attempted to delete nonexistent column %s.' % str(name)) LOG.error('delColumn attempted to delete nonexistent '
'column %s.' % str(name))
return return
del names[_index] del names[_index]
# rebuild the schema # rebuild the schema
i=0; schema = {} i = 0
schema = {}
for name in names: for name in names:
schema[name] = i schema[name] = i
i = i + 1 i = i + 1
...@@ -227,8 +229,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -227,8 +229,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
self.updateBrains() self.updateBrains()
# remove the column value from each record # remove the column value from each record
for key in self.data.keys(): for key, value in self.data.items():
rec = list(self.data[key]) rec = list(value)
del rec[_index] del rec[_index]
self.data[key] = tuple(rec) self.data[key] = tuple(rec)
...@@ -236,37 +238,36 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -236,37 +238,36 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
"""Create a new index, given a name and a index_type. """Create a new index, given a name and a index_type.
Old format: index_type was a string, 'FieldIndex' 'TextIndex' or Old format: index_type was a string, 'FieldIndex' 'TextIndex' or
'KeywordIndex' is no longer valid; the actual index must be instantiated 'KeywordIndex' is no longer valid; the actual index must be
and passed in to addIndex. instantiated and passed in to addIndex.
New format: index_type is the actual index object to be stored. New format: index_type is the actual index object to be stored.
""" """
if self.indexes.has_key(name): if name in self.indexes:
raise CatalogError, 'The index %s already exists' % name raise CatalogError('The index %s already exists' % name)
if name.startswith('_'): if name.startswith('_'):
raise CatalogError, 'Cannot index fields beginning with "_"' raise CatalogError('Cannot index fields beginning with "_"')
if not name: if not name:
raise CatalogError, 'Name of index is empty' raise CatalogError('Name of index is empty')
indexes = self.indexes indexes = self.indexes
if isinstance(index_type, str): if isinstance(index_type, str):
raise TypeError,"""Catalog addIndex now requires the index type to raise TypeError("Catalog addIndex now requires the index type to"
be resolved prior to adding; create the proper index in the caller.""" "be resolved prior to adding; create the proper "
"index in the caller.")
indexes[name] = index_type;
indexes[name] = index_type
self.indexes = indexes self.indexes = indexes
def delIndex(self, name): def delIndex(self, name):
""" deletes an index """ """ deletes an index """
if not self.indexes.has_key(name): if not name in self.indexes:
raise CatalogError, 'The index %s does not exist' % name raise CatalogError('The index %s does not exist' % name)
indexes = self.indexes indexes = self.indexes
del indexes[name] del indexes[name]
...@@ -354,11 +355,12 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -354,11 +355,12 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
self.updateMetadata(object, uid) self.updateMetadata(object, uid)
# do indexing # do indexing
total = 0 total = 0
if idxs==[]: use_indexes = self.indexes.keys() if idxs == []:
else: use_indexes = idxs use_indexes = self.indexes.keys()
else:
use_indexes = idxs
for name in use_indexes: for name in use_indexes:
x = self.getIndex(name) x = self.getIndex(name)
...@@ -366,7 +368,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -366,7 +368,8 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
blah = x.index_object(index, object, threshold) blah = x.index_object(index, object, threshold)
total = total + blah total = total + blah
else: else:
LOG.error('catalogObject was passed bad index object %s.' % str(x)) LOG.error('catalogObject was passed bad index '
'object %s.' % str(x))
return total return total
...@@ -417,19 +420,19 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -417,19 +420,19 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
def recordify(self, object): def recordify(self, object):
""" turns an object into a record tuple """ """ turns an object into a record tuple """
record = [] record = []
# the unique id is allways the first element # the unique id is always the first element
for x in self.names: for x in self.names:
attr=getattr(object, x, MV) attr = getattr(object, x, MV)
if(attr is not MV and safe_callable(attr)): attr=attr() if (attr is not MV and safe_callable(attr)):
attr = attr()
record.append(attr) record.append(attr)
return tuple(record) return tuple(record)
def instantiate(self, record): def instantiate(self, record):
r=self._v_result_class(record[1]) r = self._v_result_class(record[1])
r.data_record_id_ = record[0] r.data_record_id_ = record[0]
return r.__of__(self) return r.__of__(self)
def getMetadataForRID(self, rid): def getMetadataForRID(self, rid):
record = self.data[rid] record = self.data[rid]
result = {} result = {}
...@@ -559,7 +562,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -559,7 +562,7 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
return LazyMap(self.instantiate, self.data.items(), len(self)) return LazyMap(self.instantiate, self.data.items(), len(self))
else: else:
return self.sortResults( return self.sortResults(
self.data, sort_index, reverse, limit, merge) self.data, sort_index, reverse, limit, merge)
elif rs: elif rs:
# We got some results from the indexes. # We got some results from the indexes.
# Sort and convert to sequences. # Sort and convert to sequences.
...@@ -777,13 +780,13 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base): ...@@ -777,13 +780,13 @@ class Catalog(Persistent, Acquisition.Implicit, ExtensionClass.Base):
# self.indexes is always a dict, so get() w/ 1 arg works # self.indexes is always a dict, so get() w/ 1 arg works
sort_index = self.indexes.get(sort_index_name) sort_index = self.indexes.get(sort_index_name)
if sort_index is None: if sort_index is None:
raise CatalogError, 'Unknown sort_on index (%s)' % sort_index_name raise CatalogError('Unknown sort_on index (%s)' %
sort_index_name)
else: else:
if not hasattr(sort_index, 'documentToKeyMap'): if not hasattr(sort_index, 'documentToKeyMap'):
raise CatalogError( raise CatalogError(
'The index chosen for sort_on (%s) is not capable of ' 'The index chosen for sort_on (%s) is not capable of '
'being used as a sort index.' % sort_index_name 'being used as a sort index.' % sort_index_name)
)
return sort_index return sort_index
else: else:
return None return None
......
...@@ -22,6 +22,7 @@ import warnings ...@@ -22,6 +22,7 @@ import warnings
from Acquisition import aq_base from Acquisition import aq_base
from App.special_dtml import DTMLFile from App.special_dtml import DTMLFile
class CatalogAware: class CatalogAware:
""" This is a Mix-In class to make objects automaticly catalog and """ This is a Mix-In class to make objects automaticly catalog and
uncatalog themselves in Zope, and to provide some other basic uncatalog themselves in Zope, and to provide some other basic
...@@ -58,7 +59,8 @@ class CatalogAware: ...@@ -58,7 +59,8 @@ class CatalogAware:
except Exception: except Exception:
s = 0 s = 0
object.manage_afterAdd(item, container) object.manage_afterAdd(item, container)
if s is None: object._p_deactivate() if s is None:
object._p_deactivate()
def manage_afterClone(self, item): def manage_afterClone(self, item):
self.index_object() self.index_object()
...@@ -68,7 +70,8 @@ class CatalogAware: ...@@ -68,7 +70,8 @@ class CatalogAware:
except Exception: except Exception:
s = 0 s = 0
object.manage_afterClone(item) object.manage_afterClone(item)
if s is None: object._p_deactivate() if s is None:
object._p_deactivate()
def manage_beforeDelete(self, item, container): def manage_beforeDelete(self, item, container):
self.unindex_object() self.unindex_object()
...@@ -78,7 +81,8 @@ class CatalogAware: ...@@ -78,7 +81,8 @@ class CatalogAware:
except Exception: except Exception:
s = 0 s = 0
object.manage_beforeDelete(item, container) object.manage_beforeDelete(item, container)
if s is None: object._p_deactivate() if s is None:
object._p_deactivate()
def creator(self): def creator(self):
"""Return a sequence of user names who have the local """Return a sequence of user names who have the local
...@@ -100,13 +104,13 @@ class CatalogAware: ...@@ -100,13 +104,13 @@ class CatalogAware:
if hasattr(self, 'DestinationURL') and \ if hasattr(self, 'DestinationURL') and \
callable(self.DestinationURL): callable(self.DestinationURL):
url='%s/%s' % (self.DestinationURL(), self.id) url='%s/%s' % (self.DestinationURL(), self.id)
else: url=self.absolute_url() else:
type, uri=ftype(url) url = self.absolute_url()
host, uri=fhost(uri) type, uri = ftype(url)
script_name=self.REQUEST['SCRIPT_NAME'] host, uri = fhost(uri)
__traceback_info__=(`uri`, `script_name`) script_name = self.REQUEST['SCRIPT_NAME']
if script_name: if script_name:
uri=filter(None, uri.split(script_name))[0] uri = filter(None, uri.split(script_name))[0]
if not uri: if not uri:
uri = '/' uri = '/'
if uri[0] != '/': if uri[0] != '/':
...@@ -117,24 +121,27 @@ class CatalogAware: ...@@ -117,24 +121,27 @@ class CatalogAware:
"""Return a summary of the text content of the object.""" """Return a summary of the text content of the object."""
if not hasattr(self, 'text_content'): if not hasattr(self, 'text_content'):
return '' return ''
attr=getattr(self, 'text_content') attr = getattr(self, 'text_content')
if callable(attr): if callable(attr):
text=attr() text = attr()
else: text=attr else:
n=min(num, len(text)) text = attr
n = min(num, len(text))
return text[:n] return text[:n]
def index_object(self): def index_object(self):
"""A common method to allow Findables to index themselves.""" """A common method to allow Findables to index themselves."""
self._warn_deprecated() self._warn_deprecated()
if hasattr(self, self.default_catalog): catalog = getattr(self, self.default_catalog, None)
getattr(self, self.default_catalog).catalog_object(self, self.url()) if catalog is not None:
catalog.catalog_object(self, self.url())
def unindex_object(self): def unindex_object(self):
"""A common method to allow Findables to unindex themselves.""" """A common method to allow Findables to unindex themselves."""
self._warn_deprecated() self._warn_deprecated()
if hasattr(self, self.default_catalog): catalog = getattr(self, self.default_catalog, None)
getattr(self, self.default_catalog).uncatalog_object(self.url()) if catalog is not None:
catalog.uncatalog_object(self.url())
def reindex_object(self): def reindex_object(self):
""" Suprisingly useful """ """ Suprisingly useful """
...@@ -143,7 +150,8 @@ class CatalogAware: ...@@ -143,7 +150,8 @@ class CatalogAware:
def reindex_all(self, obj=None): def reindex_all(self, obj=None):
""" """ """ """
if obj is None: obj=self if obj is None:
obj = self
if hasattr(aq_base(obj), 'index_object'): if hasattr(aq_base(obj), 'index_object'):
obj.index_object() obj.index_object()
if hasattr(aq_base(obj), 'objectValues'): if hasattr(aq_base(obj), 'objectValues'):
......
...@@ -18,6 +18,7 @@ import warnings ...@@ -18,6 +18,7 @@ import warnings
from Acquisition import aq_base from Acquisition import aq_base
from App.special_dtml import DTMLFile from App.special_dtml import DTMLFile
class CatalogAware: class CatalogAware:
""" This is a Mix-In class to make objects automaticly catalog and """ This is a Mix-In class to make objects automaticly catalog and
uncatalog themselves in Zope, and to provide some other basic uncatalog themselves in Zope, and to provide some other basic
...@@ -54,7 +55,8 @@ class CatalogAware: ...@@ -54,7 +55,8 @@ class CatalogAware:
except Exception: except Exception:
s = 0 s = 0
object.manage_afterAdd(item, container) object.manage_afterAdd(item, container)
if s is None: object._p_deactivate() if s is None:
object._p_deactivate()
def manage_afterClone(self, item): def manage_afterClone(self, item):
self.index_object() self.index_object()
...@@ -64,7 +66,8 @@ class CatalogAware: ...@@ -64,7 +66,8 @@ class CatalogAware:
except Exception: except Exception:
s = 0 s = 0
object.manage_afterClone(item) object.manage_afterClone(item)
if s is None: object._p_deactivate() if s is None:
object._p_deactivate()
def manage_beforeDelete(self, item, container): def manage_beforeDelete(self, item, container):
self.unindex_object() self.unindex_object()
...@@ -74,7 +77,8 @@ class CatalogAware: ...@@ -74,7 +77,8 @@ class CatalogAware:
except Exception: except Exception:
s = 0 s = 0
object.manage_beforeDelete(item, container) object.manage_beforeDelete(item, container)
if s is None: object._p_deactivate() if s is None:
object._p_deactivate()
def creator(self): def creator(self):
"""Return a sequence of user names who have the local """Return a sequence of user names who have the local
...@@ -102,8 +106,9 @@ class CatalogAware: ...@@ -102,8 +106,9 @@ class CatalogAware:
attr=getattr(self, 'text_content') attr=getattr(self, 'text_content')
if callable(attr): if callable(attr):
text=attr() text=attr()
else: text=attr else:
n=min(num, len(text)) text=attr
n = min(num, len(text))
return text[:n] return text[:n]
def index_object(self): def index_object(self):
...@@ -127,7 +132,8 @@ class CatalogAware: ...@@ -127,7 +132,8 @@ class CatalogAware:
def reindex_all(self, obj=None): def reindex_all(self, obj=None):
""" """ """ """
if obj is None: obj=self if obj is None:
obj = self
if hasattr(aq_base(obj), 'index_object'): if hasattr(aq_base(obj), 'index_object'):
obj.index_object() obj.index_object()
if hasattr(aq_base(obj), 'objectValues'): if hasattr(aq_base(obj), 'objectValues'):
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
import ZCatalog import ZCatalog
def initialize(context): def initialize(context):
context.registerClass( context.registerClass(
ZCatalog.ZCatalog, ZCatalog.ZCatalog,
......
...@@ -246,6 +246,7 @@ class IZCatalog(Interface): ...@@ -246,6 +246,7 @@ class IZCatalog(Interface):
""" """
# This should inherit from an IRecord interface, if there ever is one. # This should inherit from an IRecord interface, if there ever is one.
class ICatalogBrain(Interface): class ICatalogBrain(Interface):
"""Catalog brain that handles looking up attributes as """Catalog brain that handles looking up attributes as
required, and provides just enough smarts to let us get the URL, path, required, and provides just enough smarts to let us get the URL, path,
......
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