Beispiel #1
0
 def __init__(self, conn, suite):
     table = ujoin(suite, 'templates')
     _TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
     self.traitparent = TraitParent(conn, suite)
     self.template = Template()
     self.template.set_suite(suite)
     self.template_path = None
Beispiel #2
0
 def __init__(self, conn, suite):
     table = ujoin(suite, 'templates')
     _TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
     self.traitparent = TraitParent(conn, suite)
     self.template = Template()
     self.template.set_suite(suite)
     self.template_path = None
     self.textfiles = TextFileManager(self.conn)
     self._jtable = '%s as s join textfiles as t ' % table
     self._jtable += 'on s.templatefile = t.fileid' 
Beispiel #3
0
 def __init__(self, conn, suite):
     table = ujoin(suite, 'templates')
     _TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
     self.traitparent = TraitParent(conn, suite)
     self.template = Template()
     self.template.set_suite(suite)
     self.template_path = None
 def __init__(self, conn, suite):
     table = ujoin(suite, 'templates')
     TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
     self.traitparent = TraitParent(conn, suite)
     self.template = Template()
     self.template.set_suite(suite)
     self.template_path = None
     self.textfiles = TextFileManager(self.conn)
     self._jtable = '%s as s join textfiles as t ' % table
     self._jtable += 'on s.templatefile = t.fileid' 
Beispiel #5
0
class TraitTemplate(_TraitRelation):
    def __init__(self, conn, suite):
        table = ujoin(suite, 'templates')
        _TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
        self.traitparent = TraitParent(conn, suite)
        self.template = Template()
        self.template.set_suite(suite)
        self.template_path = None

    def _clause(self, package, template, trait=None):
        if trait is None:
            trait = self.current_trait
        return Eq('trait', trait) & Eq('package', package) & Eq('template', template)
    
    def templates(self, trait=None, fields=['*']):
        if trait is None:
            trait = self.current_trait
            self.reset_clause()
            return self.cmd.select(fields=fields, order=['package', 'template'])
        else:
            self.set_clause(trait)
            rows = self.cmd.select(fields=fields, order=['package', 'template'])
            self.reset_clause()
            return rows

    def has_template(self, template):
        return self.has_it('template', template)

    def get_row(self, template):
        return _TraitRelation.get_row(self, 'template', template)

    def insert_template(self, data, templatefile):
        if type(data) is not dict:
            raise Error, 'Need to pass dict'
        if len(data) == 2 and 'template' in data and 'package' in data:
            data.update(dict(owner='root', grp_owner='root', mode='0100644'))
        insert_data = {'trait' : self.current_trait}
        insert_data.update(data)
        f = self.cmd.file(self.conn)
        insert_data['templatefile'] = f.name
        f.write(templatefile.read())
        f.close()
        self.cmd.insert(data=insert_data)

    def update_template(self, data, templatefile):
        clause = self._clause(data['package'], data['template'])
        fields = ['owner', 'grp_owner', 'mode']
        update = {}.fromkeys(fields)
        for field in update:
            update[field] = data[field]
        row = self.cmd.select_row(fields=['templatefile'], clause=clause)
        self.conn.removefile(row.templatefile.name)
        f = self.cmd.file(self.conn)
        update['templatefile'] = f.name
        f.write(templatefile.read())
        f.close()
        self.cmd.update(data=update, clause=clause)
        self.reset_clause()

    def drop_template(self, package, template):
        clause = self._clause(package, template)
        self._drop_template(clause)
        
    def _drop_template(self, clause):
        self.cmd.delete(clause=clause)
        self.reset_clause()

    def set_trait(self, trait):
        _TraitRelation.set_trait(self, trait)
        self.traitparent.set_trait(trait)
        self.template.set_trait(trait)
        
    def set_template(self, package, template):
        self.template.set_template(self.templatefile(package, template))
        self.template.update(self.traitparent.Environment())

    def set_template_path(self, path):
        self.template_path = join(path, self.suite, self.current_trait)
        
        
    def backup_templates(self, bkup_path):
        makepaths(join(bkup_path, 'templates'))
        for t in self.templates():
            npath = join(bkup_path, 'templates', t.package, t.template)
            tfile = self.templatefile(t.package, t.template)
            makepaths(dirname(npath))
            filecopy(tfile, npath + '.template')
            tfile.close()
            
    def templatefile(self, package, template):
        clause = self._clause(package, template)
        return self.cmd.open_file(self.conn, 'templatefile', clause)

    def remove_templatefile(self, package, template):
        clause = self._clause(package, template)
        self.cmd.delete_file(self.conn, 'templatefile', clause)
            
    def save_template(self, package, template, templatefile):
        clause = self._clause(package, template)
        self.cmd.update_file(self.conn, 'templatefile', clause, templatefile)
