home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / testpage < prev    next >
Encoding:
Text File  |  2006-08-30  |  7.7 KB  |  261 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2006 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__ = '4.0'
  24. __title__ = 'Testpage Print Utility'
  25. __doc__ = "Print a tespage to a printer. Prints a summary of device information and shows the printer's margins."
  26.  
  27. # Std Lib
  28. import sys
  29. import os
  30. import getopt
  31. import re
  32. import time
  33.  
  34. # Local
  35. from base.g import *
  36. from base import device, utils
  37. from prnt import cups
  38.  
  39. USAGE = [(__doc__, "", "name", True),
  40.          ("Usage: hp-testpage [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  41.          utils.USAGE_ARGS,
  42.          utils.USAGE_DEVICE,
  43.          utils.USAGE_PRINTER,
  44.          utils.USAGE_SPACE,
  45.          utils.USAGE_OPTIONS,
  46.          utils.USAGE_BUS1, utils.USAGE_BUS2,         
  47.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  48.          utils.USAGE_HELP,
  49.          utils.USAGE_SPACE,
  50.          utils.USAGE_NOTES,
  51.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  52.         ]
  53.  
  54. def usage(typ='text'):
  55.     if typ == 'text':
  56.         utils.log_title(__title__, __version__)
  57.         
  58.     utils.format_text(USAGE, typ, __title__, 'hp-testpage', __version__)
  59.     sys.exit(0)
  60.     
  61.  
  62. try:
  63.     opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:g',
  64.                                ['printer=', 'device=', 'help', 'help-rest', 
  65.                                 'help-man', 'logging=', 'bus='])
  66. except getopt.GetoptError:
  67.     usage()
  68.  
  69. printer_name = None
  70. device_uri = None
  71. bus = 'cups'
  72. log_level = logger.DEFAULT_LOG_LEVEL
  73.  
  74. if os.getenv("HPLIP_DEBUG"):
  75.     log.set_level('debug')
  76.  
  77. for o, a in opts:
  78.     if o in ('-h', '--help'):
  79.         usage()
  80.         
  81.     elif o == '--help-rest':
  82.         usage('rest')
  83.         
  84.     elif o == '--help-man':
  85.         usage('man')
  86.         
  87.     elif o in ('-p', '--printer'):
  88.         printer_name = a
  89.  
  90.     elif o in ('-d', '--device'):
  91.         device_uri = a
  92.  
  93.     elif o in ('-b', '--bus'):
  94.         bus = a.lower().strip()
  95.         if not device.validateBusList(bus):
  96.             usage()
  97.  
  98.     elif o in ('-l', '--logging'):
  99.         log_level = a.lower().strip()
  100.         if not log.set_level(log_level):
  101.             usage()
  102.             
  103.     elif o == '-g':
  104.         log.set_level('debug')
  105.         
  106.  
  107. if device_uri and printer_name:
  108.     log.error("You may not specify both a printer (-p) and a device (-d).")
  109.     usage()
  110.  
  111. if device_uri and printer_name:
  112.     log.error("You may not specify both a printer (-p) and a device (-d).")
  113.     usage()
  114.  
  115. utils.log_title(__title__, __version__)
  116.  
  117. if not device_uri and not printer_name:
  118.     try:
  119.         device_uri = device.getInteractiveDeviceURI(bus)
  120.         if device_uri is None:
  121.             sys.exit(0)
  122.     except Error:
  123.         log.error("Error occured during interactive mode. Exiting.")
  124.         sys.exit(0)
  125.  
  126. try:
  127.     d = device.Device(device_uri, printer_name)
  128. except Error, e:
  129.     log.error("Device error (%s)." % e.msg)
  130.     sys.exit(1)
  131.  
  132. if d.device_uri is None and printer_name:
  133.     log.error("Printer '%s' not found." % printer_name)
  134.     sys.exit(1)
  135.  
  136. if d.device_uri is None and device_uri:
  137.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  138.     sys.exit(1)
  139.  
  140.  
  141. try:
  142.     try:
  143.         d.open()
  144.     except Error:
  145.         log.error("Unable to print to printer. Please check device and try again.")
  146.         sys.exit(1)
  147.     
  148.     if len(d.cups_printers) == 0:
  149.         log.error("No printer queues found for device.")
  150.         sys.exit(1)
  151.         
  152.     elif len(d.cups_printers) > 1:
  153.         log.info("\nMultiple printers (queues) found in CUPS for device.")
  154.         log.info(utils.bold("\nPlease choose the printer (queue) to use for the test page:\n"))
  155.         
  156.         max_name = 24
  157.         for q in d.cups_printers:
  158.             max_name = max(max_name, len(q))
  159.             
  160.         formatter = utils.TextFormatter(
  161.             (
  162.                 {'width': 4, 'margin': 2},
  163.                 {'width': max_name, 'margin': 2},
  164.             )
  165.         )
  166.         
  167.         log.info(formatter.compose(("Num.", "CUPS printer (queue)")))
  168.         log.info(formatter.compose(('-'*4, '-'*(max_name))))
  169.         
  170.         x = 0
  171.         for q in d.cups_printers:
  172.             log.info(formatter.compose((str(x), d.cups_printers[x])))
  173.             x += 1
  174.             
  175.         while 1:
  176.             user_input = raw_input(utils.bold("\nEnter number 0...%d for printer (q=quit) ?" % (x-1)))
  177.  
  178.             if user_input == '':
  179.                 log.warn("Invalid input - enter a numeric value or 'q' to quit.")
  180.                 continue
  181.  
  182.             if user_input.strip()[0] in ('q', 'Q'):
  183.                 sys.exit(0)
  184.  
  185.             try:
  186.                 i = int(user_input)
  187.             except ValueError:
  188.                 log.warn("Invalid input - enter a numeric value or 'q' to quit.")
  189.                 continue
  190.  
  191.             if i < 0 or i > (x-1):
  192.                 log.warn("Invalid input - enter a value between 0 and %d or 'q' to quit." % (x-1))
  193.                 continue
  194.  
  195.             break
  196.             
  197.         printer_name = d.cups_printers[i]
  198.             
  199.     else:
  200.         printer_name = d.cups_printers[0]
  201.         
  202.     log.info("")
  203.     
  204.     if d.isIdleAndNoError():
  205.         d.close()
  206.         log.info( "Printing test page to printer %s..." % printer_name)
  207.         try:
  208.             d.printTestPage(printer_name)
  209.         except Error, e:
  210.             if e.opt == ERROR_NO_CUPS_QUEUE_FOUND_FOR_DEVICE:
  211.                 log.error("No CUPS queue found for device. Please install the printer in CUPS and try again.")
  212.             else:
  213.                 log.error("An error occured (code=%d)." % e.opt)
  214.         else:
  215.             log.info("Test page has been sent to printer. Waiting for printout to complete...")
  216.             
  217.             time.sleep(5)
  218.             i = 0
  219.  
  220.             while True:
  221.                 time.sleep(5)
  222.                 
  223.                 try:
  224.                     d.queryDevice(quick=True)
  225.                 except Error, e:
  226.                     log.error("An error has occured.")
  227.                 
  228.                 if d.error_state == ERROR_STATE_CLEAR:
  229.                     break
  230.                 
  231.                 elif d.error_state == ERROR_STATE_ERROR:
  232.                     log.error("An error has occured (code=%d). Please check the printer and try again." % d.status_code)
  233.                     break
  234.                     
  235.                 elif d.error_state == ERROR_STATE_WARNING:
  236.                     log.warning("There is a problem with the printer (code=%d). Please check the printer." % d.status_code)
  237.                 
  238.                 else: # ERROR_STATE_BUSY
  239.                     update_spinner()
  240.                     
  241.                 i += 1
  242.                 
  243.                 if i > 24:  # 2min
  244.                     break
  245.  
  246.         #sys.exit(0)
  247.     else:
  248.         log.error("Device is busy or in an error state. Please check device and try again.")
  249.         sys.exit(1)
  250.         
  251.         
  252. finally:
  253.     d.close()
  254.     
  255.     log.info("")
  256.     log.info(utils.red("If an error occured, or the test page failed to print, refer to the HPLIP website"))
  257.     log.info(utils.red("at: http://hplip.sourceforge.net for troubleshooting and support."))
  258.     log.info("")
  259.  
  260. log.info("Done.")
  261.