# Download .exe file at http://cechvala.byethost31.com/projects/comparator.zip # This is a simple tool for comparing text lines in two selected files and writting down the result into output file. # Output shows which lines from first file are missing in the other one and vice versa # 2014 beta P. Cechvala # 2015 v1.0 P.Cechvala : added time stamp, about(toolbar), auto log scrolling, bugfixes import gtk import easygui import os import datetime def refresh_time(): global ts ts = datetime.datetime.now().strftime("%H:%M:%S : ") # One may why I'm calling refresh_time() individually on every log action, and why don't I use # gtk.timeout_add and refresh time every second. Well, it was throwing this error: #__call__ (method from deprecation.py) # raise TypeError(str(e).replace(func.__name__, self.oldname)) #TypeError: second argument not callable # and I could not fix it...if you now how, let me know :) ..after this I'm done with gtk2 # Selecting I/O files def select1(widget): global f1 f1 = easygui.fileopenbox() refresh_time() buffer1.insert_at_cursor(ts+f1 +" selected for file1 " + "\n",len(ts)+len(f1) +21) def select2(widget): global f2 f2 = easygui.fileopenbox() refresh_time() buffer1.insert_at_cursor(ts+f2 +" selected for file2 " + "\n",len(ts)+len(f2) +21) def select3(widget): global f3 f3 = easygui.fileopenbox() refresh_time() buffer1.insert_at_cursor(ts+f3 +" selected for output file " + "\n",len(ts)+len(f3) +27) # The comparator itself def comparator(widget): refresh_time() try: len(f1) except (TypeError, NameError): buffer1.insert_at_cursor(ts+"error: file1 not selected" + "\n",len(ts)+26) try: len(f2) except (TypeError, NameError): buffer1.insert_at_cursor(ts+"error: file2 not selected" + "\n",len(ts)+26) try: len(f3) except (TypeError, NameError): buffer1.insert_at_cursor(ts+"error: output not selected" + "\n",len(ts)+27) with open(f1, 'r') as file1: with open(f2, 'r') as file2: diff = set(file1).difference(file2) f1name = os.path.basename(f1) f2name = os.path.basename(f2) with open(f3, 'w') as FO: FO.write("\n" + "Lines present in " + f1name +" and missing in " + f2name + "\n" ) for line in diff: FO.write(line) with open(f1, 'r') as file1: with open(f2, 'r') as file2: over = set(file2).difference(file1) with open(f3, 'a') as FO: FO.write("\n" + "Lines present in " + f2name +" and missing in " + f1name + "\n" ) for line in over: FO.write(line) buffer1.insert_at_cursor(ts+"comparison done" + "\n",len(ts)+16) #window win = gtk.Window() win.connect('destroy', lambda w: gtk.main_quit()) box = gtk.VBox() win.add(box) win.set_default_size(350, 300) #scrollable window inside box scrolled_window = gtk.ScrolledWindow() scrolled_window.set_border_width(10) scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS) win.add(scrolled_window) # text log field log = gtk.TextView() log.set_editable(False) log.set_wrap_mode(gtk.WRAP_CHAR) log.set_cursor_visible(True) #buffer1= gtk.TextBuffer() buffer1=log.get_buffer() gtk.TextMark.set_visible # auto scrolling to the bottom of TextView def scroll_down(smthg): adj = scrolled_window.get_vadjustment() adj.set_value( adj.upper) buffer1.connect("changed",scroll_down) #table with buttons table = gtk.Table(1,1, gtk.TRUE) button1 = gtk.Button("load file1") button2 = gtk.Button("load file2") button3 = gtk.Button("output file") button4 = gtk.Button("run comparison") button1.connect("clicked",select1) button2.connect("clicked",select2) button3.connect("clicked",select3) button4.connect("clicked",comparator,) table.attach(button1,0,1,0,1) table.attach(button2,1,2,0,1) table.attach(button3,0,1,1,2) table.attach(button4,1,2,1,2) # About dialog def show_dialog(self): dialog = gtk.MessageDialog(win, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_NONE, "This is a simple tool for comparing text lines in two selected files and writting down the result into output file. Output shows which lines from first file are missing in the other one and vice versa. \n \n View, alter or redistribute source code as you wish. \n \n Version 1.0 \n 2015 P. Cechvala ") dialog.run() dialog.destroy() # Toolbar toolbar = gtk.Toolbar() toolbar.append_item("About", "about comparator", "tooltip_private_text", None, show_dialog, None) # can't have 2 children in gtk.window or ScrolledWindow, # but you can "pack" them inside box, if you tell them rules #(so they don't fight too much :)) box.pack_start(toolbar, expand=False, fill=True, padding=0) box.pack_start(table,False) box.pack_end(scrolled_window,True) scrolled_window.add(log) log.show() win.show_all() gtk.main()