merge with master
[platform/upstream/hplip.git] / testpage.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # (c) Copyright 2003-2008 Hewlett-Packard Development Company, L.P.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 # Author: Don Welch
21 #
22
23 __version__ = '6.0'
24 __title__ = 'Testpage Print Utility'
25 __mod__ = 'hp-testpage'
26 __doc__ = "Print a tespage to a printer. Prints a summary of device information and shows the printer's margins."
27
28 # Std Lib
29 import sys
30 import os
31 import getopt
32 import re
33 import time
34
35 # Local
36 from base.g import *
37 from base import device, utils, tui, module
38 from prnt import cups
39
40
41
42 try:
43     mod = module.Module(__mod__, __title__, __version__, __doc__, None,
44                         (INTERACTIVE_MODE, GUI_MODE),
45                         (UI_TOOLKIT_QT4,))
46
47     mod.setUsage(module.USAGE_FLAG_DEVICE_ARGS)
48
49     opts, device_uri, printer_name, mode, ui_toolkit, loc = \
50         mod.parseStdOpts()
51
52     printer_name, device_uri = mod.getPrinterName(printer_name, device_uri)
53     wait_for_printout = False
54
55     if mode == GUI_MODE:
56         if not utils.canEnterGUIMode4():
57             log.error("%s -u/--gui requires Qt4 GUI support. Entering interactive mode." % __mod__)
58             mode = INTERACTIVE_MODE
59
60     if mode == GUI_MODE:
61         try:
62             from PyQt4.QtGui import QApplication
63             from ui4.printtestpagedialog import PrintTestPageDialog
64         except ImportError:
65             log.error("Unable to load Qt4 support. Is it installed?")
66             sys.exit(1)
67
68         log.set_module("%s(UI)" % __mod__)
69
70         if 1:
71             app = QApplication(sys.argv)
72
73             dialog = PrintTestPageDialog(None, printer_name)
74             dialog.show()
75             try:
76                 log.debug("Starting GUI loop...")
77                 app.exec_()
78             except KeyboardInterrupt:
79                 sys.exit(0)
80
81         sys.exit(0)
82
83     if mode == INTERACTIVE_MODE:
84     #else: # INTERACTIVE_MODE
85         try:
86             d = device.Device(device_uri, printer_name)
87         except Error, e:
88             log.error("Device error (%s)." % e.msg)
89             sys.exit(1)
90
91         try:
92             try:
93                 d.open()
94             except Error:
95                 log.error("Unable to print to printer. Please check device and try again.")
96                 sys.exit(1)
97
98             # TODO: Fix the wait for printout stuff... can't get device ID
99             # while hp: backend has device open in printing mode...
100             wait_for_printout = False
101
102             if d.isIdleAndNoError():
103                 d.close()
104                 log.info( "Printing test page to printer %s..." % printer_name)
105                 try:
106                     d.printTestPage(printer_name)
107                 except Error, e:
108                     if e.opt == ERROR_NO_CUPS_QUEUE_FOUND_FOR_DEVICE:
109                         log.error("No CUPS queue found for device. Please install the printer in CUPS and try again.")
110                     else:
111                         log.error("An error occured (code=%d)." % e.opt)
112                 else:
113                     if wait_for_printout:
114                         log.info("Test page has been sent to printer. Waiting for printout to complete...")
115
116                         time.sleep(5)
117                         i = 0
118
119                         while True:
120                             time.sleep(5)
121
122                             try:
123                                 d.queryDevice(quick=True)
124                             except Error, e:
125                                 log.error("An error has occured.")
126
127                             if d.error_state == ERROR_STATE_CLEAR:
128                                 break
129
130                             elif d.error_state == ERROR_STATE_ERROR:
131                                 cleanup_spinner()
132                                 log.error("An error has occured (code=%d). Please check the printer and try again." % d.status_code)
133                                 break
134
135                             elif d.error_state == ERROR_STATE_WARNING:
136                                 cleanup_spinner()
137                                 log.warning("There is a problem with the printer (code=%d). Please check the printer." % d.status_code)
138
139                             else: # ERROR_STATE_BUSY
140                                 update_spinner()
141
142                             i += 1
143
144                             if i > 24:  # 2min
145                                 break
146
147                         cleanup_spinner()
148
149                     else:
150                         log.info("Test page has been sent to printer.")
151
152             else:
153                 log.error("Device is busy or in an error state. Please check device and try again.")
154                 sys.exit(1)
155
156
157         finally:
158             d.close()
159
160             log.info("")
161             log.notice("If an error occured, or the test page failed to print, refer to the HPLIP website")
162             log.notice("at: http://hplip.sourceforge.net for troubleshooting and support.")
163             log.info("")
164
165 except KeyboardInterrupt:
166     log.error("User exit")
167
168 log.info("")
169 log.info("Done.")