Beispiel #6
0
class TraitTemplate(_TraitRelation):
    def __init__(self, conn, suite):
        table = ujoin(suite, 'templates')
        _TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
        self.traitparent = TraitParent(conn, suite)
        self.template = Template()
        self.template.set_suite(suite)
        self.template_path = None
        self.textfiles = TextFileManager(self.conn)
        self._jtable = '%s as s join textfiles as t ' % table
        self._jtable += 'on s.templatefile = t.fileid' 

    def _clause(self, package, template, trait=None):
        if trait is None:
            trait = self.current_trait
        return Eq('trait', trait) & Eq('package', package) & Eq('template', template)
    
    def templates(self, trait=None, fields=['*']):
        if trait is None:
            trait = self.current_trait
            self.reset_clause()
            return self.cmd.select(fields=fields, order=['package', 'template'])
        else:
            self.set_clause(trait)
            rows = self.cmd.select(fields=fields, order=['package', 'template'])
            self.reset_clause()
            return rows

    def has_template(self, template):
        return self.has_it('template', template)

    def get_row(self, template):
        return _TraitRelation.get_row(self, 'template', template)

    def insert_template(self, data, templatefile):
        if type(data) is not dict:
            raise Error, 'Need to pass dict'
        if len(data) == 2 and 'template' in data and 'package' in data:
            data.update(dict(owner='root', grp_owner='root', mode='0100644'))
        insert_data = {'trait' : self.current_trait}
        insert_data.update(data)
        id = self.textfiles.insert_file(templatefile)
        insert_data['templatefile'] = str(id)
        self.cmd.insert(data=insert_data)

    def update_template(self, data, templatefile):
        clause = self._clause(data['package'], data['template'])
        id = self.textfiles.insert_file(templatefile)
        fields = ['owner', 'grp_owner', 'mode']
        update = {}.fromkeys(fields)
        for field in update:
            update[field] = data[field]
        update['templatefile'] = str(id)
        self.cmd.update(data=update, clause=clause)
        self.reset_clause()

    def update_templatedata(self, package, template, data):
        clause = self._clause(package, template)
        id = self.textfiles.insert_data(data)
        self.cmd.update(data=dict(templatefile=str(id)), clause=clause)
        

    def drop_template(self, package, template):
        clause = self._clause(package, template)
        self._drop_template(clause)
        
    def _drop_template(self, clause):
        self.cmd.delete(clause=clause)
        self.reset_clause()

    def set_trait(self, trait):
        _TraitRelation.set_trait(self, trait)
        self.traitparent.set_trait(trait)
        self.template.set_trait(trait)
        
    def set_template(self, package, template):
        self.template.set_template(self.templatefile(package, template))
        self.template.update(self.traitparent.Environment())

    def set_template_path(self, path):
        self.template_path = join(path, self.suite, self.current_trait)
        
        
    def export_templates(self, bkup_path):
        makepaths(join(bkup_path, 'templates'))
        for t in self.templates():
            npath = join(bkup_path, 'templates', t.package, t.template)
            tfile = self.templatefile(t.package, t.template)
            makepaths(dirname(npath))
            filecopy(tfile, npath + '.template')
            tfile.close()
            
    def templatefile(self, package, template):
        return strfile(self.templatedata(package, template))
    
    def templatedata(self, package, template):
        return self._template_row(package, template).data

    def _template_row(self, package, template):
        table = self._jtable
        clause = self._clause(package, template)
        return self.cmd.select_row(fields=['data', 'templatefile'], table=table, clause=clause)

    def _template_id(self, package, template):
        return self._template_row(package, template).templatefile

    def remove_templatefile(self, package, template):
        print 'remove_templatefile deprecated'
        
    def save_template(self, package, template, templatefile):
        id = self.textfiles.insert_file(templatefile)
        clause = self._clause(package, template)
        self.cmd.update(data=dict(templatefile=str(id)), clause=clause)
