| 17 | |
| 18 | |
| 19 | class postscript(printer): |
| 20 | # -------------------------------------------------------------------- |
| 21 | # send PostScript command to printer, optionally receive response |
| 22 | def cmd(self, str_send, fb=True, crop=True, binary=False): |
| 23 | str_recv = "" # response buffer |
| 24 | if self.iohack: |
| 25 | str_send = '{' + str_send + '} stopped' # br-script workaround |
| 26 | # unique response delimiter |
| 27 | token = c.DELIMITER + str(random.randrange(2**16)) |
| 28 | iohack = c.PS_IOHACK if self.iohack else '' # optionally include output hack |
| 29 | # additional line feed necessary |
| 30 | footer = '\n(' + token + '\\n) print flush\n' |
| 31 | # send command to printer device # to get output on some printers |
| 32 | try: |
| 33 | cmd_send = c.UEL + c.PS_HEADER + iohack + str_send + footer # + c.UEL |
| 34 | # write to logfile |
| 35 | log().write(self.logfile, str_send + os.linesep) |
| 36 | # sent to printer |
| 37 | self.send(cmd_send) |
| 38 | # use random token or error message as delimiter PS responses |
| 39 | str_recv = self.recv(token + ".*$" + "|" + c.PS_FLUSH, fb, crop, binary) |
| 40 | return self.ps_err(str_recv) |
| 41 | |
| 42 | # handle CTRL+C and exceptions |
| 43 | except (KeyboardInterrupt, Exception) as e: |
| 44 | self.reconnect(str(e)) |
| 45 | return "" |
| 46 | |
| 47 | # send PostScript command, cause permanent changes |
| 48 | def globalcmd(self, str_send, *stuff): |
| 49 | return self.cmd(c.PS_GLOBAL + str_send, *stuff) |
| 50 | |
| 51 | # send PostScript command, bypass invalid access |
| 52 | def supercmd(self, str_send, *stuff): |
| 53 | return self.cmd('{' + str_send + '}' + c.PS_SUPER, *stuff) |
| 54 | |
| 55 | # handle error messages from PostScript interpreter |
| 56 | def ps_err(self, str_recv): |
| 57 | self.error = None |
| 58 | msg = item(re.findall(c.PS_ERROR, str_recv)) |
| 59 | if msg: # real postscript command errors |
| 60 | output().errmsg("PostScript Error", msg) |
| 61 | self.error = msg |
| 62 | str_recv = "" |
| 63 | else: # printer errors or status messages |
| 64 | msg = item(re.findall(c.PS_CATCH, str_recv)) |
| 65 | if msg: |
| 66 | self.chitchat("Status Message: '" + msg.strip() + "'") |
| 67 | str_recv = re.sub(r'' + c.PS_CATCH + '\r?\n', '', str_recv) |
| 68 | return str_recv |
| 69 | |
| 70 | # disable printing hard copies of error messages |
| 71 | def on_connect(self, mode): |
| 72 | if mode == 'init': # only for the first connection attempt |
| 73 | str_send = '(x1) = (x2) ==' # = original, == overwritten |
| 74 | str_send += ' << /DoPrintErrors false >> setsystemparams' |
| 75 | str_recv = self.cmd(str_send) |
| 76 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |