Ejemplo n.º 1
0
    def request_dto_computer_key():
        dto0 = RequestDto[ComputerKey](data=ComputerKey("5", "40"))
        json0 = JsonFormatter.serialize(dto0)
        val0 = JsonFormatter.deserialize(json0, RequestDto[ComputerKey])
        print(val0)

        print(RequestDto.__annotations__["data"])

        dto1 = RequestDto[ComputerManager.GetKeysByAuditoriumDto](
            data=ComputerManager.GetKeysByAuditoriumDto("37"))
        json1 = JsonFormatter.serialize(dto1)
        val1 = JsonFormatter.deserialize(
            json1, RequestDto[ComputerManager.GetKeysByAuditoriumDto])
        print(val1)
        print(RequestDto.__annotations__["data"])
Ejemplo n.º 2
0
    def request_dto_serialize():
        dto = RequestDto[ComputerFlow]("some_method",
                                       ComputerFlow.get_random_flow())
        json_dto = JsonFormatter.serialize(dto)
        dto_value = JsonFormatter.deserialize(json_dto,
                                              RequestDto[ComputerFlow])
        print(dto_value)

        # repeat

        json_dto2 = JsonFormatter.serialize(dto_value)
        dto_value2 = JsonFormatter.deserialize(json_dto2,
                                               RequestDto[ComputerFlow])
        print(dto_value2
              )  # needed for breakpoint to check the result of formatting
Ejemplo n.º 3
0
 def __read_from_config(cls) -> Union[UrlInfo, None]:
     if os.path.exists(cls.__file_name):
         with open(cls.__file_name) as file:
             json = file.read()
             if len(json) > 0:
                 return JsonFormatter.deserialize(json, UrlInfo)
     return None
    def __read_temp_data(cls) -> ComputerFlow:
        temp_data: str = ""

        if os.path.exists(cls.__temp_data_file):
            with open(cls.__temp_data_file, "r") as file:
                temp_data = file.read()

        return JsonFormatter.deserialize(
            temp_data, ComputerFlow) if temp_data else ComputerFlow()
Ejemplo n.º 5
0
    def read_all(self) -> List[Computer]:
        if len(self.__cache) == super().count():
            return [*self.__cache.values()]

        read_all_result = super().read_all()
        computers = []
        for name, auditorium, json_data in read_all_result:
            computer: Computer = JsonFormatter.deserialize(json_data, Computer)
            self.__cache[ComputerKey(name, auditorium)] = computer
            computers.append(computer)
        return computers
Ejemplo n.º 6
0
    def get_request_dto(self, json_request_dto: str, method_name: str,
                        client_address: Tuple[str, int]) -> BaseRequestDto:
        dto_type = self.__get_request_type(method_name)
        request_dto = JsonFormatter.deserialize(json_request_dto, dto_type)

        if not hasattr(request_dto, "data"):
            raise ServerLogicException(
                400, f"Server work only with dto types ! Stop spam !")

        UrlManager.resolve_client_address(request_dto, client_address)
        return request_dto
Ejemplo n.º 7
0
    def read(self, key: ComputerKey) -> Union[Computer, None]:
        computer = self.__cache.get(key)
        if computer is not None:
            return computer

        *_, value = super().read(key) or (None, None)
        if value is not None:
            computer = JsonFormatter.deserialize(value, Computer)
            self.__cache[key] = computer
            return computer

        return None
Ejemplo n.º 8
0
    def __read_config_from_file(file_name: str) -> ClientConfig:
        if os.path.exists(file_name):
            with open(file_name, "r") as file:
                file_content = file.read()
        else:
            raise Exception(f"Can not open file with Client config (file: {file_name})")

        if file_content:
            config = JsonFormatter.deserialize(file_content, ClientConfig)
            if ConfigManager.__validate_config(config):
                return config

        raise Exception(f"Client config in {file_name} is invalid, check the file")
Ejemplo n.º 9
0
 def __read_keys_from_file(cls):
     if os.path.exists(cls.__file_name):
         with open(cls.__file_name, "r") as file:
             keys = file.read()
         return JsonFormatter.deserialize(
             keys, ComputerKeysData) if keys else None
Ejemplo n.º 10
0
 def client_config():
     c = ClientConfig(None, None)
     json = JsonFormatter.serialize(c)
     val = JsonFormatter.deserialize(json, ClientConfig)
     print(val.__dict__)
     print(val)
Ejemplo n.º 11
0
 def computer_serialize():
     computer = Computer.get_random_computer()
     json = JsonFormatter.serialize(computer)
     val = JsonFormatter.deserialize(json, Computer)
     print(val)
Ejemplo n.º 12
0
 def dict_field_serialize():
     some_type = SomeType.get_random_some_type()
     json = JsonFormatter.serialize(some_type)
     val = JsonFormatter.deserialize(json, SomeType)
     print(val)
Ejemplo n.º 13
0
 def request_complex_dto_serialize():
     dto = SomeEntity.get_temp_some_entity()
     json_dto = JsonFormatter.serialize(dto)
     dto_val = JsonFormatter.deserialize(json_dto, SomeEntity)
     print(dto_val)
Ejemplo n.º 14
0
 def computer_flow_serialize():
     computer_flow = ComputerFlow.get_random_flow()
     computer_flow_json = JsonFormatter.serialize(computer_flow)
     val = JsonFormatter.deserialize(computer_flow_json, ComputerFlow)
     print(val)
Ejemplo n.º 15
0
 def serialize_test():
     person: Person = Person(13, "Max")
     json = JsonFormatter.serialize(person)
     person_val = JsonFormatter.deserialize(json, Person)
     print(person_val)
Ejemplo n.º 16
0
 def __read_from_server() -> Union[ClientConfig, None]:
     json: str = NetworkManager.get("GetClientConfig")
     return JsonFormatter.deserialize(
         json, ResponseDto[ClientConfig]).data if json else None