blob: 0a1346f4d1eec317d5b117999848e4851a75437b [file] [log] [blame]
Guido van Rossum93a35f41992-12-14 14:12:10 +00001#! /usr/local/bin/python
Guido van Rossum9cf8f331992-03-30 10:54:51 +00002
3# Watch line printer queues (only works with BSD 4.3 lpq).
4#
5# This brings up a window containing one line per printer argument.
6#
7# Each line gives a small summary of the printer's status and queue.
8# The status tries to give as much relevant information as possible,
9# and gives extra info if you have jobs in the queue.
10#
11# The line's background color gives a hint at the status: navajo white
12# for idle, green if your job is now printing, yellow/orange for
13# small/large queue, red for errors.
14#
15# To reduce the duration of the unresponsive time while it is waiting
16# for an lpq subprocess to finish, it polls one printer every
17# delay/len(printers) seconds. A tiny dot indicates the last printer
18# updated. Hit the mouse button in the window to update the next one.
19#
20# To do:
21# - add an argument to override the default delay
22# - add arguments to override the default colors
23# - better heuristic for small/large queue (and more colors!)
24# - mouse clicks should update the printer clicked in
25# - better visual appearance, e.g., boxes around the lines?
26
27import posix
28import sys
29import time
30import string
31
32import stdwin
33from stdwinevents import *
34import mainloop
35
36# Default parameters
37DEF_PRINTER = 'oce' # This is CWI specific!
38DEF_DELAY = 10
39
40# Color assignments
41c_unknown = stdwin.fetchcolor('white')
42c_idle = stdwin.fetchcolor('navajo white')
43c_ontop = stdwin.fetchcolor('green')
44c_smallqueue = stdwin.fetchcolor('yellow')
45c_bigqueue = stdwin.fetchcolor('orange')
46c_error = stdwin.fetchcolor('red')
47
48def main():
49 delay = DEF_DELAY
50 #
51 try:
52 thisuser = posix.environ['LOGNAME']
53 except:
54 thisuser = posix.environ['USER']
55 #
56 printers = sys.argv[1:]
57 if printers:
58 # Strip '-P' from printer names just in case
59 # the user specified it...
60 for i in range(len(printers)):
61 if printers[i][:2] == '-P':
62 printers[i] = printers[i][2:]
63 else:
64 if posix.environ.has_key('PRINTER'):
65 printers = [posix.environ['PRINTER']]
66 else:
67 printers = [DEF_PRINTER]
68 #
69 width = stdwin.textwidth('in')*20
70 height = len(printers) * stdwin.lineheight() + 5
71 stdwin.setdefwinsize(width, height)
72 stdwin.setdefscrollbars(0, 0)
73 #
74 win = stdwin.open('lpwin')
75 #
76 win.printers = printers
77 win.colors = [c_unknown] * len(printers)
78 win.texts = printers[:]
79 win.next = 0
80 win.delay = DEF_DELAY
81 win.thisuser = thisuser
82 win.dispatch = lpdispatch
83 #
84 win.settimer(1)
85 #
86 mainloop.register(win)
87 mainloop.mainloop()
88
89def lpdispatch(type, win, detail):
90 if type == WE_CLOSE or type == WE_CHAR and detail in ('q', 'Q'):
91 mainloop.unregister(win)
92 elif type == WE_DRAW:
93 drawproc(win)
94 elif type == WE_TIMER:
95 update(win)
96 win.change((0,0), (10000, 10000))
97 elif type == WE_MOUSE_UP:
98 win.settimer(1)
99
100def drawproc(win):
101 d = win.begindrawing()
102 offset = d.textwidth('.')
103 h, v = 0, 0
104 for i in range(len(win.printers)):
105 text = win.texts[i]
106 color = win.colors[i]
107 d.setbgcolor(color)
108 d.erase((h, v), (h+10000, v+d.lineheight()))
109 if (i+1) % len(win.printers) == win.next and color <> c_unknown:
110 d.text((h, v), '.')
111 d.text((h+offset, v), text)
112 v = v + d.lineheight()
113
114def update(win):
115 i = win.next
116 win.next = (i+1) % len(win.printers)
117 win.texts[i], win.colors[i] = makestatus(win.printers[i], win.thisuser)
118 win.settimer(int(win.delay * 10.0 / len(win.printers)))
119
120def makestatus(name, thisuser):
121 pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r')
122 lines = []
123 users = {}
124 aheadbytes = 0
125 aheadjobs = 0
126 userseen = 0
127 totalbytes = 0
128 totaljobs = 0
129 color = c_unknown
130 while 1:
131 line = pipe.readline()
132 if not line: break
133 fields = string.split(line)
134 n = len(fields)
135 if len(fields) >= 6 and fields[n-1] == 'bytes':
136 rank = fields[0]
137 user = fields[1]
138 job = fields[2]
139 files = fields[3:-2]
140 bytes = eval(fields[n-2])
141 if user == thisuser:
142 userseen = 1
143 if aheadjobs == 0:
144 color = c_ontop
145 elif not userseen:
146 aheadbytes = aheadbytes + bytes
147 aheadjobs = aheadjobs + 1
148 totalbytes = totalbytes + bytes
149 totaljobs = totaljobs + 1
150 if color == c_unknown:
151 color = c_smallqueue
152 elif color == c_smallqueue:
153 color = c_bigqueue
154 if users.has_key(user):
155 ujobs, ubytes = users[user]
156 else:
157 ujobs, ubytes = 0, 0
158 ujobs = ujobs + 1
159 ubytes = ubytes + bytes
160 users[user] = ujobs, ubytes
161 else:
162 if fields and fields[0] <> 'Rank':
163 line = string.strip(line)
164 if line == 'no entries':
165 line = name + ': idle'
166 if color == c_unknown:
167 color = c_idle
168 elif line[-22:] == ' is ready and printing':
169 line = line[:-22]
170 else:
171 line = name + ': ' + line
172 color = c_error
173 lines.append(line)
174 #
175 if totaljobs:
176 line = `(totalbytes+1023)/1024` + ' K'
177 if totaljobs <> len(users):
178 line = line + ' (' + `totaljobs` + ' jobs)'
179 if len(users) == 1:
180 line = line + ' for ' + users.keys()[0]
181 else:
182 line = line + ' for ' + `len(users)` + ' users'
183 if userseen:
184 if aheadjobs == 0:
185 line = line + ' (' + thisuser + ' first)'
186 else:
187 line = line + ' (' + `(aheadbytes+1023)/1024`
188 line = line + ' K before ' + thisuser + ')'
189 lines.append(line)
190 #
191 sts = pipe.close()
192 if sts:
193 lines.append('lpq exit status ' + `sts`)
194 color = c_error
195 return string.joinfields(lines, ': '), color
196
197main()