home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / levels < prev    next >
Encoding:
Text File  |  2006-08-30  |  7.5 KB  |  266 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__ = '1.0'
  24. __title__ = 'Supply Levels Utility'
  25. __doc__ = "Display bar graphs of current supply levels for supported HPLIP printers."
  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. DEFAULT_BAR_GRAPH_SIZE = 100
  36.  
  37.  
  38.  
  39. USAGE = [(__doc__, "", "name", True),
  40.          ("Usage: hp-levels [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.         ("Bar graph size:", "-s<size> or --size=<size> (default=%d)", "option", False),
  48.         ("Use colored bar graphs:", "-c or --color (default=False)", "option", False),
  49.         ("Bar graph character:", "-a<char> or --char=<char> (default is '/')", "option", False),
  50.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  51.          utils.USAGE_HELP,
  52.          utils.USAGE_SPACE,
  53.          utils.USAGE_NOTES,
  54.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  55.          ]
  56.          
  57. def usage(typ='text'):
  58.     if typ == 'text':
  59.         utils.log_title(__title__, __version__)
  60.         
  61.     utils.format_text(USAGE, typ, __title__, 'hp-levels', __version__)
  62.     sys.exit(0)
  63.  
  64.  
  65. def logBarGraph(agent_level, agent_type, size=DEFAULT_BAR_GRAPH_SIZE, use_colors=True, bar_char='/'):
  66.     if size == 0: size = 100
  67.     adj = 100.0/size
  68.     if adj==0.0: adj=100.0
  69.     bar = int(agent_level/adj)
  70.     if bar > (size-2): bar = size-2
  71.  
  72.     if use_colors:
  73.         if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM, AGENT_TYPE_CYAN, AGENT_TYPE_CYAN_LOW):
  74.             log.info(utils.codes['teal'])
  75.         elif agent_type in (AGENT_TYPE_MAGENTA, AGENT_TYPE_MAGENTA_LOW):
  76.             log.info(utils.codes['fuscia'])
  77.         elif agent_type in (AGENT_TYPE_YELLOW, AGENT_TYPE_YELLOW_LOW):
  78.             log.info(utils.codes['yellow'])
  79.         elif agent_type == AGENT_TYPE_BLUE:
  80.             log.info(utils.codes['blue'])
  81.         elif agent_type == AGENT_TYPE_BLACK:
  82.             log.info(utils.codes['bold'])
  83.         elif agent_type in (AGENT_TYPE_LG, AGENT_TYPE_G, AGENT_TYPE_PG):
  84.             pass
  85.  
  86.     color = ''
  87.     if use_colors:
  88.         if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM):
  89.             color = utils.codes['fuscia']
  90.  
  91.     log.info(("-"*size)+color)
  92.  
  93.     color = ''
  94.     if use_colors:
  95.         if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM):
  96.             color = utils.codes['yellow']
  97.  
  98.     log.info("%s%s%s%s (approx. %d%%)%s" % ("|", bar_char*bar, 
  99.              " "*(size-bar-2), "|", agent_level, color))
  100.  
  101.     color = ''
  102.     if use_colors:
  103.         color = utils.codes['reset']
  104.  
  105.     log.info(("-"*size)+color)
  106.  
  107.  
  108. try:
  109.     opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:s:ca:g',
  110.         ['printer=', 'device=', 'help', 'help-rest', 'help-man', 
  111.          'logging=', 'size=', 'color', 'char='])
  112.          
  113. except getopt.GetoptError:
  114.     usage()
  115.  
  116. printer_name = None
  117. device_uri = None
  118. log_level = logger.DEFAULT_LOG_LEVEL
  119. bus = device.DEFAULT_PROBE_BUS
  120. size=100
  121. color=False
  122. bar_char = '/'
  123.  
  124. if os.getenv("HPLIP_DEBUG"):
  125.     log.set_level('debug')
  126.  
  127. for o, a in opts:
  128.     if o in ('-h', '--help'):
  129.         usage()
  130.  
  131.     elif o == '--help-rest':
  132.         usage('rest')
  133.         
  134.     elif o == '--help-man':
  135.         usage('man')
  136.  
  137.     elif o in ('-p', '--printer'):
  138.         if a.startswith('*'):
  139.             printer_name = cups.getDefault()
  140.             log.info("Using CUPS default printer: %s" % printer_name)
  141.             log.debug(printer_name)
  142.         else:
  143.             printer_name = a
  144.  
  145.     elif o in ('-d', '--device'):
  146.         device_uri = a
  147.  
  148.     elif o in ('-b', '--bus'):
  149.         bus = a.lower().strip()
  150.         if not device.validateBusList(bus):
  151.             usage()
  152.  
  153.     elif o in ('-l', '--logging'):
  154.         log_level = a.lower().strip()
  155.         if not log.set_level(log_level):
  156.             usage()
  157.  
  158.     elif o in ('-s', '--size'):
  159.         try:
  160.             size = int(a.strip())
  161.         except:
  162.             size = DEFAULT_BAR_GRAPH_SIZE
  163.  
  164.         if size < 0 or size > 200:
  165.             size = DEFAULT_BAR_GRAPH_SIZE
  166.  
  167.     elif o in ('-c', '--color'):
  168.         color = True
  169.  
  170.     elif o in ('-a', '--char'):
  171.         try:
  172.             bar_char = a[0]
  173.         except:
  174.             bar_char = '/'
  175.  
  176.     elif o == '-g':
  177.         log.set_level('debug')
  178.  
  179.  
  180. if device_uri and printer_name:
  181.     log.error("You may not specify both a printer (-p) and a device (-d).")
  182.     usage()
  183.  
  184. utils.log_title(__title__, __version__)
  185.  
  186. if not device_uri and not printer_name:
  187.     try:
  188.         device_uri = device.getInteractiveDeviceURI(bus)
  189.         if device_uri is None:
  190.             sys.exit(1)
  191.     except Error:
  192.         log.error("Error occured during interactive mode. Exiting.")
  193.         sys.exit(1)
  194.  
  195. try:
  196.     d = device.Device(device_uri, printer_name)
  197. except Error:
  198.     log.error("Error opening device. Exiting.")
  199.     sys.exit(1)
  200.  
  201. if d.device_uri is None and printer_name:
  202.     log.error("Printer '%s' not found." % printer_name)
  203.     sys.exit(1)
  204.  
  205. if d.device_uri is None and device_uri:
  206.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  207.     sys.exit(1)
  208.  
  209.  
  210. try:
  211.     d.open()
  212.     d.queryDevice()
  213. except Error, e:
  214.     log.error("Error opening device (%s). Exiting." % e.msg)
  215.     sys.exit(1)
  216.  
  217. if d.mq['status-type'] != STATUS_TYPE_NONE:
  218.  
  219.     log.info("")
  220.  
  221.     a = 1
  222.     while True:
  223.  
  224.         try:
  225.             agent_type = d.dq['agent%d-type' % a]
  226.         except KeyError:
  227.             break
  228.         else:
  229.             agent_kind = d.dq['agent%d-kind' % a]
  230.             agent_health = d.dq['agent%d-health' % a]
  231.             agent_level = d.dq['agent%d-level' % a]
  232.             agent_sku = str(d.dq['agent%d-sku' % a])
  233.             agent_desc = d.dq['agent%d-desc' % a]
  234.             agent_health_desc = d.dq['agent%d-health-desc' % a]
  235.  
  236.             if agent_health == AGENT_HEALTH_OK and \
  237.                 agent_kind in (AGENT_KIND_SUPPLY,
  238.                                 AGENT_KIND_HEAD_AND_SUPPLY,
  239.                                 AGENT_KIND_TONER_CARTRIDGE,
  240.                                 AGENT_KIND_MAINT_KIT,
  241.                                 AGENT_KIND_ADF_KIT,
  242.                                 AGENT_KIND_INT_BATTERY,
  243.                                 AGENT_KIND_DRUM_KIT,):
  244.  
  245.                 log.info(utils.bold(agent_desc))
  246.                 log.info("Part No.: %s" % agent_sku)
  247.                 log.info("Health: %s" % agent_health_desc)
  248.                 logBarGraph(agent_level, agent_type, size, color, bar_char)
  249.                 log.info("")
  250.  
  251.             else:
  252.                 log.info(utils.bold(agent_desc))
  253.                 log.info("Part No.: %s" % agent_sku)
  254.                 log.info("Health: %s" % agent_health_desc)
  255.                 log.info("")
  256.  
  257.         a += 1
  258.  
  259. else:
  260.     log.error("Status not supported for selected device.")
  261.     sys.exit(1)
  262.  
  263. d.close()
  264. sys.exit(0)
  265.  
  266.