class PrinterAdapter(AbstractPrinterAdapter): """Provides the basic functionality of wrapping the printer and allowing data to be printed. """ def __init__(self, printer_name): """Initializes the adapter""" self._printer_name = printer_name self._connection = Connection() def print_data(self, data_file_str, title_str, options): """Prints the given data with the given information. @param data_file_str: str representing the file to be printed. @param title_str: str representing the title that the printer job should be associated with. @param options: dict of options that the should be associated with the printer job. @return: bool value representing if the printer was successful in scheduling the job. """ value = self._connection.printFile(self._printer_name, data_file_str, title_str, options) return value > 0
class PrinterController: def __init__(self): from cups import Connection self.connect = Connection() def scan(self): printers = self.connect.getPrinters() for printer in printers: print(printer, printers[printer]["device-uri"]) def set_printer(self): return list(self.connect.getPrinters()) def print(self, printer, filename, title=None, *args, **kwargs): if not title: title = "" self.connect.printFile(printer, filename, title, kwargs)
class PublishController(object): def __init__(self): self.conn = Connection() self.printers = self.conn.getPrinters() self.check_printer_status() self.job_id = None def check_printer_status(self): is_selphy = [] for printer in self.printers: is_selphy.append(printer == CONST["PRINTER_NAME"]) if any(is_selphy): print("printer {} is available.".format(CONST["PRINTER_NAME"])) return True else: print("Printer {} is missing.".format(CONST["PRINTER_NAME"])) return False def start_print(self): self.conn.enablePrinter(CONST["PRINTER_NAME"]) self.job_id = self.conn.printFile( CONST["PRINTER_NAME"], "ramdisk/polaroid.jpg", "{event}-{date}".format( event=CONST["EVENT_NAME"], date=datetime.now().strftime("%Y%m%d_%H-%M")), {}, ) self.save_image() def check_print_done(self): if self.conn.getJobs().get(self.job_id, None) is not None: print("Still printing...") return False return True def save_image(self): filepath = "events/{event_name}/{date_time}.jpg".format( event_name=CONST["EVENT_NAME"], date_time=format_time(datetime.now(), True)) """ create folder """ if not os.path.exists(os.path.dirname(filepath)): try: os.makedirs(os.path.dirname(filepath)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise image = Image.open("ramdisk/polaroid.jpg") image.save(filepath)