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_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 highlightParagraph(self, text, endStateOfLastPara):
     text = str(text)
     template = Template()
     template.set_template(text)
     for span in template.spans():
         font = self.textEdit().currentFont()
         font.setBold(True)
         color =QColor('blue')
         length = span[1] - span[0]
         self.setFormat(span[0], length, font, color)
     return 0
Beispiel #3
0
 def highlightParagraph(self, text, endStateOfLastPara):
     text = str(text)
     template = Template()
     template.set_template(text)
     for span in template.spans():
         font = self.textEdit().currentFont()
         font.setBold(True)
         color = QColor('blue')
         length = span[1] - span[0]
         self.setFormat(span[0], length, font, color)
     return 0
 def __init__(self, name='TextTemplate'):
     TextBuffer.__init__(self)
     #self.set_name(name)
     self.__tagtable__ = self.get_tag_table()
     template_tag = _TemplateTag('template', foreground='magenta', scale=.85)
     self.__tagtable__.add(template_tag)
     self.__template__ = Template()
class TextTemplate(TextBuffer):
    def __init__(self, name='TextTemplate'):
        TextBuffer.__init__(self)
        #self.set_name(name)
        self.__tagtable__ = self.get_tag_table()
        template_tag = _TemplateTag('template', foreground='magenta', scale=.85)
        self.__tagtable__.add(template_tag)
        self.__template__ = Template()
        #StringTemplate('', delimiters=self.delimiters)
        

    def _remake_tag_(self, span):
        start, end = span
        tagtext = self.__template__.template[start:end]
        start, end = map(self.get_iter_at_offset, span)
        self.delete(start, end)
        self.insert_with_tags_by_name(start, tagtext, 'template')

    def position_tag(self):
        start, end = self.get_selection_bounds()
        self.__replace_pos__ = start, end

    def tag_slice(self):
        return self.get_slice(*self.__replace_pos__)

    def insert_tag(self, keytext):
        self.position_tag()
        start, end = self.__replace_pos__
        text = self.get_slice(start, end)
        self.delete(start, end)
        left, right = self.__template__.delimiters
        tag = ''.join([left, keytext, right])
        self.insert_with_tags_by_name(start, tag, 'template')
        self.__template__[keytext] = text
        self.reset_template()

    def set_text(self, text):
        TextBuffer.set_text(self, text)
        self.reset_template()

    def reset_template(self):
        text = get_buffer_text(self)
        self.__template__.set_template(text)
        map(self._remake_tag_, self.__template__.spans())
        
    def set_subs(self, datadict):
        self.__template__.clear()
        self.__template__.update(datadict)
        self.reset_template()

    def preview(self):
        return str(self.__template__.sub())

    def get_full_text(self):
        return self.get_text(*self.get_bounds())
Beispiel #6
0
 def tag_selection(self, trait, name):
     if not self.hasSelectedText():
         raise RuntimeError , 'we need selected text'
     template = Template()
     left, right = template.delimiters
     tagname = '_'.join([trait, name])
     tag = ''.join([left, tagname, right])
     pstart, istart, pend, iend = self.getSelection()
     self.removeSelectedText()
     self.insertAt(tag, pstart, istart)
 def __init__(self, data={}):
     _Template.__init__(self, data)
     self.template_path = None
 def set_template(self, templatefile):
     _Template.set_template(self, templatefile.read())
     templatefile.close()
Beispiel #9
0
 def __init__(self, data={}):
     deprecated("This Template class is completely deprecated.")
     _Template.__init__(self, data)
     self.template_path = None
Beispiel #10
0
 def __init__(self, data={}):
     _Template.__init__(self, data)
     self.template_path = None
Beispiel #11
0
 def set_template(self, templatefile):
     _Template.set_template(self, templatefile.read())
     templatefile.close()
Beispiel #12
0
 def __init__(self, data={}):
     deprecated("This Template class is completely deprecated.")
     _Template.__init__(self, data)
     self.template_path = None
Beispiel #13
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_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 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)
        
    def set_template(self, template):
        self.template.set_template(self.templatedata(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):
        for t in self.templates():
            template_id = t.template.replace('/', '-slash-')
            filename = join(bkup_path, 'template-%s' % template_id)
            tfile = self.templatefile(t.template)
            filecopy(tfile, filename)
            tfile.close()
            
    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)