def _readline(self): try: try: try: line = self.printer.readline().decode('ascii') except UnicodeDecodeError: self.logError( _("Got rubbish reply from %s at baudrate %s:") % (self.port, self.baud) + "\n" + _("Maybe a bad baudrate?")) return None if self.printer_tcp and not line: raise OSError(-1, "Read EOF from socket") except socket.timeout: return "" if len(line) > 1: self.log.append(line) for handler in self.event_handler: try: handler.on_recv(line) except: logging.error(traceback.format_exc()) if self.recvcb: try: self.recvcb(line) except: self.logError(traceback.format_exc()) if self.loud: logging.info("RECV: %s" % line.rstrip()) return line except SelectError as e: if 'Bad file descriptor' in e.args[1]: self.logError( _("Can't read from printer (disconnected?) (SelectError {0}): {1}" ).format(e.errno, decode_utf8(e.strerror))) return None else: self.logError( _("SelectError ({0}): {1}").format(e.errno, decode_utf8( e.strerror))) raise except SerialException as e: self.logError( _("Can't read from printer (disconnected?) (SerialException): {0}" ).format(decode_utf8(str(e)))) return None except socket.error as e: self.logError( _("Can't read from printer (disconnected?) (Socket error {0}): {1}" ).format(e.errno, decode_utf8(e.strerror))) return None except OSError as e: if e.errno == errno.EAGAIN: # Not a real error, no data was available return "" self.logError( _("Can't read from printer (disconnected?) (OS Error {0}): {1}" ).format(e.errno, e.strerror)) return None
def _send(self, command, lineno=0, calcchecksum=False): # Only add checksums if over serial (tcp does the flow control itself) if calcchecksum and not self.printer_tcp: prefix = "N" + str(lineno) + " " + command command = prefix + "*" + str(self._checksum(prefix)) if "M110" not in command: self.sentlines[lineno] = command if self.printer: self.sent.append(command) # run the command through the analyzer gline = None try: gline = self.analyzer.append(command, store=False) except: logging.warning( _("Could not analyze command %s:") % command + "\n" + traceback.format_exc()) if self.loud: logging.info("SENT: %s" % command) for handler in self.event_handler: try: handler.on_send(command, gline) except: logging.error(traceback.format_exc()) if self.sendcb: try: self.sendcb(command, gline) except: self.logError(traceback.format_exc()) try: self.printer.write(str(command + "\n")) if self.printer_tcp: try: self.printer.flush() except socket.timeout: pass self.writefailures = 0 except socket.error as e: if e.errno is None: self.logError( _(u"Can't write to printer (disconnected ?):") + "\n" + traceback.format_exc()) else: self.logError( _(u"Can't write to printer (disconnected?) (Socket error {0}): {1}" ).format(e.errno, decode_utf8(e.strerror))) self.writefailures += 1 except SerialException as e: self.logError( _(u"Can't write to printer (disconnected?) (SerialException): {0}" ).format(decode_utf8(str(e)))) self.writefailures += 1 except RuntimeError as e: self.logError( _(u"Socket connection broken, disconnected. ({0}): {1}"). format(e.errno, decode_utf8(e.strerror))) self.writefailures += 1
B_cards = [] # utils.read_param('Bob/cards.json', 'cards') for idx in range(5): B_cards.append(utils.read_param('Bob/Eb({}).json'.format(idx), 'card')) C_cards = [] # utils.read_param('Carroll/cards.json', 'cards') for idx in range(5): C_cards.append(utils.read_param('Carroll/Ec({}).json'.format(idx), 'card')) B_sk = utils.read_param('Bob/B_sk.json', 'key') C_sk = utils.read_param('Carroll/C_sk.json', 'key') print('Eb(Bcards)\t\t', B_cards) B_cards = decrypt(B_sk, B_cards) print('Bcards\t\t', B_cards) print('Ec(Ccards)\t', C_cards) C_cards = decrypt(C_sk, C_cards) print('Ccards\t\t', C_cards) # utils.save_param('Bob/cards.json', 'cards', utils.decode_utf8(B_cards)) for idx, item in enumerate(utils.decode_utf8(B_cards)): utils.save_param('Bob/decode_card_{}.json'.format(idx), 'card', item[0]) utils.save_param('Bob/decode_card_{}.json'.format(idx), 'num', item[1]) # utils.save_param('Carroll/cards.json', 'cards', utils.decode_utf8(C_cards)) for idx, item in enumerate(utils.decode_utf8(C_cards)): utils.save_param('Carroll/decode_card_{}.json'.format(idx), 'card', item[0]) utils.save_param('Carroll/decode_card_{}.json'.format(idx), 'num', item[1]) if step == 8 or debug: # Carroll deck = [] # utils.read_param('Carroll/deck42.json', 'deck') for idx in range(42): deck.append(utils.read_param('Carroll/new_{}.json'.format(idx), 'card')) new_deck, cards = choose_card(deck, 5) # utils.save_param('Alice/cards.json', 'cards', cards) for idx, item in enumerate(cards): utils.save_param('Alice/Ea({}).json'.format(idx), 'card', item) if step == 9 or debug: # Alice
def prepare_str_for_rouge(str): return decode_utf8(remove_stopwords(str))