pgtoolkit/pgdatadiff.py
changeset 101 2a2d0d5df03b
parent 83 515fadd3d286
--- a/pgtoolkit/pgdatadiff.py	Tue May 06 18:37:41 2014 +0200
+++ b/pgtoolkit/pgdatadiff.py	Tue May 06 18:37:43 2014 +0200
@@ -28,6 +28,8 @@
 from pgtoolkit import pgbrowser
 from pycolib.ansicolor import *
 
+import sys
+
 
 class DiffData:
     COLORS = {
@@ -38,14 +40,14 @@
         'K' : BOLD | BLUE}
 
     def __init__(self, change, cols1, cols2, key=None):
-        '''
+        """
 
         change - one of '+', '-', '*' (add, remove, update)
         cols1 - original column values (OrderedDict)
         cols2 - new column values (OrderedDict)
         key - primary key columns (OrderedDict)
 
-        '''
+        """
         self.change = change
         self.cols1 = cols1
         self.cols2 = cols2
@@ -155,11 +157,11 @@
         self.fulltable2 = '"' + schema + '"."'+ table + '"'
 
     def iter_diff(self):
-        '''Return differencies between data of two tables.
+        """Return differencies between data of two tables.
 
         Yields one line at the time.
 
-        '''
+        """
         curs1, curs2 = self._select()
 
         row1 = curs1.fetchone_dict()
@@ -186,25 +188,25 @@
         curs1.close()
         curs2.close()
 
-    def print_diff(self):
-        '''Print differencies between data of two tables.
+    def print_diff(self, file=sys.stdout):
+        """Print differencies between data of two tables.
 
         The output is in human readable form.
 
         Set allowcolor=True of PgDataDiff instance to get colored output.
 
-        '''
+        """
         for ln in self.iter_diff():
-            print(ln.format())
+            print(ln.format(), file=file)
 
-    def print_patch(self):
-        '''Print SQL script usable as patch for destination table.
+    def print_patch(self, file=sys.stdout):
+        """Print SQL script usable as patch for destination table.
 
         Supports INSERT, DELETE and UPDATE operations.
 
-        '''
+        """
         for ln in self.iter_diff():
-            print(ln.format_patch(self.fulltable1))
+            print(ln.format_patch(self.fulltable1), file=file)
 
     def _select(self):
         browser = pgbrowser.PgBrowser(self.conn1)