From cbb8d5cd127bdca318e3001bb2ca706c1bef6e6e Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Fri, 16 Mar 2012 14:59:59 +0100 Subject: [PATCH] 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). --- gen/mixins/ToolMixin.py | 3 ++- pod/actions.py | 38 ++++++++++++++++++++++++++++++++++++++ shared/utils.py | 18 ++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/gen/mixins/ToolMixin.py b/gen/mixins/ToolMixin.py index 6a7886c..bf06c23 100644 --- a/gen/mixins/ToolMixin.py +++ b/gen/mixins/ToolMixin.py @@ -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): diff --git a/pod/actions.py b/pod/actions.py index 58b126f..def475c 100644 --- a/pod/actions.py +++ b/pod/actions.py @@ -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 diff --git a/shared/utils.py b/shared/utils.py index de898c2..16eb3a8 100644 --- a/shared/utils.py +++ b/shared/utils.py @@ -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.'''