2 # -*- coding: utf-8 -*-
4 # (c) Copyright 2003-2008 Hewlett-Packard Development Company, L.P.
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.
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.
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
24 __title__ = 'Supply Levels Utility'
26 __doc__ = "Display bar graphs of current supply levels for supported HPLIP printers."
37 from base import device, status, utils, tui, module
40 DEFAULT_BAR_GRAPH_SIZE = 8*(tui.ttysize()[1])/10
43 def logBarGraph(agent_level, agent_type, size=DEFAULT_BAR_GRAPH_SIZE, use_colors=True, bar_char='/'):
44 #print agent_level, agent_type, size, use_colors, bar_char
47 if adj==0.0: adj=100.0
48 bar = int(agent_level/adj)
49 if bar > (size-2): bar = size-2
52 if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM, AGENT_TYPE_CYAN, AGENT_TYPE_CYAN_LOW):
53 log.info(log.codes['teal'])
54 elif agent_type in (AGENT_TYPE_MAGENTA, AGENT_TYPE_MAGENTA_LOW):
55 log.info(log.codes['fuscia'])
56 elif agent_type in (AGENT_TYPE_YELLOW, AGENT_TYPE_YELLOW_LOW):
57 log.info(log.codes['yellow'])
58 elif agent_type == AGENT_TYPE_BLUE:
59 log.info(log.codes['blue'])
60 elif agent_type in (AGENT_TYPE_BLACK, AGENT_TYPE_BLACK_B8800):
61 log.info(log.codes['bold'])
62 elif agent_type in (AGENT_TYPE_LG, AGENT_TYPE_G, AGENT_TYPE_PG):
67 if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM):
68 color = log.codes['fuscia']
70 log.info(("-"*size)+color)
74 if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM):
75 color = log.codes['yellow']
77 log.info("%s%s%s%s (approx. %d%%)%s" % ("|", bar_char*bar,
78 " "*(size-bar-2), "|", agent_level, color))
82 color = log.codes['reset']
84 log.info(("-"*size)+color)
88 log.set_module('hp-levels')
91 mod = module.Module(__mod__, __title__, __version__, __doc__, None,
94 mod.setUsage(module.USAGE_FLAG_DEVICE_ARGS,
96 ("Bar graph size:", "-s<size> or --size=<size> (current default=%d)" % DEFAULT_BAR_GRAPH_SIZE, "option", False),
97 ("Use colored bar graphs:", "-c or --color (default is colorized)", "option", False),
98 ("Bar graph character:", "-a<char> or --char=<char> (default is '/')", "option", False)])
101 opts, device_uri, printer_name, mode, ui_toolkit, lang = \
102 mod.parseStdOpts('s:ca:', ['size=', 'color', 'char='])
104 device_uri = mod.getDeviceUri(device_uri, printer_name)
106 size = DEFAULT_BAR_GRAPH_SIZE
111 if o in ('-s', '--size'):
113 size = int(a.strip())
114 except (TypeError, ValueError):
115 log.warn("Invalid size specified. Using the default of %d" % DEFAULT_BAR_GRAPH_SIZE)
116 size = DEFAULT_BAR_GRAPH_SIZE
118 if size < 1 or size > DEFAULT_BAR_GRAPH_SIZE:
119 log.warn("Invalid size specified. Using the default of %d" % DEFAULT_BAR_GRAPH_SIZE)
120 size = DEFAULT_BAR_GRAPH_SIZE
122 elif o in ('-c', '--color'):
125 elif o in ('-a', '--char'):
133 d = device.Device(device_uri, printer_name)
135 log.error("Error opening device. Exiting.")
143 log.error("Error opening device (%s). Exiting." % e.msg)
146 if d.mq['status-type'] != STATUS_TYPE_NONE:
153 agent_type = int(d.dq['agent%d-type' % a])
154 agent_kind = int(d.dq['agent%d-kind' % a])
155 agent_sku = d.dq['agent%d-sku' % a]
156 log.debug("%d: agent_type %d agent_kind %d agent_sku '%s'" % (a, agent_type, agent_kind, agent_sku))
160 sorted_supplies.append((a, agent_kind, agent_type, agent_sku))
163 sorted_supplies.sort(lambda x, y: cmp(x[1], y[1]) or cmp(x[3], y[3]))
165 for x in sorted_supplies:
166 a, agent_kind, agent_type, agent_sku = x
167 agent_health = d.dq['agent%d-health' % a]
168 agent_level = d.dq['agent%d-level' % a]
169 agent_desc = d.dq['agent%d-desc' % a]
170 agent_health_desc = d.dq['agent%d-health-desc' % a]
172 if agent_health in (AGENT_HEALTH_OK, AGENT_HEALTH_UNKNOWN) and \
173 agent_kind in (AGENT_KIND_SUPPLY,
174 AGENT_KIND_HEAD_AND_SUPPLY,
175 AGENT_KIND_TONER_CARTRIDGE,
176 AGENT_KIND_MAINT_KIT,
178 AGENT_KIND_INT_BATTERY,
179 AGENT_KIND_DRUM_KIT,):
181 log.info(log.bold(agent_desc))
182 log.info("Part No.: %s" % agent_sku)
183 log.info("Health: %s" % agent_health_desc)
184 logBarGraph(agent_level, agent_type, size, color, bar_char)
188 log.info(log.bold(agent_desc))
189 log.info("Part No.: %s" % agent_sku)
190 log.info("Health: %s" % agent_health_desc)
195 log.error("Status not supported for selected device.")
200 except KeyboardInterrupt:
201 log.error("User exit")