Mercurial > p > roundup > code
annotate roundup/cgi/ZTUtils/Batch.py @ 4587:a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
We now have two configurable templating engines, the old Zope TAL
templates (called zopetal in the config) and the new Chameleon (called
chameleon in the config). A new config-option "template_engine" under
[main] can take these config-options, the default is zopetal.
Thanks to Cheer Xiao for the idea of making this configurable *and*
for the actual implementation!
Cheer Xiao commit log:
- The original TAL engine ported from Zope is thereafter referred to as
"zopetal", in speech and in code
- A new option "template_engine" under [main] introduced
- Zopetal-specific code stripped from cgi/templating.py to form the new
cgi/engine_zopetal.py
- Interface to Chameleon in cgi/engine_chameleon.py
- Engines are supposed to provide a Templates class that mimics the
behavior of the old cgi.templating.Templates. The Templates class is
preferably subclassed from cgi.templating.TemplatesBase.
- New function cgi.templating.get_templates to get the appropriate engine's
Templates instance according to the engine name
| author | Ralf Schlatterbeck <rsc@runtux.com> |
|---|---|
| date | Thu, 23 Feb 2012 18:10:03 +0100 |
| parents | 6e3e4f24c753 |
| children | 39af8a0f3446 |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. | |
| 4 # | |
| 5 # This software is subject to the provisions of the Zope Public License, | |
| 6 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | |
| 7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | |
| 8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | |
| 10 # FOR A PARTICULAR PURPOSE | |
| 11 # | |
| 12 ############################################################################## | |
| 13 __doc__='''Batch class, for iterating over a sequence in batches | |
| 14 | |
|
4570
6e3e4f24c753
Remove keyword expansions from CVS. All regression tests passed afterwards.
Eric S. Raymond <esr@thyrsus.com>
parents:
4532
diff
changeset
|
15 ''' |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1120
diff
changeset
|
16 __docformat__ = 'restructuredtext' |
| 1049 | 17 |
| 18 class LazyPrevBatch: | |
| 19 def __of__(self, parent): | |
| 20 return Batch(parent._sequence, parent._size, | |
| 21 parent.first - parent._size + parent.overlap, 0, | |
| 22 parent.orphan, parent.overlap) | |
| 23 | |
| 24 class LazyNextBatch: | |
| 25 def __of__(self, parent): | |
| 26 try: parent._sequence[parent.end] | |
| 27 except IndexError: return None | |
| 28 return Batch(parent._sequence, parent._size, | |
| 29 parent.end - parent.overlap, 0, | |
| 30 parent.orphan, parent.overlap) | |
| 31 | |
| 32 class LazySequenceLength: | |
| 33 def __of__(self, parent): | |
| 34 parent.sequence_length = l = len(parent._sequence) | |
| 35 return l | |
| 36 | |
| 37 class Batch: | |
| 38 """Create a sequence batch""" | |
| 39 __allow_access_to_unprotected_subobjects__ = 1 | |
| 40 | |
| 41 previous = LazyPrevBatch() | |
| 42 next = LazyNextBatch() | |
| 43 sequence_length = LazySequenceLength() | |
| 44 | |
| 45 def __init__(self, sequence, size, start=0, end=0, | |
| 46 orphan=0, overlap=0): | |
| 47 '''Encapsulate "sequence" in batches of "size". | |
| 48 | |
| 49 Arguments: "start" and "end" are 0-based indexes into the | |
| 50 sequence. If the next batch would contain no more than | |
| 51 "orphan" elements, it is combined with the current batch. | |
| 52 "overlap" is the number of elements shared by adjacent | |
| 53 batches. If "size" is not specified, it is computed from | |
|
4532
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
54 "start" and "end". If "size" is 0, it is the length of |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
55 the sequence. Failing that, it is 7. |
| 1049 | 56 |
| 57 Attributes: Note that the "start" attribute, unlike the | |
| 58 argument, is a 1-based index (I know, lame). "first" is the | |
| 59 0-based index. "length" is the actual number of elements in | |
| 60 the batch. | |
| 61 | |
| 62 "sequence_length" is the length of the original, unbatched, sequence | |
| 63 ''' | |
| 64 | |
| 65 start = start + 1 | |
| 66 | |
| 67 start,end,sz = opt(start,end,size,orphan,sequence) | |
| 68 | |
| 69 self._sequence = sequence | |
| 70 self.size = sz | |
| 71 self._size = size | |
| 72 self.start = start | |
| 73 self.end = end | |
| 74 self.orphan = orphan | |
| 75 self.overlap = overlap | |
| 76 self.first = max(start - 1, 0) | |
| 77 self.length = self.end - self.first | |
| 78 if self.first == 0: | |
| 79 self.previous = None | |
| 80 | |
| 81 | |
| 82 def __getitem__(self, index): | |
| 83 if index < 0: | |
| 84 if index + self.end < self.first: raise IndexError, index | |
| 85 return self._sequence[index + self.end] | |
| 86 | |
| 87 if index >= self.length: raise IndexError, index | |
|
1120
c26471971d18
Exposed the Batch mechanism through the top-level "utils" variable.
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
88 return self._sequence[index + self.first] |
| 1049 | 89 |
| 90 def __len__(self): | |
| 91 return self.length | |
| 92 | |
| 93 def opt(start,end,size,orphan,sequence): | |
| 94 if size < 1: | |
|
4532
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
95 if size == 0: |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
96 size=len(sequence) |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
97 elif start > 0 and end > 0 and end >= start: |
| 1049 | 98 size=end+1-start |
| 99 else: size=7 | |
| 100 | |
| 101 if start > 0: | |
| 102 | |
| 103 try: sequence[start-1] | |
| 104 except IndexError: start=len(sequence) | |
| 105 | |
| 106 if end > 0: | |
| 107 if end < start: end=start | |
| 108 else: | |
| 109 end=start+size-1 | |
| 110 try: sequence[end+orphan-1] | |
| 111 except IndexError: end=len(sequence) | |
| 112 elif end > 0: | |
| 113 try: sequence[end-1] | |
| 114 except IndexError: end=len(sequence) | |
| 115 start=end+1-size | |
| 116 if start - 1 < orphan: start=1 | |
| 117 else: | |
| 118 start=1 | |
| 119 end=start+size-1 | |
| 120 try: sequence[end+orphan-1] | |
| 121 except IndexError: end=len(sequence) | |
| 122 return start,end,size |
