File: PrinterInterface_W.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (39 lines) | stat: -rw-r--r-- 1,468 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging
import win32print

from odoo.addons.hw_drivers.interface import Interface

_logger = logging.getLogger(__name__)


class PrinterInterface(Interface):
    _loop_delay = 30
    connection_type = 'printer'

    def get_devices(self):
        printer_devices = {}
        printers = win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL)

        for printer in printers:
            identifier = printer[2]
            handle_printer = win32print.OpenPrinter(identifier)
            # The value "2" is the level of detail we want to get from the printer, see:
            # https://learn.microsoft.com/en-us/windows/win32/printdocs/getprinter#parameters
            printer_details = win32print.GetPrinter(handle_printer, 2)
            printer_port = None
            if printer_details:
                # see: https://learn.microsoft.com/en-us/windows/win32/printdocs/printer-info-2#members
                printer_port = printer_details.get('pPortName')
            if printer_port is None:
                _logger.warning('Printer "%s" has no port name. Used dummy port', identifier)
                printer_port = 'IOT_DUMMY_PORT'

            printer_devices[identifier] = {
                'identifier': identifier,
                'printer_handle': handle_printer,
                'port': printer_port,
            }
        return printer_devices