def _return_collector(self): try: while self.is_open(): try: ret = self._object_packer.loads(self._tcp_connection.get_data()) if ret["id"] in self._generator_ids: self._return_data[ret["id"]].append(ret) else: self._return_data[ret["id"]] = ret except Exception as e: self._return_data["error"] = e except Exception as e: log.critical("_return_collector error: {}".format(e))
def _action_thread(self, action): """ info: will run an action :param action: bytes :return: """ try: # unpack action action_dict = self._object_packer.loads(action) # uncompress data if "optimizer" in action_dict: # init lase received cache if target does not have one if action_dict["target"] not in self._transport_optimizers: self._transport_optimizers[action_dict["target"]] = transport_optimizer.LastReceivedCache() action_dict = self._transport_optimizers[action_dict["target"]](action_dict["optimizer"]) action_dict = self._object_packer.loads(action_dict) # get action id action_id = action_dict.get("id") if "none_generator" in action_dict: try: func = getattr(self._obj, action_dict["target"]) # call func for ret in func(*action_dict.get("args", ()), **action_dict.get("kwargs", {})): if ret is not None: self._tcp_connection.send_data(self._object_packer.dumps({"id": action_id, "return": ret})) else: sleep(0.00001) self._tcp_connection.send_data(self._object_packer.dumps({"id": action_id, "error": "StopIteration"})) except Exception as e: # return error if action_id is not None: self._tcp_connection.send_data(self._object_packer.dumps({"id": action_id, "error": str(e)})) raise e else: try: func = getattr(self._obj, action_dict["target"]) # call func ret = func(*action_dict.get("args", ()), **action_dict.get("kwargs", {})) # return data if action_id is not None: self._tcp_connection.send_data(self._object_packer.dumps({"id": action_id, "return": ret})) except Exception as e: # return error if action_id is not None: self._tcp_connection.send_data(self._object_packer.dumps({"id": action_id, "error": str(e)})) raise e except Exception as e: log.critical("_action_thread error: {} data: {}".format(e, action))
def open_sound(self, file, sound_id): """ info: will open .wav sound :param file: str :param sound_id: int :return: None """ if sound_id in self._active_sound_ids: log.critical("Sound ID \"{}\" all ready in use!".format(sound_id)) raise TAMSoundError("Sound ID \"{}\" all ready in use!".format(sound_id)) if not file.lower().endswith(".wav"): raise TAMSoundError("File \"{0}\" must end with \".wav\"".format(file)) self._active_sound_ids.add(sound_id)
def test_critical(self): try: log.enable_logging(log.CRITICAL) log.debug("test2") log.debug("cats2") log.critical("colors2") with open(log.LOG_FILE_NAME) as file: output = "".join(file.readlines()) self.assertNotIn("dogs", output) self.assertNotIn("test2", output) self.assertNotIn("cats2", output) self.assertIn("colors2", output) finally: log.disable_logging() remove(log.LOG_FILE_NAME)
def test_debug(self): try: log.enable_logging() log.debug("test") log.debug("dogs") log.critical("colors") with open(log.LOG_FILE_NAME) as file: output = "".join(file.readlines()) self.assertIn("test", output) self.assertIn("dogs", output) self.assertIn("colors", output) self.assertIn("test", log.LOG.read(0)) self.assertIn("dogs", log.LOG.read(1)) self.assertIn("colors", log.LOG.read(2)) finally: log.disable_logging() remove(log.LOG_FILE_NAME)
def run_application(cls, *args, **kwargs): """ info: will run tam loop as an application note: when tam loop is done running the program will quit if tam loop has an error and the frame does not catch it the error will be printed onto the screen and the program will quit after user input :param args: :param kwargs: :return: """ try: loop = cls(*args, **kwargs) loop() except KeyboardInterrupt: log.critical("Caught KeyboardInterrupt") except BaseException as error: try: log.critical("TAMLoop Error: {}".format(error)) traceback.print_exception(error.__class__, error, sys.exc_info()[2]) time.sleep(1) input("Press Enter To Continue . . .") except KeyboardInterrupt: log.critical("Caught KeyboardInterrupt") finally: sys.exit()
def run_tcp_connection(connection, io=None): """ info: will run a tcp connection :param connection: TCPConnection :param io: IO or None: None will uses default IO :return: None """ if io is None: io = tam_identifier.IO snapshot = io.get_snapshot() try: tcp.TCPObjectWrapper( connection, io, object_packer.ObjectPackerJson( (tam_colors.RGBA, tam_colors.Color, tam_surface.TAMSurface)))() except tcp.TCPError as error: log.warning("run_tcp_connection error: {}".format(error)) except KeyboardInterrupt: log.critical("Caught KeyboardInterrupt") finally: io.apply_snapshot(snapshot) io.clear()