3 # Watch line printer queue(s).
4 # Intended for BSD 4.3 lpq.
15 delay
= DEF_DELAY
# XXX Use getopt() later
17 thisuser
= posix
.environ
['LOGNAME']
19 thisuser
= posix
.environ
['USER']
20 printers
= sys
.argv
[1:]
22 # Strip '-P' from printer names just in case
23 # the user specified it...
24 for i
in range(len(printers
)):
25 if printers
[i
][:2] == '-P':
26 printers
[i
] = printers
[i
][2:]
28 if posix
.environ
.has_key('PRINTER'):
29 printers
= [posix
.environ
['PRINTER']]
31 printers
= [DEF_PRINTER
]
33 clearhome
= posix
.popen('clear', 'r').read()
38 text
= text
+ makestatus(name
, thisuser
) + '\n'
42 def makestatus(name
, thisuser
):
43 pipe
= posix
.popen('lpq -P' + name
+ ' 2>&1', 'r')
52 line
= pipe
.readline()
54 fields
= string
.split(line
)
56 if len(fields
) >= 6 and fields
[n
-1] == 'bytes':
61 bytes
= eval(fields
[n
-2])
65 aheadbytes
= aheadbytes
+ bytes
66 aheadjobs
= aheadjobs
+ 1
67 totalbytes
= totalbytes
+ bytes
68 totaljobs
= totaljobs
+ 1
69 if users
.has_key(user
):
70 ujobs
, ubytes
= users
[user
]
74 ubytes
= ubytes
+ bytes
75 users
[user
] = ujobs
, ubytes
77 if fields
and fields
[0] <> 'Rank':
78 line
= string
.strip(line
)
79 if line
== 'no entries':
80 line
= name
+ ': idle'
81 elif line
[-22:] == ' is ready and printing':
86 line
= `
(totalbytes
+1023)/1024`
+ ' K'
87 if totaljobs
<> len(users
):
88 line
= line
+ ' (' + `totaljobs`
+ ' jobs)'
90 line
= line
+ ' for ' + users
.keys()[0]
92 line
= line
+ ' for ' + `
len(users
)`
+ ' users'
95 line
= line
+ ' (' + thisuser
+ ' first)'
97 line
= line
+ ' (' + `
(aheadbytes
+1023)/1024`
98 line
= line
+ ' K before ' + thisuser
+ ')'
103 lines
.append('lpq exit status ' + `sts`
)
104 return string
.joinfields(lines
, ': ')
108 except KeyboardInterrupt: