Exemplo n.º 1
0
 def write(self, fname, tmpl_name, kwds):
     "Create new file from given template name and set of arguments"
     code = ""
     read_code = False
     with open(fname, 'w') as stream:
         for line in open(tmpl_name, 'r').readlines():
             line = self.parse_etags(line)
             if  not line:
                 continue
             if  line.find('#python_begin') != -1:
                 read_code = True
                 continue
             if  line.find('#python_end') != -1:
                 read_code = False
             if  read_code:
                 code += line
             if  code and not read_code:
                 res   = functor(code, kwds, self.debug)
                 stream.write(res)
                 code  = ""
                 continue
             if  not read_code:
                 for key, val in kwds.items():
                     if  isinstance(val, basestring):
                         line = line.replace(key, val)
                 stream.write(line)
Exemplo n.º 2
0
    def test_functor(self):
        "Test functor function"
        snippet = """
#python_begin
    for i in __data__:
        print i
#python_end
        """
        data = range(5)
        result = functor(snippet, {"__data__": data})
        expect = "\n".join([str(i) for i in data]) + "\n"
        self.assertEqual(expect, result)