def _get_event_data(message: JsonValue) -> Tuple[str, str, JsonValue, str]: try: subsystem = json_get(message, "subsystem", str) event = json_get(message, "event", str) data = json_get(message, "data", dict) id = json_get(message, "id", str) except (ValueError, TypeError) as exc: logging.error("received invalid JSON: " + str(exc)) raise ValueError() return subsystem, event, data, id
async def handle(self, reader: StreamReader, _: StreamWriter) -> None: while True: try: message = await read_json_line(reader) except DisconnectedError: break action = json_get(message, "action", str) if action == "notify": self._notify_dispatcher(message) else: logging.warning(f"received unknown action '{action}'")
def read(self, key: str, obj_path: str = None): try: if not key: raise KeyError("key is not provided to read args") config = self.__read(self.file_path) if not obj_path: if key not in config: return None return config[key] else: return json_get(config, self.__get_obj_path(key, obj_path)) except Exception as ex: logging.exception(ex) raise ex
def _get_value(self, message: JsonValue) -> Any: try: v = json_get(message, self._field, self.field_type) except (ValueError, TypeError): raise ValueError() return self.parse_value(cast(Any, v))
def test_remove_recursive(self): data1 = { "name1": "val1", "name2": "val2", "sample": { "a1": { "test1": "testValue", "test2": "testValue2" } } } Collections.remove_field_recursive(data1, 'name1') assert "name1" not in data1 data2 = { "sample": { "a1": { "test1": "testValue", "test2": "testValue2" } } } Collections.remove_field_recursive(data2, 'sample.a1.test1') assert "test1" not in json_get(data2, "/sample/a1") data3 = { "list": [{ "key": "1", "value": "2" }, { "key": "3", "value": "4" }] } Collections.remove_field_recursive(data3, 'list.key') list = data3["list"] for l in list: assert "key" not in l data4 = { "list": [{ "key": "1", "value": { "name": "1", "lastName": "a" } }, { "key": "3" }] } Collections.remove_field_recursive(data4, 'list.value.name') list = data4["list"] assert "name" not in list[0]['value'] data5 = [{"key": "1", "value": "2"}, {"key": "3", "value": "4"}] Collections.remove_field_recursive(data5, 'key') for l in data5: assert "key" not in l data6 = [{ "key": "1", "value": { "name": "1", "lastName": "a" } }, { "key": "3", "value": { "name": "2", "lastName": "ab" } }] Collections.remove_field_recursive(data6, 'value.name') for l in data5: assert "name" not in l['value']