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
async def new_project_cb(req: rpc.project.NewProjectRequest, ui: WsClient) -> None: if glob.PACKAGE_STATE.state in (common.PackageStateEnum.PAUSED, common.PackageStateEnum.RUNNING): raise Arcor2Exception("Can't create project while package runs.") unique_name(req.args.name, (await project_names())) if req.dry_run: return None if glob.SCENE: if glob.SCENE.id != req.args.scene_id: raise Arcor2Exception("Another scene is opened.") if glob.SCENE.has_changes(): await storage.update_scene(glob.SCENE) glob.SCENE.modified = (await storage.get_scene(glob.SCENE.id)).modified else: if req.args.scene_id not in { scene.id for scene in (await storage.get_scenes()).items }: raise Arcor2Exception("Unknown scene id.") await open_scene(req.args.scene_id) glob.PROJECT = common.Project(common.uid(), req.args.name, req.args.scene_id, desc=req.args.desc, has_logic=req.args.has_logic) assert glob.SCENE asyncio.ensure_future( notif.broadcast_event( events.OpenProject( data=events.OpenProjectData(glob.SCENE, glob.PROJECT)))) return None
async def new_project_cb(req: srpc.p.NewProject.Request, ui: WsClient) -> None: if glob.PACKAGE_STATE.state in PackageState.RUN_STATES: raise Arcor2Exception("Can't create project while package runs.") unique_name(req.args.name, (await project_names())) if req.dry_run: return None if glob.SCENE: if glob.SCENE.id != req.args.scene_id: raise Arcor2Exception("Another scene is opened.") if glob.SCENE.has_changes(): glob.SCENE.modified = await storage.update_scene(glob.SCENE.scene) else: if req.args.scene_id not in { scene.id for scene in (await storage.get_scenes()).items }: raise Arcor2Exception("Unknown scene id.") await open_scene(req.args.scene_id) project.PREV_RESULTS.clear() glob.PROJECT = UpdateableCachedProject( common.Project(req.args.name, req.args.scene_id, desc=req.args.desc, has_logic=req.args.has_logic)) assert glob.SCENE asyncio.ensure_future( notif.broadcast_event( sevts.p.OpenProject( sevts.p.OpenProject.Data(glob.SCENE.scene, glob.PROJECT.project)))) return None
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)