home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / clean < prev    next >
Encoding:
Text File  |  2006-08-30  |  5.6 KB  |  201 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.  
  24. __version__ = '1.6'
  25. __title__ = 'Printer Cartridge Cleaning Utility'
  26. __doc__ = "Cartridge cleaning utility for HPLIP supported inkjet printers."
  27.  
  28. #Std Lib
  29. import sys
  30. import re
  31. import getopt
  32.  
  33. # Local
  34. from base.g import *
  35. from base import device, utils, maint
  36. from prnt import cups
  37.  
  38. USAGE = [(__doc__, "", "name", True),
  39.          ("Usage: hp-clean [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  40.          utils.USAGE_ARGS,
  41.          utils.USAGE_DEVICE,
  42.          utils.USAGE_PRINTER,
  43.          utils.USAGE_SPACE,
  44.          utils.USAGE_OPTIONS,
  45.          ("Cleaning level:", "-v<level> or --level=<level>", "option", False),
  46.          ("", "<level>: 1\*, 2, or 3 (\*default)", "option", False),
  47.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  48.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  49.          utils.USAGE_HELP,
  50.          utils.USAGE_EXAMPLES,
  51.          ("""Clean CUPS printer named 'hp5550':""", """$ hp-clean -php5550""",  "example", False),
  52.          ("""Clean printer with URI of 'hp:/usb/DESKJET_990C?serial=12345':""", """$ hp-clean -dhp:/usb/DESKJET_990C?serial=12345""", 'example', False),
  53.          utils.USAGE_SPACE,
  54.          utils.USAGE_NOTES,
  55.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  56.          utils.USAGE_SEEALSO,
  57.          ("hp-align", "", "seealso", False),
  58.          ("hp-colorcal", "", "seealso", False),
  59.          ]
  60.          
  61. def usage(typ='text'):
  62.     if typ == 'text':
  63.         utils.log_title(__title__, __version__)
  64.         
  65.     utils.format_text(USAGE, typ, __title__, 'hp-clean', __version__)
  66.     sys.exit(0)
  67.  
  68.  
  69. try:
  70.     opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:v:g',
  71.                                 ['printer=', 'device=', 'help', 'help-rest', 'help-man', 
  72.                                  'logging=', 'bus=', 'level='])
  73. except getopt.GetoptError:
  74.     usage()
  75.  
  76. bus = device.DEFAULT_PROBE_BUS
  77. log_level = logger.DEFAULT_LOG_LEVEL
  78. printer_name = None
  79. device_uri = None
  80. level = 1
  81.  
  82. if os.getenv("HPLIP_DEBUG"):
  83.     log.set_level('debug')
  84.  
  85.  
  86. for o, a in opts:
  87.     if o in ('-h', '--help'):
  88.         usage()
  89.         
  90.     elif o == '--help-rest':
  91.         usage('rest')
  92.         
  93.     elif o == '--help-man':
  94.         usage('man')
  95.  
  96.     elif o in ('-p', '--printer'):
  97.         if a.startswith('*'):
  98.             printer_name = cups.getDefault()
  99.         else:
  100.             printer_name = a
  101.  
  102.     elif o in ('-d', '--device'):
  103.         device_uri = a
  104.  
  105.     elif o in ('-b', '--bus'):
  106.         bus = a.lower().strip()
  107.  
  108.     elif o in ('-l', '--logging'):
  109.         log_level = a.lower().strip()
  110.         
  111.     elif o == '-g':
  112.         log.set_level('debug')
  113.  
  114.     elif o in ('-v', '--level'):
  115.         try:
  116.             level = int(a)
  117.         except ValueError:
  118.             log.error("Invalid cleaning level, setting level to 1.")
  119.             level = 1
  120.  
  121.             
  122. if level < 1 or level > 3:
  123.     log.error("Invalid cleaning level, setting level to 1.")
  124.     level = 1
  125.  
  126. if not device.validateBusList(bus):
  127.     usage()
  128.  
  129. if not log.set_level(log_level):
  130.     usage()
  131.  
  132. if device_uri and printer_name:
  133.     log.error("You may not specify both a printer (-p) and a device (-d).")
  134.     usage()
  135.  
  136. utils.log_title(__title__, __version__)
  137.     
  138. if not device_uri and not printer_name:
  139.     try:
  140.         device_uri = device.getInteractiveDeviceURI(bus)
  141.         if device_uri is None:
  142.             sys.exit(0)
  143.     except Error:
  144.         log.error("Error occured during interactive mode. Exiting.")
  145.         sys.exit(0)
  146.  
  147. try:
  148.     d = device.Device(device_uri, printer_name)
  149. except Error, e:
  150.     log.error("Unable to open device: %s" % e.msg)
  151.     sys.exit(0)
  152.  
  153. if d.device_uri is None and printer_name:
  154.     log.error("Printer '%s' not found." % printer_name)
  155.     sys.exit(0)
  156.  
  157. if d.device_uri is None and device_uri:
  158.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  159.     sys.exit(0)
  160.  
  161.  
  162. try:
  163.     try:
  164.         d.open()
  165.     except Error:
  166.         log.error("Unable to print to printer. Please check device and try again.")
  167.         sys.exit(1)
  168.     
  169.     if d.isIdleAndNoError():
  170.         clean_type = d.mq.get('clean-type', 0)
  171.         log.info("Performing type %d, level %d cleaning..." % (clean_type, level))
  172.         
  173.         if clean_type in (CLEAN_TYPE_PCL,CLEAN_TYPE_PCL_WITH_PRINTOUT):
  174.             if level == 3:
  175.                 maint.wipeAndSpitType1(d)
  176.             elif level == 2:
  177.                 maint.primeType1(d)
  178.             else:
  179.                 maint.cleanType1(d)
  180.         
  181.         elif clean_type == CLEAN_TYPE_LIDIL:
  182.             if level == 3:
  183.                 maint.wipeAndSpitType2(d)
  184.             elif level == 2:
  185.                 maint.primeType2(d)
  186.             else:
  187.                 maint.cleanType2(d)
  188.     
  189.         else:
  190.             log.error("Cleaning not needed or supported on this device.")
  191.     
  192.     else:
  193.         log.error("Device is busy or in an error state. Please check device and try again.")
  194.         sys.exit(1)
  195. finally:
  196.     d.close()
  197.     
  198. log.info("")
  199. log.info("Done.")
  200.  
  201.