def create_template_file(tdir, template, from_template=None, template_code=None):
    splitted = template.split('/')
    fname = splitted[-1]
    if len(splitted) > 1:  # we have to check/create containing dirs
        for dname in splitted[:-1]:
            if not tdir.has_key(dname):
                # lets create it
                new = Directory(dname)
                tdir[dname] = new
                tdir = tdir
    
            tdir = tdir[dname]
    
    
    # get the template-template name
    if from_template and from_template != 'none':
        print 'getting template template:', from_template
        # and create the pagetemplate from the template-template
        if fname not in tdir.keys():
            templ = DTMLTemplate()
            tdir.factories[fname] = JinjaTemplate
            templ.template = templatepath(from_template) + '.dtml'
            tdir[fname] = templ
    else:
        # if template code is given, fill it into the file
        if template_code:
            if fname not in tdir.keys():
                file = File()
                file.data = template_code
                tdir[fname] = file
Пример #2
0
    def test_file_mode_text(self):
        filepath = os.path.join(self.tempdir, 'file.txt')
        file = File(name=filepath)
        file.direct_sync = True

        self.assertEqual(file.mode, MODE_TEXT)
        self.assertEqual(file.data, '')
        self.assertEqual(file.lines, [])

        file.data = 'abc\ndef'
        file()
        with open(filepath) as f:
            out = f.readlines()
        self.assertEqual(out, ['abc\n', 'def'])

        file = File(name=filepath)
        self.assertEqual(file.data, 'abc\ndef')
        self.assertEqual(file.lines, ['abc', 'def'])

        file.lines = ['a', 'b', 'c']
        file()
        with open(filepath) as f:
            out = f.read()
        self.assertEqual(out, 'a\nb\nc')