home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / info < prev    next >
Encoding:
Text File  |  2006-08-30  |  6.2 KB  |  222 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__ = '3.4'
  24. __title__ = 'Device Information Utility'
  25. __doc__ = "Query a printer for both static model information and dynamic status."
  26.  
  27. # Std Lib
  28. import sys, getopt, time
  29.  
  30. # Local
  31. from base.g import *
  32. from base import device, status, utils
  33. from prnt import cups
  34.  
  35. USAGE = [(__doc__, "", "name", True),
  36.          ("Usage: hp-info [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  37.          utils.USAGE_ARGS,
  38.          utils.USAGE_DEVICE,
  39.          utils.USAGE_PRINTER,
  40.          utils.USAGE_SPACE,
  41.          utils.USAGE_OPTIONS,
  42.          ("Device ID mode:", "-i or --id (prints device ID only and exits)", "option", False),
  43.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  44.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  45.          utils.USAGE_HELP,
  46.          utils.USAGE_SPACE,
  47.          utils.USAGE_NOTES,
  48.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  49.          utils.USAGE_SEEALSO,
  50.          ("hp-toolbox", "", "seealso", False),
  51.  
  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-info', __version__)
  59.     sys.exit(0)
  60.  
  61.          
  62.  
  63. try:
  64.     opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:ig',
  65.         ['printer=', 'device=', 'help', 'help-rest', 'help-man', 
  66.          'logging=', 'id', 'bus='])
  67.          
  68. except getopt.GetoptError:
  69.     usage()
  70.  
  71. printer_name = None
  72. device_uri = None
  73. log_level = logger.DEFAULT_LOG_LEVEL
  74. bus = "cups,par,usb"
  75. devid_mode = False
  76.  
  77. if os.getenv("HPLIP_DEBUG"):
  78.     log.set_level('debug')
  79.  
  80. for o, a in opts:
  81.     if o in ('-h', '--help'):
  82.         usage()
  83.  
  84.     elif o == '--help-rest':
  85.         usage('rest')
  86.         
  87.     elif o == '--help-man':
  88.         usage('man')
  89.  
  90.     elif o in ('-p', '--printer'):
  91.         if a.startswith('*'):
  92.             printer_name = cups.getDefault()
  93.             log.info("Using CUPS default printer: %s" % printer_name)
  94.             log.debug(printer_name)
  95.         else:
  96.             printer_name = a
  97.  
  98.     elif o in ('-d', '--device'):
  99.         device_uri = a
  100.  
  101.     elif o in ('-b', '--bus'):
  102.         bus = a.lower().strip()
  103.         if not device.validateBusList(bus):
  104.             usage()
  105.  
  106.     elif o in ('-l', '--logging'):
  107.         log_level = a.lower().strip()
  108.         if not log.set_level(log_level):
  109.             usage()
  110.             
  111.     elif o == '-g':
  112.         log.set_level('debug')
  113.  
  114.     elif o in ('-i', '--id'):
  115.         devid_mode = True
  116.  
  117.  
  118. if device_uri and printer_name:
  119.     log.error("You may not specify both a printer (-p) and a device (-d).")
  120.     usage()
  121.  
  122. if not devid_mode:
  123.     utils.log_title(__title__, __version__)
  124.     
  125. if not device_uri and not printer_name:
  126.     try:
  127.         device_uri = device.getInteractiveDeviceURI(bus)
  128.         if device_uri is None:
  129.             sys.exit(1)
  130.     except Error:
  131.         log.error("Error occured during interactive mode. Exiting.")
  132.         sys.exit(1)
  133.  
  134. try:
  135.     d = device.Device(device_uri, printer_name)
  136. except Error:
  137.     log.error("Error opening device. Exiting.")
  138.     sys.exit(1)
  139.  
  140. if d.device_uri is None and printer_name:
  141.     log.error("Printer '%s' not found." % printer_name)
  142.     sys.exit(1)
  143.  
  144. if d.device_uri is None and device_uri:
  145.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  146.     sys.exit(1)
  147.  
  148. if not devid_mode:
  149.     log.info("")
  150.     log.info(utils.bold(d.device_uri))
  151.     log.info("")
  152.  
  153. try:
  154.     d.open()
  155.     d.queryDevice()
  156. except Error, e:
  157.     log.error("Error opening device (%s). Exiting." % e.msg)
  158.     sys.exit(1)
  159.  
  160. if not devid_mode:
  161.     formatter = utils.TextFormatter(
  162.                     (
  163.                         {'width': 28, 'margin' : 2},
  164.                         {'width': 58, 'margin' : 2},
  165.                     )
  166.                 )
  167.  
  168. if devid_mode:
  169.     try:
  170.         print d.dq['deviceid']
  171.     except KeyError:
  172.         log.error("Device ID not available.")
  173. else:
  174.     dq_keys = d.dq.keys()
  175.     dq_keys.sort()
  176.  
  177.     log.info(utils.bold("Device Parameters (dynamic data):"))
  178.     log.info(utils.TextFormatter.bold(formatter.compose(("Parameter", "Value(s)"))))
  179.     log.info(formatter.compose(('-'*28, '-'*58)))
  180.  
  181.     for key in dq_keys:
  182.         log.info(formatter.compose((key, str(d.dq[key]))))
  183.  
  184.     log.info(utils.bold("\nModel Parameters (static data):"))
  185.     log.info(utils.TextFormatter.bold(formatter.compose(("Parameter", "Value(s)"))))
  186.     log.info(formatter.compose(('-'*28, '-'*58)))
  187.  
  188.     mq_keys = d.mq.keys()
  189.     mq_keys.sort()
  190.  
  191.     for key in mq_keys:
  192.         log.info(formatter.compose((key, str(d.mq[key]))))
  193.         
  194.     formatter = utils.TextFormatter(
  195.                     (
  196.                         {'width': 20, 'margin' : 2}, # date/time
  197.                         {'width': 5, 'margin' : 2}, # code
  198.                         {'width': 40, 'margin' : 2}, # desc
  199.                         {'width': 8, 'margin' : 2}, # user
  200.                         {'width': 8, 'margin' : 2}, # job id
  201.                     )
  202.                 )
  203.         
  204.  
  205.     log.info(utils.bold("\nStatus History (most recent first):"))
  206.     log.info(utils.TextFormatter.bold(formatter.compose(("Date/Time", "Code", "Status Description", "User", "Job ID"))))
  207.     log.info(formatter.compose(('-'*20, '-'*5, '-'*40, '-'*8, '-'*8)))
  208.  
  209.     hq = d.queryHistory()
  210.  
  211.     for h in hq:
  212.         if h[9]:
  213.             j = str(h[9])
  214.         else:
  215.             j = ''
  216.         log.info(formatter.compose((time.strftime("%x %H:%M:%S", h[:9]),  str(h[11]), h[12], h[10], j)))
  217.  
  218.     log.info("")
  219.  
  220. d.close()
  221. sys.exit(0)
  222.