appy.pod: variable named 'loop' is now available in the POD context of any section within a 'for' statement, with attributes like loop.[iterVariableName].length (=total number of looped elements) and loop.[iterVariableName].nb (=index of currently looped element).

This commit is contained in:
Gaetan Delannay 2012-03-16 14:59:59 +01:00
parent 0dd8b72dca
commit cbb8d5cd12
3 changed files with 58 additions and 1 deletions

View file

@ -979,7 +979,8 @@ class ToolMixin(BaseMixin):
rolesToShow = [r for r in appyUser.roles \
if r not in ('Authenticated', 'Member')]
if rolesToShow:
res.append(', '.join([self.translate(r) for r in rolesToShow]))
res.append(', '.join([self.translate('role_%s'%r) \
for r in rolesToShow]))
return (' | '.join(res), appyUser.o.getUrl(mode='edit'))
def generateUid(self, className):

View file

@ -17,6 +17,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA.
# ------------------------------------------------------------------------------
from appy import Object
from appy.pod import PodError
from appy.pod.elements import *
@ -128,6 +129,34 @@ class ForAction(BufferAction):
fromExpr)
self.iter = iter # Name of the iterator variable used in the each loop
def initialiseLoop(self):
'''Initialises information about the loop, before entering into it.'''
context = self.buffer.env.context
# The "loop" object, made available in the POD context, contains info
# about all currently walked loops. For every walked loop, a specific
# object, le'ts name it curLoop, accessible at getattr(loop, self.iter),
# stores info about its status:
# * curLoop.length gives the total number of walked elements withhin
# the loop
# * curLoop.nb gives the index (starting at 0) if the currently
# walked element.
# For example, if you have a "for" statement like this:
# for elem in myListOfElements
# Within the part of the ODT document impacted by this statement, you
# may access to:
# * loop.elem.length to know the total length of myListOfElements
# * loop.elem.nb to know the index of the current elem within
# myListOfElements.
if 'loop' not in context:
context['loop'] = Object()
try:
total = len(self.exprResult)
except:
total = 0
curLoop = Object(length=total)
setattr(context['loop'], self.iter, curLoop)
return curLoop
def do(self):
context = self.buffer.env.context
# Check self.exprResult type
@ -156,7 +185,11 @@ class ForAction(BufferAction):
if not self.exprResult:
self.result.dumpElement(Cell.OD.elem)
# Enter the "for" loop
loop = self.initialiseLoop()
i = -1
for item in self.exprResult:
i += 1
loop.nb = i
context[self.iter] = item
# Cell: add a new row if we are at the end of a row
if isCell and (currentColIndex == nbOfColumns):
@ -191,6 +224,11 @@ class ForAction(BufferAction):
context[self.iter] = ''
for i in range(nbOfMissingCellsLastLine):
self.buffer.evaluate(subElements=False)
# Delete the object representing info about the current loop.
try:
delattr(context['loop'], self.iter)
except AttributeError:
pass
# Restore hidden variable if any
if hasHiddenVariable:
context[self.iter] = hiddenVariable

View file

@ -127,6 +127,24 @@ def copyData(data, target, targetMethod, type='string', encoding=None,
dump(encodeData(data.data, encoding))
data = data.next
# ------------------------------------------------------------------------------
def splitList(l, sub):
'''Returns a list that was build from list p_l whose elements were
re-grouped into sub-lists of p_sub elements.
For example, if l = [1,2,3,4,5] and sub = 3, the method returns
[ [1,2,3], [4,5] ].'''
res = []
i = -1
for elem in l:
i += 1
if (i % sub) == 0:
# A new sub-list must be created
res.append([elem])
else:
res[-1].append(elem)
return res
# ------------------------------------------------------------------------------
class Traceback:
'''Dumps the last traceback into a string.'''