ion
ion copied to clipboard
pycups for Printing
pycups
can be used for the printing module instead of creating subprocesses
Here's an example:
This uses pycups
import cups
def get_printers():
try:
conn = cups.Connection()
names = conn.getPrinters().keys()
# Don't die if cups isn't installed.
except cups.IPPError:
return []
if "Please_Select_a_Printer" in names:
names.remove("Please_Select_a_Printer")
if "" in names:
names.remove("")
return names
This uses subprocess
import subprocess
def get_printers():
try:
output = subprocess.check_output(["lpstat", "-a"], universal_newlines=True)
# Don't die if cups isn't installed.
except FileNotFoundError:
return []
lines = output.splitlines()
names = []
for l in lines:
if "requests since" in l:
names.append(l.split(" ", 1)[0])
if "Please_Select_a_Printer" in names:
names.remove("Please_Select_a_Printer")
if "" in names:
names.remove("")
return names
Worth noting that this means Ion has a hard dependency on CUPS, which is probably fine.
The Vagrant VM should have CUPS installed already.
Are we really willing to run a CUPS server in the development environment, though? If not, do we want to connect to production?
Fwiw, master currently dies if cups isn't installed, but I've fixed that in dev. @tcyrus feel free to make a pr moving us to pycups, but it needs to fallback cleanly if cups isn't available.
For the record. the last release of pycups was in October of 2018.