Skip to content

Commit

Permalink
Import code into git.
Browse files Browse the repository at this point in the history
  • Loading branch information
thefunny42 committed Oct 15, 2009
0 parents commit d5585bb
Show file tree
Hide file tree
Showing 28 changed files with 919 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# git-ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
*~
*.pyc
*.egg-info
.installed.cfg
bin/
parts/
develop-eggs/
38 changes: 38 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
zeam.utils.batch
================

This package provides a batch functionality for Zope 2 and Zope 3.

You can use it like that:

1. Your container which is going to handle the batch must implements
the marker interface ``zeam.utils.batch.interfaces.IBatchedContent``.

2. You create your batch object in your view code::

from zope.component import queryMultiAdapter
from zeam.utils.batch import batch
from zeam.utils.batch.interfaces import IBatchView


class MyViewClass(...):

def update(self):
fulllist = ...
self.myitems = batch(fulllist, count=10, name='myitems', request=self.request)
self.batch = queryMultiAdapter((self.context, self.myitems, self.request),
IBatchView)()


3. You use it in your template::

<tal:navigation tal:replace="structure view/batch" />

<tal:items tal:repeat="item view/myitems">
...
</tal:items>

<tal:navigation tal:replace="structure view/batch" />

That's it.

55 changes: 55 additions & 0 deletions bootstrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
##############################################################################
#
# Copyright (c) 2006 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Bootstrap a buildout-based project
Simply run this script in a directory containing a buildout.cfg.
The script accepts buildout command-line options, so you can
use the -c option to specify an alternate configuration file.
$Id$
"""

import os, shutil, sys, tempfile, urllib2

tmpeggs = tempfile.mkdtemp()

try:
import pkg_resources
except ImportError:
ez = {}
exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
).read() in ez
ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)

import pkg_resources

cmd = 'from setuptools.command.easy_install import main; main()'
if sys.platform == 'win32':
cmd = '"%s"' % cmd # work around spawn lamosity on windows

ws = pkg_resources.working_set
assert os.spawnle(
os.P_WAIT, sys.executable, sys.executable,
'-c', cmd, '-mqNxd', tmpeggs, 'zc.buildout',
dict(os.environ,
PYTHONPATH=
ws.find(pkg_resources.Requirement.parse('setuptools')).location
),
) == 0

ws.add_entry(tmpeggs)
ws.require('zc.buildout')
import zc.buildout.buildout
zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap'])
shutil.rmtree(tmpeggs)
8 changes: 8 additions & 0 deletions buildout.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[buildout]
develop = .
parts = test

[test]
recipe = zc.recipe.testrunner
eggs = zeam.utils.batch [test]
defaults = ['-v', '-c', '-s', 'zeam.utils.batch']
23 changes: 23 additions & 0 deletions docs/HISTORY.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Changelog
=========

0.4 (unreleased)
----------------

* You adapt the batch with the view on which you display the batch:
its name will keep when generating links,
* Batch can be *disabled* with a count of 0,
* Add translations for french, english and dutch.

0.3 (2008/10/18)
----------------

* Fix and add tests,
* Don't display batch navigation if everything fits on one page,
* No more special links are generated for the first page of the batch.

0.2
---

* Initial release

3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[egg_info]
tag_build = dev
tag_svn_revision = true
50 changes: 50 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from setuptools import setup, find_packages
import os

version = '0.4'

install_requires=[
'setuptools',
'zope.interface',
'zope.schema',
'zope.annotation',
'zope.traversing',
'zope.app.pagetemplate',
'zope.cachedescriptors',
]

tests_requires = install_requires + [
'zope.testing',
'zope.app.testing',
'zope.app.securitypolicy',
'zope.app.zcmlfiles',
'zope.app.zptpage',
],


setup(name='zeam.utils.batch',
version=version,
description="Generic Batch support for Zope",
long_description=open("README.txt").read() + "\n" +
open(os.path.join("docs", "HISTORY.txt")).read(),
classifiers=[
"Framework :: Zope2",
"Framework :: Zope3",
"Programming Language :: Python",
"Topic :: Software Development :: Libraries :: Python Modules",
"License :: OSI Approved :: BSD License",
],
keywords='batch zope',
author='Sylvain Viollon',
author_email='thefunny@gmail.com',
url='',
license='BSD',
packages=find_packages(exclude=['ez_setup']),
namespace_packages=['zeam', 'zeam.utils'],
include_package_data=True,
zip_safe=False,
test_suite='zeam.utils.batch',
tests_require = tests_requires,
install_requires=install_requires,
extras_require = {'test': tests_requires},
)
6 changes: 6 additions & 0 deletions zeam/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
try:
__import__('pkg_resources').declare_namespace(__name__)
except ImportError:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
6 changes: 6 additions & 0 deletions zeam/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
try:
__import__('pkg_resources').declare_namespace(__name__)
except ImportError:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
2 changes: 2 additions & 0 deletions zeam/utils/batch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

from batch import batch
117 changes: 117 additions & 0 deletions zeam/utils/batch/batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Copyright Sylvain Viollon 2008 (c)
# $Id: batch.py 94 2008-10-20 22:20:34Z sylvain $

from zope.interface import implements

from interfaces import IBatch

class batchBaseIterator(object):
"""An iterator on batch object.
"""

def __init__(self, context):
self.context = context
self.start = 0

def __iter__(self):
return self


class batchItemIterator(batchBaseIterator):
"""Return the next object in the batch iterator.
"""

def next(self):
try:
elt = self.context[self.start]
except IndexError:
raise StopIteration
self.start += 1
return elt

class batchIndiceIterator(batchBaseIterator):
"""Return the next indice in the batch iterator.
"""

def next(self):
last = self.context.last
if not last:
raise StopIteration
if self.start < last:
value = self.start
self.start += self.context.count
return (value, value / self.context.count + 1)
raise StopIteration


class batch(object):
"""A simple batch object.
"""

implements(IBatch)

def __init__(self, collection, start=0, count=10, name='', request=None):
if not (request is None):
key = 'bstart'
if name:
key += '_' + name
start = int(request.form.get(key, 0))
self.start = start
self.count = count
self.data = collection
self.name = name

def _setData(self, data):
self._data = data
self._end = len(self._data)
if not self.count or self.count > self._end:
self._max = self._end
else:
self._max = self.count

def _getData(self):
return self._data

data = property(_getData, _setData)

def __getitem__(self, index):
if index < 0 or index >= self._max:
raise IndexError, "invalid index"
return self.data[self.start + index]

def batchLen(self):
if not self.count:
return 0
last = self._end % self.count
if last:
last = 1
return (self._end / self.count) + last

def __iter__(self):
return batchItemIterator(self)

def all(self):
return batchIndiceIterator(self)

@property
def first(self):
return 0

@property
def previous(self):
previous = self.start - self.count
if previous < 0:
return None
return previous

@property
def last(self):
return self.batchLen() * self.count

@property
def next(self):
next = self.start + self.count
if next >= self.last:
return None
return next

Loading

0 comments on commit d5585bb

Please sign in to comment.