示例#1
0
    def __init__(self, name='default', max_pages=-1):
        self._conn = cups.Connection() if cups else None
        self._notifier = Subscriber(self._conn) if cups else None
        self.name = None
        self.max_pages = max_pages
        self.nbr_printed = 0
        if not cups:
            LOGGER.warning(
                "No printer found (pycups or pycups-notify not installed)")
            return  # CUPS is not installed
        elif not name or name.lower() == 'default':
            self.name = self._conn.getDefault()
            if not self.name and self._conn.getPrinters():
                self.name = list(
                    self._conn.getPrinters().keys())[0]  # Take first one
        elif name in self._conn.getPrinters():
            self.name = name

        if not self.name:
            if name.lower() == 'default':
                LOGGER.warning(
                    "No printer configured in CUPS (see http://localhost:631)")
            else:
                LOGGER.warning(
                    "No printer named '%s' in CUPS (see http://localhost:631)",
                    name)
        else:
            LOGGER.info("Connected to printer '%s'", self.name)
示例#2
0
文件: printer.py 项目: jo-ei/pibooth
    def __init__(self,
                 name='default',
                 max_pages=-1,
                 options=None,
                 counters=None):
        self._conn = cups.Connection() if cups else None
        self._notifier = Subscriber(self._conn) if cups else None
        self.name = None
        self.max_pages = max_pages
        self.options = options
        self.count = counters
        if not cups:
            LOGGER.warning(
                "No printer found (pycups or pycups-notify not installed)")
            return  # CUPS is not installed

        if not name or name.lower() == 'default':
            self.name = self._conn.getDefault()
            if not self.name and self._conn.getPrinters():
                self.name = list(
                    self._conn.getPrinters().keys())[0]  # Take first one
        elif name in self._conn.getPrinters():
            self.name = name

        if not self.name:
            if name.lower() == 'default':
                LOGGER.warning(
                    "No printer configured in CUPS (see http://localhost:631)")
            else:
                LOGGER.warning(
                    "No printer named '%s' in CUPS (see http://localhost:631)",
                    name)
        else:
            LOGGER.info("Connected to printer '%s'", self.name)

        if self.options and not isinstance(self.options, dict):
            LOGGER.warning("Invalid printer options '%s', dict is expected",
                           self.options)
            self.options = {}
        elif not self.options:
            self.options = {}
示例#3
0
class Printer(object):
    def __init__(self, name='default', max_pages=-1):
        self._conn = cups.Connection() if cups else None
        self._notifier = Subscriber(self._conn) if cups else None
        self.name = None
        self.max_pages = max_pages
        self.nbr_printed = 0
        if not cups:
            LOGGER.warning(
                "No printer found (pycups or pycups-notify not installed)")
            return  # CUPS is not installed
        elif not name or name.lower() == 'default':
            self.name = self._conn.getDefault()
            if not self.name and self._conn.getPrinters():
                self.name = list(
                    self._conn.getPrinters().keys())[0]  # Take first one
        elif name in self._conn.getPrinters():
            self.name = name

        if not self.name:
            if name.lower() == 'default':
                LOGGER.warning(
                    "No printer configured in CUPS (see http://localhost:631)")
            else:
                LOGGER.warning(
                    "No printer named '%s' in CUPS (see http://localhost:631)",
                    name)
        else:
            LOGGER.info("Connected to printer '%s'", self.name)

    def _on_event(self, evt):
        """
        Call for each new printer event.
        """
        LOGGER.info(evt.title)
        pygame.event.post(
            pygame.event.Event(PRINTER_TASKS_UPDATED,
                               tasks=self.get_all_tasks()))

    def is_installed(self):
        """Return True if the CUPS server is available for printing.
        """
        return cups is not None and self.name is not None

    def is_available(self):
        """Return True is paper/ink counter is reached or printing is disabled.
        """
        if not self.is_installed():
            return False
        if self.max_pages < 0:  # No limit
            return True
        return self.nbr_printed < self.max_pages

    def print_file(self, filename, copies=1):
        """Send a file to the CUPS server to the default printer.
        """
        if not self.name:
            raise EnvironmentError(
                "No printer found (check config file or CUPS config)")
        if not osp.isfile(filename):
            raise IOError("No such file or directory: {}".format(filename))
        if self._notifier and not self._notifier.is_subscribed(self._on_event):
            self._notifier.subscribe(self._on_event, [
                event.CUPS_EVT_JOB_COMPLETED, event.CUPS_EVT_JOB_CREATED,
                event.CUPS_EVT_JOB_STOPPED,
                event.CUPS_EVT_PRINTER_STATE_CHANGED,
                event.CUPS_EVT_PRINTER_STOPPED
            ])

        if copies > 1:
            with tempfile.NamedTemporaryFile(
                    suffix=osp.basename(filename)) as fp:
                picture = Image.open(filename)
                factory = get_picture_factory((picture, ) * copies)
                # Don't call setup factory hook here, as the selected parameters
                # are the one necessary to render several pictures on same page.
                factory.set_margin(2)
                factory.save(fp.name)
                self._conn.printFile(self.name, fp.name,
                                     osp.basename(filename), {})
        else:
            self._conn.printFile(self.name, filename, osp.basename(filename),
                                 {})
        LOGGER.debug("File '%s' sent to the printer", filename)
        self.nbr_printed += 1

    def cancel_all_tasks(self):
        """Cancel all tasks in the queue.
        """
        if not self.name:
            raise EnvironmentError(
                "No printer found (check config file or CUPS config)")
        self._conn.cancelAllJobs(self.name)

    def get_all_tasks(self):
        """Return a dict (indexed by job ID) of dicts representing all tasks
        in the queue.
        """
        if not self.name:
            return {}  # No printer found
        return self._conn.getJobs(my_jobs=True,
                                  requested_attributes=[
                                      "job-id", "job-name", "job-uri",
                                      "job-state"
                                  ])

    def quit(self):
        """Do cleanup actions.
        """
        if self._notifier:
            self._notifier.unsubscribe_all()
示例#4
0
import os

from cups import Connection
from cups_notify import Subscriber

cups = Connection()
notifier = Subscriber(cups)

printer = os.getenv('PRINTER')