home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / align < prev    next >
Encoding:
Text File  |  2006-08-30  |  10.5 KB  |  335 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__ = '2.4'
  24. __title__ = 'Printer Cartridge Alignment Utility'
  25. __doc__ = "Cartridge alignment utility for HPLIP supported inkjet printers."
  26.  
  27. # Std Lib
  28. import sys
  29. import re
  30. import getopt
  31.  
  32. # Local
  33. from base.g import *
  34. from base import device, status, utils, maint
  35. from prnt import cups
  36.  
  37. USAGE = [(__doc__, "", "name", True),
  38.          ("""Usage: hp-align [PRINTER|DEVICE-URI] [OPTIONS]""", "", "summary", True),
  39.          utils.USAGE_ARGS,
  40.          utils.USAGE_DEVICE,
  41.          utils.USAGE_PRINTER,
  42.          utils.USAGE_SPACE,
  43.          utils.USAGE_OPTIONS,
  44.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  45.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  46.          utils.USAGE_HELP,
  47.          utils.USAGE_EXAMPLES,
  48.          ("""Align CUPS printer named 'hp5550':""", """$ hp-align -php5550""",  "example", False),
  49.          ("""Align printer with URI of 'hp:/usb/DESKJET_990C?serial=12345':""", """$ hp-align -dhp:/usb/DESKJET_990C?serial=12345""", 'example', False),
  50.          utils.USAGE_SPACE,
  51.          utils.USAGE_NOTES,
  52.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  53.          utils.USAGE_SEEALSO,
  54.          ("hp-clean", "", "seealso", False),
  55.          ("hp-colorcal", "", "seealso", False),
  56.          ]
  57.  
  58.  
  59. def usage(typ='text'):
  60.     if typ == 'text':
  61.         utils.log_title(__title__, __version__)
  62.         
  63.     utils.format_text(USAGE, typ, __title__, 'hp-align', __version__)
  64.     sys.exit(0)
  65.     
  66.  
  67. def enterNumber(text, minimum, maximum):
  68.     while True:
  69.         x = raw_input(utils.bold(text))
  70.  
  71.         if len(x) > 0 and x[0] in ['q', 'Q']:
  72.             return False, 0
  73.  
  74.         try:
  75.             x = int(x)
  76.         except ValueError:
  77.             log.error("You must enter a numeric value.")
  78.             continue
  79.         if x < minimum or x > maximum:
  80.             log.error("You must enter a number between %d and %d." % (minimum, maximum))
  81.             continue
  82.         break
  83.  
  84.     return True, x
  85.  
  86. def enterAlignmentNumber(letter, hortvert, colors, line_count, maximum):
  87.     return enterNumber("Enter the best aligned value for line %s (1-%d): " % (letter, maximum),
  88.                         1,
  89.                         maximum)
  90.  
  91. def enterPaperEdge(maximum):
  92.     return enterNumber("Enter numbered arrow that is best aligned with the paper edge (1-%d): " % maximum,
  93.                         1,
  94.                         maximum)
  95.  
  96. def colorAdj(line, maximum):
  97.     return enterNumber("Enter the numbered box on line %s that is best color matched to the background color (1-%d): " % (line, maximum),
  98.                         1,
  99.                         maximum)
  100.  
  101.  
  102. def loadPlainPaper():
  103.     x = raw_input(utils.bold("An alignment page will be printed.\nPlease load plain paper into the printer. Press <Enter> to contine or 'q' to quit."))
  104.     if len(x) > 0 and x[0].lower() == 'q':
  105.         return False
  106.     return True
  107.  
  108. def bothPensRequired():
  109.     log.error("Cannot perform alignment with 0 or 1 cartridges installed.\nPlease install both cartridges and try again.")
  110.  
  111. def invalidPen():
  112.     log.error("Invalid cartridge(s) installed.\nPlease install valid cartridges and try again.")
  113.  
  114. def aioUI1():
  115.     log.info("To perform alignment, you will need the alignment page that is automatically\nprinted after you install a print cartridge.")
  116.     log.info("If you would like to cancel, enter 'C' or 'c'")
  117.     log.info("If you do not have this page (and need it to be printed), enter 'N' or 'n'")
  118.     log.info("If you already have this page, enter 'Y' or 'y'")
  119.  
  120.     while 1:
  121.         x = raw_input(utils.bold("Enter 'C', 'c'; 'Y', 'y'; 'N', or 'n': "))
  122.         if len(x) > 0:
  123.             x = x.lower()
  124.             if x[0] in ['c', 'y', 'n']:
  125.                 break
  126.  
  127.         log.warning("Please enter 'C', 'c'; 'Y', 'y'; 'N' or 'n'.")
  128.  
  129.     if x[0] == 'n':
  130.         return False
  131.  
  132.     elif x[0] == 'c':
  133.         sys.exit(0)
  134.  
  135.     elif x[0] == 'y':
  136.         return True
  137.  
  138. def type10Align(pattern):
  139.     if pattern == 1:
  140.         controls = { 'A' : (True, 23),
  141.                      'B' : (True, 9),
  142.                      'C' : (True, 9),
  143.                      'D' : (False, 0),
  144.                      'E' : (False, 0),
  145.                      'F' : (False, 0),
  146.                      'G' : (False, 0),
  147.                      'H' : (False, 0),}
  148.     elif pattern == 2:
  149.         controls = { 'A' : (True, 23),
  150.                      'B' : (True, 17),
  151.                      'C' : (True, 23),
  152.                      'D' : (True, 23),
  153.                      'E' : (True, 9),
  154.                      'F' : (True, 9),
  155.                      'G' : (True, 9),
  156.                      'H' : (True, 9),}
  157.     
  158.     elif pattern == 3:
  159.         controls = { 'A' : (True, 23),
  160.                      'B' : (True, 9),
  161.                      'C' : (True, 23),
  162.                      'D' : (True, 23),
  163.                      'E' : (True, 9),
  164.                      'F' : (True, 9),
  165.                      'G' : (True, 9),
  166.                      'H' : (True, 9),}    
  167.  
  168.     values = []
  169.     for line in controls:
  170.         if not controls[line][0]:
  171.             values.append(0)
  172.         else:
  173.             cont, value = enterNumber( "Enter the numbered box on line %s where the inner lines best line up with the outer lines (1-%d): " % ( line, controls[line][1] ),
  174.                            1, controls[line][1] )
  175.             if not cont:
  176.                 sys.exit(0)
  177.             
  178.             values.append(value)
  179.                 
  180.  
  181.     return values
  182.     
  183.                 
  184. def aioUI2():
  185.     log.info("")
  186.     log.info(utils.bold("Follow these steps to complete the alignment:"))
  187.     log.info("1. Place the alignment page, with the printed side facing down, ")
  188.     log.info("   in the scanner.")
  189.     log.info("2. Press the Enter or Scan button on the printer.")
  190.     log.info('3. "Alignment Complete" will be displayed when the process is finished (on some models).')
  191.  
  192.  
  193. try:
  194.     opts, args = getopt.getopt(sys.argv[1:],
  195.                                 'p:d:hl:b:ag',
  196.                                 ['printer=',
  197.                                   'device=',
  198.                                   'help',
  199.                                   'help-rest',
  200.                                   'help-man',
  201.                                   'logging=',
  202.                                   'bus='
  203.                                 ]
  204.                               )
  205. except getopt.GetoptError:
  206.     usage()
  207.  
  208. printer_name = None
  209. device_uri = None
  210. bus = device.DEFAULT_PROBE_BUS
  211. log_level = logger.DEFAULT_LOG_LEVEL
  212. align_debug = False
  213.  
  214. if os.getenv("HPLIP_DEBUG"):
  215.     log.set_level('debug')
  216.  
  217. for o, a in opts:
  218.     if o in ('-h', '--help'):
  219.         usage()
  220.     
  221.     elif o == '--help-rest':
  222.         usage('rest')
  223.  
  224.     elif o == '--help-man':
  225.         usage('man')
  226.  
  227.     elif o in ('-p', '--printer'):
  228.         if a.startswith('*'):
  229.             printer_name = cups.getDefault()
  230.         else:
  231.             printer_name = a
  232.  
  233.     elif o in ('-d', '--device'):
  234.         device_uri = a
  235.  
  236.     elif o in ('-b', '--bus'):
  237.         bus = a.lower().strip()
  238.         if not device.validateBusList(bus):
  239.             usage()
  240.  
  241.     elif o in ('-l', '--logging'):
  242.         log_level = a.lower().strip()
  243.         if not log.set_level(log_level):
  244.             usage()
  245.             
  246.     elif o == '-g':
  247.         log.set_level('debug')
  248.  
  249.     elif o == '-a':
  250.         align_debug = True
  251.  
  252.  
  253.  
  254. if device_uri and printer_name:
  255.     log.error("You may not specify both a printer (-p) and a device (-d).")
  256.     usage()
  257.  
  258. utils.log_title(__title__, __version__)
  259.     
  260. if not device_uri and not printer_name:
  261.     try:
  262.         device_uri = device.getInteractiveDeviceURI(bus)
  263.         if device_uri is None:
  264.             sys.exit(0)
  265.     except Error:
  266.         log.error("Error occured during interactive mode. Exiting.")
  267.         sys.exit(0)
  268.  
  269. try:
  270.     d = device.Device( device_uri, printer_name )
  271. except Error, e:
  272.     log.error("Unable to open device: %s" % e.msg)
  273.     sys.exit(0)
  274.  
  275. if d.device_uri is None and printer_name:
  276.     log.error("Printer '%s' not found." % printer_name)
  277.     sys.exit(0)
  278.  
  279. if d.device_uri is None and device_uri:
  280.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  281.     sys.exit(0)
  282.  
  283.     
  284. try:
  285.     try:
  286.         d.open()
  287.     except Error:
  288.         log.error("Device is busy or in an error state. Please check device and try again.")
  289.         sys.exit(1)
  290.     
  291.     if d.isIdleAndNoError():
  292.         align_type = d.mq.get('align-type', 0)
  293.         log.debug("Alignment type=%d" % align_type)
  294.         
  295.         if align_type == ALIGN_TYPE_NONE:
  296.             log.error("Alignment not supported or required by device.")
  297.             sys.exit(0)
  298.         
  299.         if align_type == ALIGN_TYPE_AUTO:
  300.             maint.AlignType1(d, loadPlainPaper)
  301.         
  302.         elif align_type == ALIGN_TYPE_8XX:
  303.             maint.AlignType2(d, loadPlainPaper, enterAlignmentNumber,
  304.                               bothPensRequired)
  305.         
  306.         elif align_type in (ALIGN_TYPE_9XX,ALIGN_TYPE_9XX_NO_EDGE_ALIGN):
  307.             maint.AlignType3(d, loadPlainPaper, enterAlignmentNumber,
  308.                               enterPaperEdge, update_spinner)
  309.         
  310.         elif align_type == ALIGN_TYPE_LIDIL_AIO:
  311.             maint.AlignType6(d, aioUI1, aioUI2, loadPlainPaper)
  312.         
  313.         elif align_type == ALIGN_TYPE_DESKJET_450:
  314.             maint.AlignType8(d, loadPlainPaper, enterAlignmentNumber)
  315.         
  316.         elif align_type in (ALIGN_TYPE_LIDIL_0_3_8, ALIGN_TYPE_LIDIL_0_4_3, ALIGN_TYPE_LIDIL_VIP):
  317.         
  318.             maint.AlignxBow(d, align_type, loadPlainPaper, enterAlignmentNumber, enterPaperEdge,
  319.                              invalidPen, colorAdj)
  320.                              
  321.         elif align_type == ALIGN_TYPE_LBOW:
  322.             maint.AlignType10(d, loadPlainPaper, type10Align)
  323.             
  324.         else:
  325.             log.error("Invalid alignment type.")
  326.     
  327.     else:
  328.         log.error("Device is busy or in an error state. Please check device and try again.")
  329.  
  330. finally:
  331.     d.close()
  332.     
  333. log.info("")
  334. log.info('Done.')
  335.