Commit 15563c3f authored by Evan Simpson's avatar Evan Simpson

Initial checkin

parent c3a67c7e
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
__doc__='''Batch class, for iterating over a sequence in batches
$Id: Batch.py,v 1.1 2001/04/27 16:38:52 evan Exp $'''
__version__='$Revision: 1.1 $'[11:-2]
from ExtensionClass import Base
class LazyPrevBatch(Base):
def __of__(self, parent):
return Batch(parent._sequence, parent._size,
0, parent._first + parent.overlap,
parent.orphan, parent.overlap)
class LazyNextBatch(Base):
def __of__(self, parent):
try: parent._sequence[parent.end]
except IndexError: return None
return Batch(parent._sequence, parent._size,
parent.end - parent.overlap, 0,
parent.orphan, parent.overlap)
class Batch(Base):
"""Create a sequence batch"""
__allow_access_to_unprotected_subobjects__ = 1
previous = LazyPrevBatch(0)
next = LazyNextBatch(1)
def __init__(self, sequence, size, start=0, end=0,
orphan=3, overlap=0):
start = start + 1
start,end,sz = opt(start,end,size,orphan,sequence)
self._sequence = sequence
self.size = sz
self._size = size
self.start = start
self.end = end
self.orphan = orphan
self.overlap = overlap
self._first = max(start - 1, 0)
self.length = self.end - self._first
if self._first == 0:
self.previous = None
def __getitem__(self, index):
if index < 0:
if index + self.end < self._first: raise IndexError, index
return self._sequence[index + self.end]
if index >= self.end: raise IndexError, index
return self._sequence[index+self._first]
def __len__(self):
return self.length
def opt(start,end,size,orphan,sequence):
if size < 1:
if start > 0 and end > 0 and end >= start:
size=end+1-start
else: size=7
if start > 0:
try: sequence[start-1]
except: start=len(sequence)
if end > 0:
if end < start: end=start
else:
end=start+size-1
try: sequence[end+orphan-1]
except: end=len(sequence)
elif end > 0:
try: sequence[end-1]
except: end=len(sequence)
start=end+1-size
if start - 1 < orphan: start=1
else:
start=1
end=start+size-1
try: sequence[end+orphan-1]
except: end=len(sequence)
return start,end,size
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
__doc__='''Iterator class
$Id: Iterator.py,v 1.1 2001/04/27 16:38:52 evan Exp $'''
__version__='$Revision: 1.1 $'[11:-2]
class Iterator:
'''Simple Iterator class'''
__allow_access_to_unprotected_subobjects__ = 1
def __init__(self, seq):
self.seq = seq
self.nextIndex = 0
def next(self):
i = self.nextIndex
try:
self.seq[i]
except IndexError:
return 0
self.index = i
self.nextIndex = i+1
return 1
def number(self): return self.nextIndex
def even(self): return not self.index % 2
def odd(self): return self.index % 2
def letter(self, base=ord('a'), radix=26):
index = self.index
s = ''
while 1:
index, off = divmod(index, radix)
s = chr(base + off) + s
if not index: return s
def Letter(self):
return self.letter(base=ord('A'))
def start(self): return self.nextIndex == 1
def end(self):
try: self.seq[self.nextIndex]
except IndexError: return 1
return 0
def item(self):
return self.seq[self.index]
def length(self):
return len(self.seq)
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
__doc__='''Tree manipulation classes
$Id: Tree.py,v 1.1 2001/04/27 16:38:52 evan Exp $'''
__version__='$Revision: 1.1 $'[11:-2]
from Acquisition import Explicit
from ComputedAttribute import ComputedAttribute
class TreeNode(Explicit):
__allow_access_to_unprotected_subobjects__ = 1
state = 0 # leaf
height = 1
size = 1
def __init__(self):
self._child_list = []
def _add_child(self, child):
'Add a child which already has all of its children.'
self._child_list.append(child)
self.height = max(self.height, child.height + 1)
self.size = self.size + child.size
def flat(self):
'Return a flattened preorder list of tree nodes'
items = []
self.walk(items.append)
return items
def walk(self, f, data=None):
'Preorder walk this tree, passing each node to a function'
if data is None:
f(self)
else:
f(self, data)
for child in self._child_list:
child.__of__(self).walk(f, data)
def _depth(self):
return self.aq_parent.depth + 1
depth = ComputedAttribute(_depth, 1)
def __getitem__(self, index):
return self._child_list[index].__of__(self)
def __len__(self):
return len(self._child_list)
class TreeMaker:
'''Class for mapping a hierachy of objects into a tree of nodes.'''
__allow_access_to_unprotected_subobjects__ = 1
_id = 'tpId'
_values = 'tpValues'
_assume_children = 0
_values_filter = None
_expand_root = 1
def tree(self, root, expanded=None, subtree=0):
'''Create a tree from root, with specified nodes expanded.
"expanded" must be false, true, or a mapping.
Each key of the mapping is the id of a top-level expanded
node, and each value is the "expanded" value for the
children of that node.
'''
node = self.node(root)
child_exp = expanded
if not simple_type(expanded):
# Assume a mapping
expanded = expanded.has_key(node.id)
child_exp = child_exp.get(node.id)
if expanded or (not subtree and self._expand_root):
children = self.getChildren(root)
if children:
node.state = 1 # expanded
for child in children:
node._add_child(self.tree(child, child_exp, 1))
elif self.hasChildren(root):
node.state = -1 # collapsed
if not subtree:
node.depth = 0
return node
def node(self, object):
node = TreeNode()
node.object = object
node.id = b2a(self.getId(object))
return node
def getId(self, object):
id_attr = self._id
if hasattr(object, id_attr):
obid = getattr(object, id_attr)
if not simple_type(obid): obid = obid()
return obid
if hasattr(object, '_p_oid'): return str(object._p_oid)
return id(object)
def hasChildren(self, object):
if self._assume_children:
return 1
return self.getChildren(object)
def getChildren(self, object):
if self._values_filter and hasattr(object, 'aq_acquire'):
return object.aq_acquire(self._values, aqcallback,
self._values_filter)()
return getattr(object, self._values)()
def simple_type(ob,
is_simple={type(''):1, type(0):1, type(0.0):1,
type(0L):1, type(None):1 }.has_key):
return is_simple(type(ob))
def aqcallback(self, inst, parent, name, value, filter):
return filter(self, inst, parent, name, value)
from binascii import b2a_base64, a2b_base64
import string
from string import split, join, translate
a2u_map = string.maketrans('+/=', '-._')
u2a_map = string.maketrans('-._', '+/=')
def b2a(s):
'''Encode a value as a cookie- and url-safe string.
Encoded string use only alpahnumeric characters, and "._-".
'''
s = str(s)
if len(s) <= 57:
return translate(b2a_base64(s)[:-1], a2u_map)
frags = []
for i in range(0, len(s), 57):
frags.append(b2a_base64(s[i:i + 57])[:-1])
return translate(join(frags, ''), a2u_map)
def a2b(s):
'''Decode a b2a-encoded string.'''
s = translate(s, u2a_map)
if len(s) <= 76:
return a2b_base64(s)
frags = []
for i in range(0, len(s), 76):
frags.append(a2b_base64(s[i:i + 76]))
return join(frags, '')
def encodeExpansion(nodes):
'''Encode the expanded node ids of a tree into a string.
Accepts a list of nodes, such as that produced by root.flat().
Marks each expanded node with an expansion_number attribute.
Since node ids are encoded, the resulting string is safe for
use in cookies and URLs.
'''
steps = []
last_depth = -1
n = 0
for node in nodes:
if node.state <=0: continue
dd = last_depth - node.depth + 1
last_depth = node.depth
if dd > 0:
steps.append('.' * dd)
steps.append(node.id)
node.expansion_number = n
n = n + 1
return join(steps, ':')
def decodeExpansion(s, nth=None):
'''Decode an expanded node map from a string.
If nth is an integer, also return the (map, key) pair for the nth entry.
'''
map = m = {}
mstack = []
pop = 0
nth_pair = None
if nth is not None:
nth_pair = (None, None)
for step in split(s, ':'):
if step[:1] == '.':
pop = len(step) - 1
continue
if pop < 0:
mstack.append(m)
m[obid] = {}
m = m[obid]
elif map:
m[obid] = None
if len(step) == 0:
return map
obid = step
if pop > 0:
m = mstack[-pop]
del mstack[-pop:]
pop = -1
if nth == 0:
nth_pair = (m, obid)
nth = None
elif nth is not None:
nth = nth - 1
m[obid] = None
if nth == 0:
return map, (m, obid)
if nth_pair is not None:
return map, nth_pair
return map
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
__doc__='''Package of template utility classes and functions.
$Id: __init__.py,v 1.1 2001/04/27 16:38:52 evan Exp $'''
__version__='$Revision: 1.1 $'[11:-2]
__allow_access_to_unprotected_subobjects__ = 1
__roles__ = None
from Batch import Batch
from Iterator import Iterator
from Tree import TreeMaker, encodeExpansion, decodeExpansion, a2b, b2a
##############################################################################
#
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
#
# Copyright (c) Digital Creations. All rights reserved.
#
# This license has been certified as Open Source(tm).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions in source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions, and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. Digital Creations requests that attribution be given to Zope
# in any manner possible. Zope includes a "Powered by Zope"
# button that is installed by default. While it is not a license
# violation to remove this button, it is requested that the
# attribution remain. A significant investment has been put
# into Zope, and this effort will continue if the Zope community
# continues to grow. This is one way to assure that growth.
#
# 4. All advertising materials and documentation mentioning
# features derived from or use of this software must display
# the following acknowledgement:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# In the event that the product being advertised includes an
# intact Zope distribution (with copyright and license included)
# then this clause is waived.
#
# 5. Names associated with Zope or Digital Creations must not be used to
# endorse or promote products derived from this software without
# prior written permission from Digital Creations.
#
# 6. Modified redistributions of any form whatsoever must retain
# the following acknowledgment:
#
# "This product includes software developed by Digital Creations
# for use in the Z Object Publishing Environment
# (http://www.zope.org/)."
#
# Intact (re-)distributions of any official Zope release do not
# require an external acknowledgement.
#
# 7. Modifications are encouraged but must be packaged separately as
# patches to official Zope releases. Distributions that do not
# clearly separate the patches from the original work must be clearly
# labeled as unofficial distributions. Modifications which do not
# carry the name Zope may be packaged in any form, as long as they
# conform to all of the clauses above.
#
#
# Disclaimer
#
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations. Specific
# attributions are listed in the accompanying credits file.
#
##############################################################################
######################################################################
# Set up unit testing framework
#
# The following code should be at the top of every test module:
#
# import os, sys
# execfile(os.path.join(sys.path[0], 'framework.py'))
#
# ...and the following at the bottom:
#
# if __name__ == '__main__':
# main()
import string
scriptdir = sys.path[0]
input_dir = os.path.join(scriptdir, 'input')
output_dir = os.path.join(scriptdir, 'output')
if not sys.modules.has_key('unittest'):
if os.path.abspath(scriptdir) == os.path.abspath('.'):
# We're in the tests directory, and need to find unittest.
cwd = os.getcwd()
while 1:
for ext in 'py', 'pyc', 'pyo', 'pyd':
if os.path.isfile(os.path.join(cwd, 'unittest.' + ext)):
break
else:
cwd, lastdir = os.path.split(cwd)
if lastdir:
continue
break
sys.path.insert(1, cwd)
else:
# We must be in the same directory as unittest
sys.path.insert(1, '')
import unittest
TestRunner = unittest.TextTestRunner
def read_input(filename):
filename = os.path.join(input_dir, filename)
return open(filename, 'r').read()
def read_output(filename):
filename = os.path.join(output_dir, filename)
return open(filename, 'r').read()
def main():
if len(sys.argv) > 1:
errs = globals()[sys.argv[1]]()
else:
errs = TestRunner().run(test_suite())
sys.exit(errs and 1 or 0)
def debug():
test_suite().debug()
def pdebug():
import pdb
pdb.run('debug()')
#! /usr/bin/env python1.5
"""Run all tests."""
import os, sys, glob
execfile(os.path.join(sys.path[0], 'framework.py'))
def test_suite():
suite = unittest.TestSuite()
for mname in glob.glob(os.path.join(sys.path[0], 'test*.py')):
mname = os.path.split(mname)[1][:-3]
m = __import__(mname)
suite.addTest(m.test_suite())
return suite
if __name__ == "__main__":
main()
import os, sys
execfile(os.path.join(sys.path[0], 'framework.py'))
import string
from ZTUtils import Batch
class BatchTests(unittest.TestCase):
def testEmpty(self):
'''Test empty Batch'''
b = Batch([], 5)
assert b.previous is None
assert b.next is None
assert len(b) == b.start == b.end == 0, (len(b), b.start, b.end)
def testSingle(self):
'''Test single Batch'''
for bsize in range(1, 6):
seq = range(bsize)
b = Batch(seq, 5)
assert b.previous is None
assert b.next is None
assert b.start == 1, b.start
assert len(b) == b.end == bsize
for i in seq:
assert b[i] == i, (b[i], i)
neg = -1 - i
assert b[neg] == (bsize + neg), (b[neg], (bsize + neg))
def testOrphan(self):
'''Test orphan collection'''
for bsize in (6, 7):
b = Batch(range(bsize), 5)
assert b.next is None
assert len(b) == bsize
b = Batch(range(8), 5)
assert len(b) == 5
assert len(b.next) == 3
def test_suite():
return unittest.makeSuite(BatchTests)
if __name__=='__main__':
main()
import os, sys
execfile(os.path.join(sys.path[0], 'framework.py'))
from ZTUtils import Iterator
class IteratorTests(unittest.TestCase):
def testIterator0(self):
it = Iterator(())
assert not it.next(), "Empty iterator"
def testIterator1(self):
it = Iterator((1,))
assert it.next() and not it.next(), "Single-element iterator"
def testIterator2(self):
it = Iterator('text')
for c in 'text':
assert it.next(), "Multi-element iterator"
assert not it.next(), "Multi-element iterator"
def test_suite():
return unittest.makeSuite(IteratorTests)
if __name__=='__main__':
main()
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