|
| 1 | +# Copyright (c) 2015-2024 EPFL/Blue Brain Project |
| 2 | +# All rights reserved. Do not distribute without permission. |
| 3 | +# |
| 4 | +# Responsible Author: [email protected] |
| 5 | +# |
| 6 | +# This file is part of Brayns <https://github.com/BlueBrain/Brayns> |
| 7 | +# |
| 8 | +# This library is free software; you can redistribute it and/or modify it under |
| 9 | +# the terms of the GNU Lesser General Public License version 3.0 as published |
| 10 | +# by the Free Software Foundation. |
| 11 | +# |
| 12 | +# This library is distributed in the hope that it will be useful, but WITHOUT |
| 13 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 | +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 15 | +# details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public License |
| 18 | +# along with this library; if not, write to the Free Software Foundation, Inc., |
| 19 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + |
| 21 | +from dataclasses import dataclass |
| 22 | +from typing import Any |
| 23 | + |
| 24 | +from brayns.network.connection import Connection |
| 25 | +from brayns.utils.parsing import check_type, get, try_get |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class Object: |
| 30 | + id: int |
| 31 | + type: str |
| 32 | + user_data: Any |
| 33 | + |
| 34 | + |
| 35 | +def parse_object(message: dict[str, Any]) -> Object: |
| 36 | + return Object( |
| 37 | + id=get(message, "id", int), |
| 38 | + type=get(message, "type", str), |
| 39 | + user_data=try_get(message, "user_data", Any, None), |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +async def get_all_objects(connection: Connection) -> list[Object]: |
| 44 | + result = await connection.get_result("get-all-objects") |
| 45 | + check_type(result, dict[str, Any]) |
| 46 | + objects = get(result, "objects", list[dict[str, Any]]) |
| 47 | + return [parse_object(item) for item in objects] |
| 48 | + |
| 49 | + |
| 50 | +async def get_object(connection: Connection, id: int) -> Object: |
| 51 | + result = await connection.get_result("get-object", {"id": id}) |
| 52 | + check_type(result, dict[str, Any]) |
| 53 | + return parse_object(result) |
| 54 | + |
| 55 | + |
| 56 | +async def update_object(connection: Connection, id: int, user_data: Any) -> None: |
| 57 | + properties = {"user_data": user_data} |
| 58 | + await connection.get_result("update-object", {"id": id, "properties": properties}) |
| 59 | + |
| 60 | + |
| 61 | +async def remove_objects(connection: Connection, ids: list[int]) -> None: |
| 62 | + await connection.get_result("remove-objects", {"ids": ids}) |
| 63 | + |
| 64 | + |
| 65 | +async def clear_objects(connection: Connection) -> None: |
| 66 | + await connection.get_result("clear-objects") |
| 67 | + |
| 68 | + |
| 69 | +async def create_empty_object(connection: Connection) -> int: |
| 70 | + result = await connection.get_result("create-empty-object") |
| 71 | + check_type(result, dict[str, Any]) |
| 72 | + return get(result, "id", int) |
0 commit comments