class TraitTemplate(TraitRelation):
    def __init__(self, conn, suite):
        table = ujoin(suite, 'templates')
        TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
        self.traitparent = TraitParent(conn, suite)
        self.template = Template()
        self.template.set_suite(suite)
        self.template_path = None
        self.textfiles = TextFileManager(self.conn)
        self._jtable = '%s as s join textfiles as t ' % table
        self._jtable += 'on s.templatefile = t.fileid' 

    def _clause(self, template, trait=None):
        if trait is None:
            trait = self.current_trait
        return Eq('trait', trait) & Eq('template', template)
    
    def templates(self, trait=None, fields=['*']):
        if trait is None:
            trait = self.current_trait
            self.reset_clause()
            return self.cmd.select(fields=fields, order=['template'])
        else:
            self.set_clause(trait)
            rows = self.cmd.select(fields=fields, order=['template'])
            self.reset_clause()
            return rows

    def has_template(self, template):
        return self.has_it('template', template)

    def get_row(self, template):
        return TraitRelation.get_row(self, 'template', template)

    def insert_template(self, data, templatefile):
        if type(data) is not dict:
            raise Error, 'Need to pass dict'
        if len(data) == 2 and 'template' in data and 'package' in data:
            data.update(dict(owner='root', grp_owner='root', mode='0100644'))
        insert_data = {'trait' : self.current_trait}
        insert_data.update(data)
        txtid = self.textfiles.insert_file(templatefile)
        insert_data['templatefile'] = str(txtid)
        self.cmd.insert(data=insert_data)

    def insert_template_from_tarfile(self, template_path, tarfileobj):
        templatefile = tarfileobj.extractfile(template_path)
        info = tarfileobj.getmember(template_path)
        data = dict(owner=info.uname, grp_owner=info.gname,
                    mode=oct(info.mode), template=template_path)
        self.insert_template(data, templatefile)

    def update_template(self, template, data=None, templatefile=None,
                        contents=None):
        if templatefile is not None and contents is not None:
            raise RuntimeError, 'must either pass a file object or a string but not both'
        clause = self._clause(template)
        txtid = None
        if templatefile is not None:
            txtid = self.textfiles.insert_file(templatefile)
        if contents is not None:
            txtid = self.textfiles.insert_data(contents)
        update = {}
        if txtid is not None:
            update.update(dict(templatefile=str(txtid)))
        if data is not None:
            update.update(data)
        self.cmd.update(data=update, clause=clause)
        
    def convert_to_cheetah(self, template):
        text = self.templatedata(template)
        tmpl = convert_text_to_cheetah_template(text)
        sefl.update_template(self, template, data=str(tmpl))
        
        
    
    def update_templatedata(self, template, data):
        deprecated('update_templatedata is deprecated use update_template instead')
        self.update_template(template, contents=data)
        
        

    def drop_template(self, template):
        clause = self._clause(template)
        self._drop_template(clause)
        
    def _drop_template(self, clause):
        self.cmd.delete(clause=clause)
        self.reset_clause()

    def set_trait(self, trait):
        TraitRelation.set_trait(self, trait)
        self.traitparent.set_trait(trait)
        self.template.set_trait(trait)
        
    def set_template(self, template):
        self.template.set_template(self.templatefile(template))
        self.template.update(self.traitparent.Environment())

    def set_template_path(self, path):
        self.template_path = join(path, self.suite, self.current_trait)

        
        
    # set a keyword argument (to be removed sometime in the future)
    # to revert to previous way of naming template files.  The default is to
    # use the new method.
    def export_templates(self, bkup_path, numbered_templates=False):
        n = 0
        for t in self.templates():
            if numbered_templates:
                filename  = join(bkup_path, 'template-%d' % n)
            else:
                template_id = t.template.replace('/', '-slash-')
                filename = join(bkup_path, 'template-%s' % template_id)
            tfile = self.templatefile(t.template)
            filecopy(tfile, filename)
            tfile.close()
            n += 1
            
    def templatefile(self, template):
        return strfile(self.templatedata(template))
    
    def templatedata(self, template):
        return self._template_row(template).data

    def _template_row(self, template):
        table = self._jtable
        clause = self._clause(template)
        return self.cmd.select_row(fields=['data', 'templatefile'], table=table, clause=clause)

    def _template_id(self, template):
        return self._template_row(template).templatefile

    def save_template(self, template, templatefile):
        id = self.textfiles.insert_file(templatefile)
        clause = self._clause(template)
        self.cmd.update(data=dict(templatefile=str(id)), clause=clause)

    def edit_template(self, template):
        data = self.templatedata(template)
        tmp, path = tempfile.mkstemp('paella', 'template')
        tmp = file(path, 'w')
        tmp.write(data)
        tmp.close()
        os.system('$EDITOR %s' % path)
        tmp = file(path, 'r')
        mod = tmp.read()
        tmp.seek(0)
        if mod != data:
            print 'template modified'
            self.save_template(template, tmp)
        os.remove(path)
