Created
November 9, 2009 17:25
-
-
Save xoebus/230115 to your computer and use it in GitHub Desktop.
A script to check the printers in AT.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import subprocess | |
good_colour = "\033[0;32m" | |
bad_colour = "\033[0;31m" | |
clear_colour = "\033[0m" | |
class Printer(object): | |
""" | |
This class represents a printer in AT. | |
""" | |
def __init__(self, name): | |
self.name = name | |
self.colour = name.endswith('c') | |
self._query_output = self._get_info() | |
self.job_count = self._count_jobs() | |
self.status_colour = self._get_colour() | |
def __str__(self): | |
""" | |
The string representation of a printer object. | |
""" | |
return self.status_colour + self.name + " has " + \ | |
str(self.job_count) + " jobs." + clear_colour | |
def _get_colour(self): | |
""" | |
Return the colour based on the number of jobs in the printer. | |
""" | |
if self.job_count < 3: | |
colour = good_colour | |
else: | |
colour = bad_colour | |
return colour | |
def _count_jobs(self): | |
""" | |
Return the number of jobs the printer has. | |
""" | |
# There are 2 information lines. So we subtract 2. | |
return len(self._query_output) - 2 | |
def _get_info(self): | |
""" | |
Execute the lpq command on each printer to gain information about it. | |
""" | |
output = subprocess.Popen("lpq -P " + self.name, | |
shell=True, stdout=subprocess.PIPE) | |
return output.stdout.readlines() | |
PRINTER_NAMES = ["at3", "at13c", "at14", "at7", "at10c", "at17"] | |
PRINTERS = [Printer(name) for name in PRINTER_NAMES] | |
for printer in PRINTERS: | |
print printer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment