Cambio radicale nel funzionamento. Ora il file viene trasferito sul

Leonardo Robol [2010-01-29 09:37]
Cambio radicale nel funzionamento. Ora il file viene trasferito sul
server e poi stampato.
Filename
DrPrintBackend.py
DrPrintGui/Dialogs.py
DrPrintGui/Input.py
DrPrintGui/MainWin.py
debian/changelog
diff --git a/DrPrintBackend.py b/DrPrintBackend.py
index 190f5f2..0eb943d 100644
--- a/DrPrintBackend.py
+++ b/DrPrintBackend.py
@@ -1,7 +1,15 @@
 ## Some useful function to help DrPrint to
 # -*- coding: utf-8 -*-

-import paramiko, gobject
+import paramiko, gobject, select, time
+
+class PrintingError(Exception):
+
+    def __init__(self, value):
+        self.value = value
+
+    def __str__(self):
+        return repr(self.value)

 class Backend(gobject.GObject):

@@ -16,8 +24,11 @@ class Backend(gobject.GObject):
         print "Selected printer: %s" % printer

         # Get connection
-        client = paramiko.SSHClient()
-        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+        try:
+            client = paramiko.SSHClient()
+            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+        except:
+            raise PrintingError('Impossibili inizializzare paramiko')

         try:
             client.connect('ssh.dm.unipi.it',
@@ -27,22 +38,21 @@ class Backend(gobject.GObject):
         except paramiko.AuthenticationException, e:
             self.emit('auth_failed')
             return
-
-        channel = client.get_transport().open_session()
+
+        t = client.get_transport()
+        sftp = paramiko.SFTPClient.from_transport(t)

         print "Printing %s" % filename

-        try:
-            f = open(filename, 'r')
-        except IOError:
-            self.emit('io_error')
-            return
-
         # Questo è inevitabile.. :)
         cmd = "lpr -P%s " % printer

-        # Nunmero di pagine
-        if copies.isdigit():
+        # Numero di pagine
+        try:
+            copies = int(float(copies))
+        except ValueError:
+            copies = 1
+        if copies is not 1:
             cmd = cmd + "-# %s " % copies


@@ -65,26 +75,24 @@ class Backend(gobject.GObject):

         ## Se ci sono opzioni dai il -o e specificale
         if not cmd_opts == "":
-            cmd = cmd + "%s" % cmd_opts
+            cmd = cmd + "%s" % cmd_opts + " /tmp/drprint_tmp_%s" % username

-
-        ## Diamo il comando sul canale e infiliamo il file
-        ## dentro lo stdin :)
-        print "Eseguo %s" % cmd
+        sftp.put(filename, "/tmp/drprint_tmp_%s" % username)

-        channel.exec_command(cmd)
-        try:
-            content = f.read()
-        except IOError:
-            self.emit('io_error')
-            return
+        # Aspettiamo che il trasferimento avvenga, appena trovo
+        # un metodo serio per farlo rimuovo questo time.sleep()
+        time.sleep(1)

-        try:
-            channel.sendall( content )
-        except socket.timeout, socket.error:
-            self.emit('io_error')
-            return
-        f.close()
-        channel.close()
+        chan = t.open_session()
+
+        # Diamo il comando sul canale
+        print "Eseguo %s" % cmd
+        chan.exec_command(cmd)
+        chan.close()
+        exit_status = chan.recv_exit_status()
+
+        sftp.remove("/tmp/drprint_tmp_%s" % username)

-        print "Printed %s on %s" % (filename, printer)
+        print "Printed %s on %s (exit status = %d)" % (filename, printer, exit_status)
+        if exit_status != 0:
+            raise PrintingError('Il comando <b>lpr</b> non e\' andato a buon fine (Exit status = %d)' % exit_status)
diff --git a/DrPrintGui/Dialogs.py b/DrPrintGui/Dialogs.py
index e3c2e88..d37ea98 100644
--- a/DrPrintGui/Dialogs.py
+++ b/DrPrintGui/Dialogs.py
@@ -22,6 +22,19 @@ class ErrorDialog(Dialog):

         self.set_markup(error)
         self.format_secondary_markup(message)
+
+class InfoDialog(Dialog):
+
+    def __init__(self, error, message):
+
+        Dialog.__init__(self,
+                        buttons = gtk.BUTTONS_OK,
+                        mtype = gtk.MESSAGE_INFO
+                        )
+
+        self.set_markup(error)
+        self.format_secondary_markup(message)
+


 class MessageDialog(Dialog):
diff --git a/DrPrintGui/Input.py b/DrPrintGui/Input.py
index b008115..4ff017e 100644
--- a/DrPrintGui/Input.py
+++ b/DrPrintGui/Input.py
@@ -44,7 +44,7 @@ class PasswordField(gtk.Entry):

         gtk.Entry.__init__(self)

-        self.set_text ("Password")
+        self.set_text ("")
         self.set_visibility(False)

 class AuthBlock(gtk.HBox):
diff --git a/DrPrintGui/MainWin.py b/DrPrintGui/MainWin.py
index f6b0827..6b342e2 100644
--- a/DrPrintGui/MainWin.py
+++ b/DrPrintGui/MainWin.py
@@ -12,7 +12,8 @@ import os
 import sys

 from Input import AuthBlock, PrinterSettingsBlock, PrintButton, LeftAlignedLabel, PageRangeBlock, OrientationSelect, SidesSelect
-from Dialogs import ErrorDialog, MessageDialog
+from Dialogs import ErrorDialog, MessageDialog, InfoDialog
+from DrPrintBackend import PrintingError

 class MainWin(gtk.Window):
     """MainWin object for DrPrint"""
@@ -23,7 +24,7 @@ class MainWin(gtk.Window):

         gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)

-        self.set_title = "DrPrint 0.2"
+        self.set_title = "DrPrint 0.8"
         self.set_border_width(10)

         self.default_spacing = 5
@@ -151,15 +152,28 @@ Se vuoi continuare premi OK")


             if resp == gtk.RESPONSE_OK:
-                self.backend.send_print(printer = printer,
-                                        username = username,
-                                        password = password,
-                                        filename = filename,
-                                        page_per_page = page_per_page,
-                                        page_range = page_range,
-                                        copies = copies,
-                                        orientation=orientation,
-                                        sides = sides)
+                try:
+                    self.backend.send_print(printer = printer,
+                                            username = username,
+                                            password = password,
+                                            filename = filename,
+                                            page_per_page = page_per_page,
+                                            page_range = page_range,
+                                            copies = copies,
+                                            orientation=orientation,
+                                            sides = sides)
+                except PrintingError, e:
+                    # Comunichiamo il fallimento
+                    dialog = ErrorDialog("Errore di stampa",
+                                         "Il seguente errore si è verificato durante la stampa: %s." % e)
+                    dialog.run()
+                    dialog.destroy()
+                else:
+                    dialog = InfoDialog("Stampa effettuata",
+                                        "Il file %s è stato stampato correttamente sulla stampante <b>%s</b>."
+                                        % (filename, printer))
+                    dialog.run()
+                    dialog.destroy()
         else:
             self.debug( "Sembra che non ci sia un backend attaccato\
  a questa interfaccia, quindi non faccio nulla")
diff --git a/debian/changelog b/debian/changelog
index 75fb1e6..12b7f03 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,15 @@
+drprint (0.7-5) karmic; urgency=low
+
+  * Aggiunto try: except: per essere più sicuri.
+
+ -- Leonardo Robol <leonardo@debby>  Thu, 17 Dec 2009 16:56:36 +0100
+
+drprint (0.7-4) karmic; urgency=low
+
+  * Corretto bug nell'impostazione nel numero di copie.
+
+ -- Leonardo Robol <leonardo@debby>  Thu, 17 Dec 2009 16:51:42 +0100
+
 drprint (0.7-3) karmic; urgency=low

   * Corretto bug nel pulsante stampa
ViewGit