Пример #1
0
 def handle_for(self, model, attrs):
     loopvar = self.gensym("loopvar")
     self.loopvars.append(loopvar)
     data = {"loopvar":loopvar}
     data.update(attrs)
     res = zebra.trim(
         '''
         _%(loopvar)s_max_ = len(scope["%(series)s"])
         for %(loopvar)s in range(_%(loopvar)s_max_):
             # handle scope inside the loop in case we have
             # recursive names (eg, children->children->children)
             scope_stack.append(copy.copy(scope))
             
             # can't do .update if it's a UserDict:
             mdl = scope["%(series)s"][%(loopvar)s]
             for item in mdl.keys():
                 scope[item]=mdl[item]
         ''' % data)
     res = res + zebra.indent(self.walk(model), 1)            
     res = res + zebra.trim(
         '''
         #   ## close for-%(series)s loop ##########
             globals().update(scope_stack.pop())
         ''' % attrs)
     self.lastLoopvar = self.loopvars.pop()
     return res
Пример #2
0
    def handle_zebra(self, model, attrs):
        res = zebra.trim(
            """
            class Report:
            
                def show(self, model={}):
                    print self.fetch(model)

                def fetch(self, model={}):
                    import copy   # used for pushing scope onto stack

                    scope = globals()
                    # This scope thing is so that we can generate
                    # code that says:
                    #
                    #         zres = zres + x
                    # *OR*
                    #         zres = zres + scope.get(x, '')
                    #
                    # It also actually does variable scoping,
                    # when combined with scope_stack, below.
                    #
                    # I wanted to use scope=locals(), but
                    # then the 'zres + x' wouldn't work.
                    # @TODO: is this scope scheme threadsafe?
                    
                    scope_stack = []

                    # scope.update(model), but model might be a UserDict:
                    for item in model.keys():
                        scope[item] = model[item]

                    # zres is the result (the output we're building)
                    zres = ""
            """)
        res = res + zebra.indent(self.walk(model), 2)
        res = res + zebra.trim(
            ''' 
            # end of Report.fetch()
                    return zres

            def fetch(model={}):
                return Report().fetch(model)
                
            def show(model={}):
                return Report().show(model)
            ''')
        return res
Пример #3
0
    def handle_zebra(self, model, attrs):
        res = zebra.trim("""
            class Report:
            
                def show(self, model={}):
                    print self.fetch(model)

                def fetch(self, model={}):
                    import copy   # used for pushing scope onto stack

                    scope = globals()
                    # This scope thing is so that we can generate
                    # code that says:
                    #
                    #         zres = zres + x
                    # *OR*
                    #         zres = zres + scope.get(x, '')
                    #
                    # It also actually does variable scoping,
                    # when combined with scope_stack, below.
                    #
                    # I wanted to use scope=locals(), but
                    # then the 'zres + x' wouldn't work.
                    # @TODO: is this scope scheme threadsafe?
                    
                    scope_stack = []

                    # scope.update(model), but model might be a UserDict:
                    for item in model.keys():
                        scope[item] = model[item]

                    # zres is the result (the output we're building)
                    zres = ""
            """)
        res = res + zebra.indent(self.walk(model), 2)
        res = res + zebra.trim(''' 
            # end of Report.fetch()
                    return zres

            def fetch(model={}):
                return Report().fetch(model)
                
            def show(model={}):
                return Report().show(model)
            ''')
        return res
Пример #4
0
 def handle_for(self, model, attrs):
     loopvar = self.gensym("loopvar")
     self.loopvars.append(loopvar)
     data = {"loopvar": loopvar}
     data.update(attrs)
     res = zebra.trim('''
         _%(loopvar)s_max_ = len(scope["%(series)s"])
         for %(loopvar)s in range(_%(loopvar)s_max_):
             # handle scope inside the loop in case we have
             # recursive names (eg, children->children->children)
             scope_stack.append(copy.copy(scope))
             
             # can't do .update if it's a UserDict:
             mdl = scope["%(series)s"][%(loopvar)s]
             for item in mdl.keys():
                 scope[item]=mdl[item]
         ''' % data)
     res = res + zebra.indent(self.walk(model), 1)
     res = res + zebra.trim('''
         #   ## close for-%(series)s loop ##########
             globals().update(scope_stack.pop())
         ''' % attrs)
     self.lastLoopvar = self.loopvars.pop()
     return res
Пример #5
0
 def handle_glue(self, model, attrs):
     res = "if %s + 1 < _%s_max_:\n" % (self.loopvars[-1],
                                        self.loopvars[-1])
     res = res + zebra.indent(self.walk(model), 1)
     return res
Пример #6
0
 def handle_head(self, model, attrs):
     res = "if %s == 0:\n" % self.loopvars[-1]
     res = res + zebra.indent(self.walk(model), 1)
     return res
Пример #7
0
 def handle_el(self, model, attrs):
     res = "else:\n"
     res = res + zebra.indent(self.walk(model), 1)
     return res
Пример #8
0
 def handle_ef(self, model, attrs):
     res = "elif %s:\n" % attrs["condition"]
     res = res + zebra.indent(self.walk(model), 1)
     return res
Пример #9
0
 def handle_none(self, model, attrs):
     assert self.lastLoopvar, "found none without for!"
     res = "if not _%s_max_:\n" % self.lastLoopvar
     res = res + zebra.indent(self.walk(model), 1)
     return res
Пример #10
0
 def handle_glue(self, model, attrs):
     res = "if %s + 1 < _%s_max_:\n" % (self.loopvars[-1],
                                        self.loopvars[-1])
     res = res + zebra.indent(self.walk(model), 1)
     return res
Пример #11
0
 def handle_head(self, model, attrs):
     res = "if %s == 0:\n" % self.loopvars[-1]
     res = res + zebra.indent(self.walk(model), 1)
     return res
Пример #12
0
 def handle_el(self, model, attrs):
     res = "else:\n"
     res = res + zebra.indent(self.walk(model), 1)
     return res
Пример #13
0
 def handle_ef(self, model, attrs):
     res = "elif %s:\n" % attrs["condition"]
     res = res + zebra.indent(self.walk(model), 1)
     return res
Пример #14
0
 def handle_none(self, model, attrs):
     assert self.lastLoopvar, "found none without for!"
     res = "if not _%s_max_:\n" % self.lastLoopvar
     res = res + zebra.indent(self.walk(model), 1)
     return res