Beispiel #8
0
class TraitTemplate(_TraitRelation):
    def __init__(self, conn, suite):
        table = ujoin(suite, 'templates')
        _TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
        self.traitparent = TraitParent(conn, suite)
        self.template = Template()
        self.template.set_suite(suite)
        self.template_path = None

    def _clause(self, package, template, trait=None):
        if trait is None:
            trait = self.current_trait
        return Eq('trait', trait) & Eq('package', package) & Eq(
            'template', template)

    def templates(self, trait=None, fields=['*']):
        if trait is None:
            trait = self.current_trait
            self.reset_clause()
            return self.cmd.select(fields=fields,
                                   order=['package', 'template'])
        else:
            self.set_clause(trait)
            rows = self.cmd.select(fields=fields,
                                   order=['package', 'template'])
            self.reset_clause()
            return rows

    def has_template(self, template):
        return self.has_it('template', template)

    def get_row(self, template):
        return _TraitRelation.get_row(self, 'template', template)

    def insert_template(self, data, templatefile):
        if type(data) is not dict:
            raise Error, 'Need to pass dict'
        if len(data) == 2 and 'template' in data and 'package' in data:
            data.update(dict(owner='root', grp_owner='root', mode='0100644'))
        insert_data = {'trait': self.current_trait}
        insert_data.update(data)
        f = self.cmd.file(self.conn)
        insert_data['templatefile'] = f.name
        f.write(templatefile.read())
        f.close()
        self.cmd.insert(data=insert_data)

    def update_template(self, data, templatefile):
        clause = self._clause(data['package'], data['template'])
        fields = ['owner', 'grp_owner', 'mode']
        update = {}.fromkeys(fields)
        for field in update:
            update[field] = data[field]
        row = self.cmd.select_row(fields=['templatefile'], clause=clause)
        self.conn.removefile(row.templatefile.name)
        f = self.cmd.file(self.conn)
        update['templatefile'] = f.name
        f.write(templatefile.read())
        f.close()
        self.cmd.update(data=update, clause=clause)
        self.reset_clause()

    def drop_template(self, package, template):
        clause = self._clause(package, template)
        self._drop_template(clause)

    def _drop_template(self, clause):
        self.cmd.delete(clause=clause)
        self.reset_clause()

    def set_trait(self, trait):
        _TraitRelation.set_trait(self, trait)
        self.traitparent.set_trait(trait)
        self.template.set_trait(trait)

    def set_template(self, package, template):
        self.template.set_template(self.templatefile(package, template))
        self.template.update(self.traitparent.Environment())

    def set_template_path(self, path):
        self.template_path = join(path, self.suite, self.current_trait)

    def backup_templates(self, bkup_path):
        makepaths(join(bkup_path, 'templates'))
        for t in self.templates():
            npath = join(bkup_path, 'templates', t.package, t.template)
            tfile = self.templatefile(t.package, t.template)
            makepaths(dirname(npath))
            filecopy(tfile, npath + '.template')
            tfile.close()

    def templatefile(self, package, template):
        clause = self._clause(package, template)
        return self.cmd.open_file(self.conn, 'templatefile', clause)

    def remove_templatefile(self, package, template):
        clause = self._clause(package, template)
        self.cmd.delete_file(self.conn, 'templatefile', clause)

    def save_template(self, package, template, templatefile):
        clause = self._clause(package, template)
        self.cmd.update_file(self.conn, 'templatefile', clause, templatefile)
