Commit 74261752 authored by Hanno Schlichting's avatar Hanno Schlichting

Final PEP8 fixes

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