Logo Search packages:      
Sourcecode: umit version File versions  Download package

DiffHtml.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005-2006 Insecure.Com LLC.
# Copyright (C) 2007-2008 Adriano Monteiro Marques
#
# Author: Adriano Monteiro Marques <adriano@umitproject.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


from difflib import HtmlDiff, restore
from Diff import Diff
from xml.dom import minidom

from umitCore.I18N import _

class DiffHtml (HtmlDiff):
    style = '''
    table.diff {
        font-family:Courier; 
        font-size:13px; 
        border:medium; 
        border-top:1px solid; 
        border-bottom:1px solid; 
        border-left:1px solid; 
        border-right:1px solid; 
        background: #ececec;
    }
    body {
    background: #b1b1b1;
    }
    .diff_header {
        background-color:#e0e0e0;
        padding-left:2px;
        padding-right:2px;
    }
    td.diff_header {
        text-align:right;
        padding-left:2px;
        padding-right:2px;
    }
    .diff_next {
        background-color:#c0c0c0;
    }
    .diff_add {
        background-color:#aaffaa;
    }
    .diff_chg {
        background-color:#ffff77;
    }
    .diff_sub {
        background-color:#ffaaaa;
    }
    .umit {
        background: #4e3f8c;
        font-weight:bold;
        font-family:Helvetica,Arial,sans-serif;
        font-size: 20px;
        color: #ececec;
        width:100%;
        border-top:1px solid;
        border-bottom:1px solid;
        border-left:1px solid;
        border-right:1px solid;
        padding-top:3px;
        padding-bottom:2px;
        padding-left:2px;
        padding-right:2px;
    }
    .normal_text {
        font-size:16px;
    }
    
    a:hover {
        text-decoration:none;
        color: black;
        font-family: Helvetica,Arial,sans-serif;
        font-weight:bold;
    }
    
    a:link {
        text-decoration:none;
        color: black;
        font-family: Helvetica,Arial,sans-serif;
        font-weight:bold;
    }
    
    a:active {
        text-decoration:none;
        color: black;
        font-family: Helvetica,Arial,sans-serif;
        font-weight:bold;
    }
    
    a:visited {
        text-decoration:none;
        color: black;
        font-family: Helvetica,Arial,sans-serif;
        font-weight:bold;
    }
    pre {
        background: white;
        border-top:1px solid;
        border-bottom:1px solid;
        border-left:1px solid;
        border-right:1px solid;
        padding-top:3px;
        padding-bottom:2px;
        padding-left:2px;
        padding-right:2px;
    }
    '''
    
    header = '''
    <center>
        <div class="umit" align="center">
            %s<br />
            <a href="http://www.umitproject.org">http://www.umitproject.org</a>
            <p class="normal_text">%s</p>
        </div>
    </center>
    <br /><br />''' % (_("Umit - Take the red pill"), \
                       _("This diff was generated by Umit"))
    
    def __init__ (self, result1=[''], result2=[''], \
                  column=4, wrap=50, junk='\n'):
        
        HtmlDiff.__init__ (self, column, wrap, self.line_junk)
        
        self.result1 = result1
        self.result2 = result2
        self.junk = junk
    
    def generate (self):
        self.html_file = self.make_file (self.result1, self.result2)
        self.html_file = ' '.join(self.html_file.split('&nbsp;'))
        
        txt_diff = Diff (self.result1, self.result2, self.junk)
        self.text_file = '''<br /><br />
        <!-- %s -->
        <center><div class="umit" align="center">%s</div></center>
        <pre align="center">%s</pre>
        '''% (_("Changes to this file can make Umit unable to read it."),\
              _('Regular Text Diff: '),\
              ''.join(txt_diff.generate ()))
        
        return self.insert_banner ()
    
    def save(self, file):
        try:
            self.html_file
        except:
            self.generate()

        save_desc = open(file,'w')
        save_desc.write(self.insert_banner())

        save_desc.close()
    
    def open (self, file):
        html_desc = open(file)
        html = html_desc.read()
        html_desc.close()
        
        xml = minidom.parseString(html)
        pre = xml.getElementsByTagName ('pre')[0].firstChild.data
        
        diff = Diff ()
        return diff.restore (pre)
    
    def line_junk (self, junk):
        if junk == self.junk:
            return True
        else:
            return False
    
    def insert_banner (self):
        '''insert_banner ()
        
        Insert Umit style, header and stuff into de html file and return it.
        This methodd must not be called directily, once that the html file must
        be created first, is better to call generate or save methods.
        '''
        xml = minidom.parseString (self.html_file)
        
        xml.getElementsByTagName ('style')[0].firstChild.data = self.style
        tables = xml.getElementsByTagName ('table')
        
        for table in tables[:]:
            if table.getAttribute ('class') == 'diff':
                table.setAttribute ('align', 'center')
            else:
                table.setAttribute ('border', '0')
        
        xml = xml.toxml().split ('\n')
        try:
            xml.insert (xml.index('<body>')+1, self.header)
            xml.insert (xml.index('    </table>')+1, '<br />')
            xml.insert (xml.index('</body>'), self.text_file)
        except:
            xml.insert (1, self.header)
            xml.insert (len(xml), self.text_file)
        
        return '\n'.join(xml)

if __name__ == '__main__':
    r1 = open ('/tmp/tmpaVzZU9').readlines()
    r2 = open ('/tmp/tmpDdUaIU').readlines()
    d = DiffHtml (r1, r2)
    
    d.generate ()
    d.save ('/tmp/teste.html')
    #print '\n'.join(d.open ('/tmp/teste.html')[0])
    #print '\n'.join(d.open ('/tmp/teste.html')[1])

Generated by  Doxygen 1.6.0   Back to index