ion icon indicating copy to clipboard operation
ion copied to clipboard

pycups for Printing

Open tcyrus opened this issue 8 years ago • 5 comments

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

tcyrus avatar Jun 07 '16 17:06 tcyrus

Worth noting that this means Ion has a hard dependency on CUPS, which is probably fine.

an-empty-string avatar Jun 07 '16 17:06 an-empty-string

The Vagrant VM should have CUPS installed already.

tcyrus avatar Jun 07 '16 17:06 tcyrus

Are we really willing to run a CUPS server in the development environment, though? If not, do we want to connect to production?

an-empty-string avatar Jun 07 '16 17:06 an-empty-string

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.

pefoley2 avatar Jun 07 '16 18:06 pefoley2

For the record. the last release of pycups was in October of 2018.

theo-o avatar Jul 26 '19 16:07 theo-o