Beispiel #9
0
class TraitTemplate(_TraitRelation):
    def __init__(self, conn, suite):
        table = ujoin(suite, 'templates')
        _TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
        self.traitparent = TraitParent(conn, suite)
        self.template = Template()
        self.template.set_suite(suite)
        self.template_path = None
        self.textfiles = TextFileManager(self.conn)
        self._jtable = '%s as s join textfiles as t ' % table
        self._jtable += 'on s.templatefile = t.fileid' 

    def _clause(self, package, template, trait=None):
        if trait is None:
            trait = self.current_trait
        return Eq('trait', trait) & Eq('package', package) & Eq('template', template)
    
    def templates(self, trait=None, fields=['*']):
        if trait is None:
            trait = self.current_trait
            self.reset_clause()
            return self.cmd.select(fields=fields, order=['package', 'template'])
        else:
            self.set_clause(trait)
            rows = self.cmd.select(fields=fields, order=['package', 'template'])
            self.reset_clause()
            return rows

    def has_template(self, template):
        return self.has_it('template', template)

    def get_row(self, template):
        return _TraitRelation.get_row(self, 'template', template)

    def insert_template(self, data, templatefile):
        if type(data) is not dict:
            raise Error, 'Need to pass dict'
        if len(data) == 2 and 'template' in data and 'package' in data:
            data.update(dict(owner='root', grp_owner='root', mode='0100644'))
        insert_data = {'trait' : self.current_trait}
        insert_data.update(data)
        id = self.textfiles.insert_file(templatefile)
        insert_data['templatefile'] = str(id)
        self.cmd.insert(data=insert_data)

    def update_template(self, data, templatefile):
        clause = self._clause(data['package'], data['template'])
        id = self.textfiles.insert_file(templatefile)
        fields = ['owner', 'grp_owner', 'mode']
        update = {}.fromkeys(fields)
        for field in update:
            update[field] = data[field]
        update['templatefile'] = str(id)
        self.cmd.update(data=update, clause=clause)
        self.reset_clause()

    def update_templatedata(self, package, template, data):
        clause = self._clause(package, template)
        id = self.textfiles.insert_data(data)
        self.cmd.update(data=dict(templatefile=str(id)), clause=clause)
        

    def drop_template(self, package, template):
        clause = self._clause(package, template)
        self._drop_template(clause)
        
    def _drop_template(self, clause):
        self.cmd.delete(clause=clause)
        self.reset_clause()

    def set_trait(self, trait):
        _TraitRelation.set_trait(self, trait)
        self.traitparent.set_trait(trait)
        self.template.set_trait(trait)
        
    def set_template(self, package, template):
        self.template.set_template(self.templatefile(package, template))
        self.template.update(self.traitparent.Environment())

    def set_template_path(self, path):
        self.template_path = join(path, self.suite, self.current_trait)
        
        
    def export_templates(self, bkup_path):
        n = 0
        for t in self.templates():
            npath = join(bkup_path, 'template-%d' % n)
            tfile = self.templatefile(t.package, t.template)
            filecopy(tfile, npath)
            tfile.close()
            n += 1
            
    def templatefile(self, package, template):
        return strfile(self.templatedata(package, template))
    
    def templatedata(self, package, template):
        return self._template_row(package, template).data

    def _template_row(self, package, template):
        table = self._jtable
        clause = self._clause(package, template)
        return self.cmd.select_row(fields=['data', 'templatefile'], table=table, clause=clause)

    def _template_id(self, package, template):
        return self._template_row(package, template).templatefile

    def remove_templatefile(self, package, template):
        print 'remove_templatefile deprecated'
        
    def save_template(self, package, template, templatefile):
        id = self.textfiles.insert_file(templatefile)
        clause = self._clause(package, template)
        self.cmd.update(data=dict(templatefile=str(id)), clause=clause)

    def prefix_template(self, package, template, prefix='root/paella',
                        type_='real'):
        clause = self._clause(package, template)
        newname = os.path.join(prefix, '%s_templates' % type_ , template)
        data = dict(template=newname)
        self.cmd.update(data=data, clause=clause)
