Esempio n. 1
0
def lock() -> Lock:
    """Creates lock with initialized scene and project."""
    test = "test"
    lock = Lock({})

    scene = UpdateableCachedScene(cmn.Scene(test, description=test))
    lock.scene = scene
    project = UpdateableCachedProject(cmn.Project(test, lock.scene.id, description=test, has_logic=True))
    lock.project = project

    assert lock.scene == scene
    assert lock.scene_or_exception() == scene

    assert lock.project == project
    assert lock.project_or_exception() == project

    # add some scene and project objects
    test_object = cmn.SceneObject(test, "TestType")
    lock.scene.upsert_object(test_object)
    ap = lock.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap", cmn.Position(0, 0, 0))
    ap_ap = lock.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap_ap", cmn.Position(0, 0, 1), ap.id)
    ap_ap_ap = lock.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap_ap_ap", cmn.Position(0, 0, 2), ap_ap.id)
    lock.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap2", cmn.Position(0, 1, 0))
    ori = cmn.NamedOrientation("ori", cmn.Orientation())
    lock.project.upsert_orientation(ap_ap_ap.id, ori)
    action = cmn.Action("action", "test/type", parameters=[], flows=[])
    lock.project.upsert_action(ap_ap_ap.id, action)
    return lock
Esempio n. 2
0
async def new_scene_cb(req: rpc.scene.NewSceneRequest, ui: WsClient) -> None:
    """
    Creates and opens a new scene on the server. Fails if any scene is open or if scene id/name already exists.
    :param req:
    :return:
    """

    if glob.PACKAGE_STATE.state in (common.PackageStateEnum.PAUSED,
                                    common.PackageStateEnum.RUNNING):
        raise Arcor2Exception("Can't create scene while package runs.")

    assert glob.SCENE is None

    for scene_id in (await storage.get_scenes()).items:
        if req.args.name == scene_id.name:
            raise Arcor2Exception("Name already used.")

    if req.dry_run:
        return None

    glob.SCENE = common.Scene(common.uid(), req.args.name, desc=req.args.desc)
    asyncio.ensure_future(
        notif.broadcast_event(
            events.OpenScene(data=events.OpenSceneData(glob.SCENE))))
    return None
Esempio n. 3
0
async def new_scene_cb(req: srpc.s.NewScene.Request, ui: WsClient) -> None:
    """Creates and opens a new scene on the server. Fails if any scene is open
    or if scene id/name already exists.

    :param req:
    :return:
    """

    if glob.PACKAGE_STATE.state in PackageState.RUN_STATES:
        raise Arcor2Exception("Can't create scene while package runs.")

    assert glob.SCENE is None

    for scene_id in (await storage.get_scenes()).items:
        if req.args.name == scene_id.name:
            raise Arcor2Exception("Name already used.")

    if req.dry_run:
        return None

    await get_object_types()  # TODO not ideal, may take quite long time
    glob.SCENE = UpdateableCachedScene(
        common.Scene(req.args.name, desc=req.args.desc))
    asyncio.ensure_future(
        notif.broadcast_event(
            sevts.s.OpenScene(sevts.s.OpenScene.Data(glob.SCENE.scene))))
    asyncio.ensure_future(scene_srv.delete_all_collisions())  # just for sure
    return None
Esempio n. 4
0
async def new_scene_cb(req: srpc.s.NewScene.Request, ui: WsClient) -> None:
    """Creates and opens a new scene on the server. Fails if any scene is open
    or if scene id/name already exists.

    :param req:
    :return:
    """

    async with glob.LOCK.get_lock(dry_run=req.dry_run):
        if glob.PACKAGE_STATE.state in PackageState.RUN_STATES:
            raise Arcor2Exception("Can't create scene while package runs.")

        if glob.LOCK.scene:
            raise Arcor2Exception("Scene has to be closed first.")

        for scene_id in await storage.get_scenes():
            if req.args.name == scene_id.name:
                raise Arcor2Exception("Name already used.")

        if req.dry_run:
            return None

        await get_object_types()  # TODO not ideal, may take quite long time
        glob.LOCK.scene = UpdateableCachedScene(
            common.Scene(req.args.name, description=req.args.description))
        asyncio.ensure_future(
            notify_scene_opened(
                sevts.s.OpenScene(sevts.s.OpenScene.Data(
                    glob.LOCK.scene.scene))))
        asyncio.ensure_future(
            scene_srv.delete_all_collisions())  # just for sure
        return None
Esempio n. 5
0
async def test_ctx_read_lock() -> None:

    test = "test"
    user = "******"

    glob.LOCK = Lock({})
    assert await glob.LOCK.get_locked_roots_count() == 0

    glob.LOCK.scene = UpdateableCachedScene(cmn.Scene(test, description=test))
    glob.LOCK.project = UpdateableCachedProject(cmn.Project(test, glob.LOCK.scene.id, description=test, has_logic=True))

    async def patch() -> set[str]:
        return {glob.LOCK.project_or_exception().id, glob.LOCK.scene_or_exception().id}

    storage.get_project_ids = storage.get_scene_ids = patch

    # add some scene and project objects
    test_object = cmn.SceneObject(test, "TestType")
    glob.LOCK.scene.upsert_object(test_object)
    ap = glob.LOCK.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap", cmn.Position(0, 0, 0), test_object.id)
    ap_ap = glob.LOCK.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap_ap", cmn.Position(0, 0, 1), ap.id)

    assert await glob.LOCK.get_locked_roots_count() == 0

    await glob.LOCK.write_lock(ap_ap.id, user, True)

    assert await glob.LOCK.is_write_locked(test_object.id, user)
    assert await glob.LOCK.is_write_locked(ap.id, user)
    assert await glob.LOCK.is_write_locked(ap_ap.id, user)

    async with ctx_read_lock(test_object.id, user):
        pass

    assert await glob.LOCK.is_write_locked(test_object.id, user)
    assert await glob.LOCK.is_write_locked(ap.id, user)
    assert await glob.LOCK.is_write_locked(ap_ap.id, user)