Aggiunta gestione degli errori di I/O

Leonardo Robol [2009-09-20 13:46]
Aggiunta gestione degli errori di I/O
Filename
DrPrintBackend.py
DrPrintGui/MainWin.py
DrPrintGui/MainWin.pyc
diff --git a/DrPrintBackend.py b/DrPrintBackend.py
index 12944d4..3438add 100644
--- a/DrPrintBackend.py
+++ b/DrPrintBackend.py
@@ -9,6 +9,7 @@ class Backend(gobject.GObject):
         super(Backend, self).__init__()

         gobject.signal_new("auth_failed", Backend, gobject.SIGNAL_RUN_FIRST, None, ())
+        gobject.signal_new('io_error', Backend, gobject.SIGNAL_RUN_FIRST, None, ())

     def send_print(self, printer, username, password, page_per_page, filename, page_range):
         # Get printer name
@@ -30,7 +31,12 @@ class Backend(gobject.GObject):
         channel = client.get_transport().open_session()

         print "Printing %s" % filename
-        f = open(filename, 'r')
+
+        try:
+            f = open(filename, 'r')
+        except IOError:
+            self.emit('io_error')
+            return

         # Questo è inevitabile.. :)
         cmd = "lpr -P%s " % printer
@@ -51,8 +57,20 @@ class Backend(gobject.GObject):

         ## Diamo il comando sul canale e infiliamo il file
         ## dentro lo stdin :)
+        print "Eseguo %s" % cmd
+
         channel.exec_command(cmd)
-        channel.sendall( f.read() )
+        try:
+            content = f.read()
+        except IOError:
+            self.emit('io_error')
+            return
+
+        try:
+            channel.sendall( content )
+        except socket.timeout, socket.error:
+            self.emit('io_error')
+            return
         f.close()
         channel.close()

diff --git a/DrPrintGui/MainWin.py b/DrPrintGui/MainWin.py
index 5c0a385..282f2ec 100644
--- a/DrPrintGui/MainWin.py
+++ b/DrPrintGui/MainWin.py
@@ -86,6 +86,7 @@ class MainWin(gtk.Window):
     def connect_all(self):
         self.print_button.connect('clicked', self.print_button_clicked_callback)
         self.backend.connect('auth_failed', self.auth_failed_callback)
+        self.backend.connect('io_error', self.io_error_callback)

     def print_button_clicked_callback(self, widget):
         if not self.backend == None:
@@ -115,6 +116,17 @@ class MainWin(gtk.Window):
 corretti. L'autenticazione su ssh.dm.unipi.it\nnon è andata a buon fine.")
         dialog.run()
         dialog.destroy()
+
+    def io_error_callback(self, obj):
+
+        self.debug("Errore di I/O")
+        dialog = ErrorDialog("Errore di I/O",
+                             "<b>Errore di I/O</b>\n\
+C'è stato un errore nella lettura o nella\n \
+trasmissione del file.")
+
+        dialog.run()
+        dialog.destroy()



diff --git a/DrPrintGui/MainWin.pyc b/DrPrintGui/MainWin.pyc
index 6f72302..ffaef11 100644
Binary files a/DrPrintGui/MainWin.pyc and b/DrPrintGui/MainWin.pyc differ
ViewGit