Beispiel #10
0
class TraitTemplate(TraitRelation):
    def __init__(self, conn, suite):
        table = ujoin(suite, 'templates')
        TraitRelation.__init__(self, conn, suite, table, name='TraitTemplate')
        self.traitparent = TraitParent(conn, suite)
        self.template = Template()
        self.template.set_suite(suite)
        self.template_path = None
        self.textfiles = TextFileManager(self.conn)
        self._jtable = '%s as s join textfiles as t ' % table
        self._jtable += 'on s.templatefile = t.fileid'

    def _clause(self, template, trait=None):
        if trait is None:
            trait = self.current_trait
        return Eq('trait', trait) & Eq('template', template)

    def templates(self, trait=None, fields=['*']):
        if trait is None:
            trait = self.current_trait
            self.reset_clause()
            return self.cmd.select(fields=fields, order=['template'])
        else:
            self.set_clause(trait)
            rows = self.cmd.select(fields=fields, order=['template'])
            self.reset_clause()
            return rows

    def has_template(self, template):
        return self.has_it('template', template)

    def get_row(self, template):
        return TraitRelation.get_row(self, 'template', template)

    def insert_template(self, data, templatefile):
        if type(data) is not dict:
            raise Error, 'Need to pass dict'
        if len(data) == 2 and 'template' in data and 'package' in data:
            data.update(dict(owner='root', grp_owner='root', mode='0100644'))
        insert_data = {'trait': self.current_trait}
        insert_data.update(data)
        txtid = self.textfiles.insert_file(templatefile)
        insert_data['templatefile'] = str(txtid)
        self.cmd.insert(data=insert_data)

    def insert_template_from_tarfile(self, template_path, tarfileobj):
        templatefile = tarfileobj.extractfile(template_path)
        info = tarfileobj.getmember(template_path)
        data = dict(owner=info.uname,
                    grp_owner=info.gname,
                    mode=oct(info.mode),
                    template=template_path)
        self.insert_template(data, templatefile)

    def update_template(self,
                        template,
                        data=None,
                        templatefile=None,
                        contents=None):
        if templatefile is not None and contents is not None:
            raise RuntimeError, 'must either pass a file object or a string but not both'
        clause = self._clause(template)
        txtid = None
        if templatefile is not None:
            txtid = self.textfiles.insert_file(templatefile)
        if contents is not None:
            txtid = self.textfiles.insert_data(contents)
        update = {}
        if txtid is not None:
            update.update(dict(templatefile=str(txtid)))
        if data is not None:
            update.update(data)
        self.cmd.update(data=update, clause=clause)

    def convert_to_cheetah(self, template):
        text = self.templatedata(template)
        tmpl = convert_text_to_cheetah_template(text)
        sefl.update_template(self, template, data=str(tmpl))

    def update_templatedata(self, template, data):
        deprecated(
            'update_templatedata is deprecated use update_template instead')
        self.update_template(template, contents=data)

    def drop_template(self, template):
        clause = self._clause(template)
        self._drop_template(clause)

    def _drop_template(self, clause):
        self.cmd.delete(clause=clause)
        self.reset_clause()

    def set_trait(self, trait):
        TraitRelation.set_trait(self, trait)
        self.traitparent.set_trait(trait)
        self.template.set_trait(trait)

    def set_template(self, template):
        self.template.set_template(self.templatefile(template))
        self.template.update(self.traitparent.Environment())

    def set_template_path(self, path):
        self.template_path = join(path, self.suite, self.current_trait)

    # set a keyword argument (to be removed sometime in the future)
    # to revert to previous way of naming template files.  The default is to
    # use the new method.
    def export_templates(self, bkup_path, numbered_templates=False):
        n = 0
        for t in self.templates():
            if numbered_templates:
                filename = join(bkup_path, 'template-%d' % n)
            else:
                template_id = t.template.replace('/', '-slash-')
                filename = join(bkup_path, 'template-%s' % template_id)
            tfile = self.templatefile(t.template)
            filecopy(tfile, filename)
            tfile.close()
            n += 1

    def templatefile(self, template):
        return strfile(self.templatedata(template))

    def templatedata(self, template):
        return self._template_row(template).data

    def _template_row(self, template):
        table = self._jtable
        clause = self._clause(template)
        return self.cmd.select_row(fields=['data', 'templatefile'],
                                   table=table,
                                   clause=clause)

    def _template_id(self, template):
        return self._template_row(template).templatefile

    def save_template(self, template, templatefile):
        id = self.textfiles.insert_file(templatefile)
        clause = self._clause(template)
        self.cmd.update(data=dict(templatefile=str(id)), clause=clause)

    def edit_template(self, template):
        data = self.templatedata(template)
        tmp, path = tempfile.mkstemp('paella', 'template')
        tmp = file(path, 'w')
        tmp.write(data)
        tmp.close()
        os.system('$EDITOR %s' % path)
        tmp = file(path, 'r')
        mod = tmp.read()
        tmp.seek(0)
        if mod != data:
            print 'template modified'
            self.save_template(template, tmp)
        os.remove(path)