fps_yrooms 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,59 @@
1
+ # Licensing terms
2
+
3
+ This project is licensed under the terms of the Modified BSD License
4
+ (also known as New or Revised or 3-Clause BSD), as follows:
5
+
6
+ - Copyright (c) 2026-, Jupyter Development Team
7
+
8
+ All rights reserved.
9
+
10
+ Redistribution and use in source and binary forms, with or without
11
+ modification, are permitted provided that the following conditions are met:
12
+
13
+ Redistributions of source code must retain the above copyright notice, this
14
+ list of conditions and the following disclaimer.
15
+
16
+ Redistributions in binary form must reproduce the above copyright notice, this
17
+ list of conditions and the following disclaimer in the documentation and/or
18
+ other materials provided with the distribution.
19
+
20
+ Neither the name of the Jupyter Development Team nor the names of its
21
+ contributors may be used to endorse or promote products derived from this
22
+ software without specific prior written permission.
23
+
24
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ ## About the Jupyter Development Team
36
+
37
+ The Jupyter Development Team is the set of all contributors to the Jupyter project.
38
+ This includes all of the Jupyter subprojects.
39
+
40
+ The core team that coordinates development on GitHub can be found here:
41
+ https://github.com/jupyter/.
42
+
43
+ ## Our Copyright Policy
44
+
45
+ Jupyter uses a shared copyright model. Each contributor maintains copyright
46
+ over their contributions to Jupyter. But, it is important to note that these
47
+ contributions are typically only changes to the repositories. Thus, the Jupyter
48
+ source code, in its entirety is not the copyright of any single person or
49
+ institution. Instead, it is the collective copyright of the entire Jupyter
50
+ Development Team. If individual contributors want to maintain a record of what
51
+ changes/contributions they have specific copyright on, they should indicate
52
+ their copyright in the commit message of the change, when they commit the
53
+ change to one of the Jupyter repositories.
54
+
55
+ With this in mind, the following banner should be used in any source code file
56
+ to indicate the copyright and license terms:
57
+
58
+ # Copyright (c) Jupyter Development Team.
59
+ # Distributed under the terms of the Modified BSD License.
@@ -0,0 +1,30 @@
1
+ Metadata-Version: 2.4
2
+ Name: fps_yrooms
3
+ Version: 0.1.0
4
+ Summary: An FPS plugin for YRooms
5
+ Keywords: jupyter,server,fastapi,plugins
6
+ Author: Jupyter Development Team
7
+ Author-email: Jupyter Development Team <jupyter@googlegroups.com>
8
+ License-Expression: BSD-3-Clause
9
+ License-File: COPYING.md
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: BSD License
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Programming Language :: Python :: Implementation :: CPython
20
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
21
+ Requires-Dist: jupyverse-api>=0.13.0,<0.14.0
22
+ Requires-Dist: jupyter-ydoc>=3.3.5,<4.0.0
23
+ Requires-Dist: pycrdt>=0.12.0,<0.13.0
24
+ Requires-Python: >=3.10
25
+ Project-URL: Homepage, https://jupyter.org
26
+ Description-Content-Type: text/markdown
27
+
28
+ # fps-yrooms
29
+
30
+ An FPS plugin for YRooms.
@@ -0,0 +1,3 @@
1
+ # fps-yrooms
2
+
3
+ An FPS plugin for YRooms.
@@ -0,0 +1,45 @@
1
+ [build-system]
2
+ requires = ["uv_build"]
3
+ build-backend = "uv_build"
4
+
5
+ [project]
6
+ name = "fps_yrooms"
7
+ version = "0.1.0"
8
+ description = "An FPS plugin for YRooms"
9
+ keywords = [ "jupyter", "server", "fastapi", "plugins" ]
10
+ requires-python = ">=3.10"
11
+ classifiers = [
12
+ "Development Status :: 5 - Production/Stable",
13
+ "Intended Audience :: Developers",
14
+ "License :: OSI Approved :: BSD License",
15
+ "Programming Language :: Python",
16
+ "Programming Language :: Python :: 3.10",
17
+ "Programming Language :: Python :: 3.11",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Programming Language :: Python :: 3.14",
21
+ "Programming Language :: Python :: Implementation :: CPython",
22
+ "Programming Language :: Python :: Implementation :: PyPy",
23
+ ]
24
+ dependencies = [
25
+ "jupyverse-api >=0.13.0,<0.14.0",
26
+ "jupyter-ydoc >=3.3.5,<4.0.0",
27
+ "pycrdt >=0.12.0,<0.13.0",
28
+ ]
29
+ license = "BSD-3-Clause"
30
+ license-files = ["COPYING.md"]
31
+
32
+ [[project.authors]]
33
+ name = "Jupyter Development Team"
34
+ email = "jupyter@googlegroups.com"
35
+
36
+ [project.readme]
37
+ file = "README.md"
38
+ content-type = "text/markdown"
39
+
40
+ [project.urls]
41
+ Homepage = "https://jupyter.org"
42
+
43
+ [project.entry-points]
44
+ "fps.modules" = {yrooms = "fps_yrooms.main:YRoomsModule"}
45
+ "jupyverse.modules" = {yrooms = "fps_yrooms.main:YRoomsModule"}
@@ -0,0 +1,6 @@
1
+ import importlib.metadata
2
+
3
+ try:
4
+ __version__ = importlib.metadata.version("fps_yrooms")
5
+ except importlib.metadata.PackageNotFoundError:
6
+ __version__ = "unknown"
@@ -0,0 +1,19 @@
1
+ from pydantic import Field
2
+
3
+ from jupyverse_api import Config
4
+
5
+
6
+ class YRoomsConfig(Config):
7
+ document_cleanup_delay: float = Field(
8
+ description=(
9
+ "The time to wait (in seconds) after the last client has leaved "
10
+ "before closing the room."
11
+ ),
12
+ default=60,
13
+ )
14
+ document_save_delay: float = Field(
15
+ description=(
16
+ "The time to wait (in seconds) after the last change before saving a document to disk."
17
+ ),
18
+ default=1,
19
+ )
@@ -0,0 +1,30 @@
1
+ from functools import partial
2
+ from typing import Any
3
+
4
+ from fps import Module
5
+ from jupyverse_api.contents import Contents
6
+ from jupyverse_api.file_id import FileId
7
+ from jupyverse_api.yrooms import YRoomFactory, YRooms
8
+ from jupyverse_api.ystore import YStoreFactory
9
+
10
+ from .config import YRoomsConfig
11
+ from .yrooms import _YRoom, _YRooms
12
+
13
+
14
+ class YRoomsModule(Module):
15
+ def __init__(self, name: str, **kwargs: Any):
16
+ super().__init__(name)
17
+ self.config = YRoomsConfig(**kwargs)
18
+
19
+ async def prepare(self) -> None:
20
+ contents = await self.get(Contents) # type: ignore[type-abstract]
21
+ file_id = await self.get(FileId) # type: ignore[type-abstract]
22
+ ystore_factory = await self.get(YStoreFactory)
23
+ yroom_factory = YRoomFactory(
24
+ partial(_YRoom, contents, file_id, ystore_factory, self.config) # type: ignore[arg-type]
25
+ )
26
+ async with _YRooms(yroom_factory) as yrooms:
27
+ self.put(yrooms, YRooms)
28
+ self.done()
29
+ await self.started.wait()
30
+ await self.freed(yrooms)
File without changes
@@ -0,0 +1,229 @@
1
+ from datetime import datetime
2
+
3
+ import structlog
4
+ from anyio import (
5
+ CancelScope,
6
+ sleep,
7
+ )
8
+ from anyio.abc import TaskStatus
9
+ from jupyter_ydoc import ydocs as YDOCS
10
+ from jupyverse_api.contents import Contents
11
+ from jupyverse_api.file_id import FileId
12
+ from jupyverse_api.yrooms import AsyncChannel, YRoom, YRooms
13
+ from jupyverse_api.ystore import YDocNotFound, YStoreFactory
14
+ from pycrdt import (
15
+ Doc,
16
+ YMessageType,
17
+ YSyncMessageType,
18
+ handle_sync_message,
19
+ )
20
+
21
+ from .config import YRoomsConfig
22
+
23
+ logger = structlog.get_logger()
24
+ YFILE = YDOCS["file"]
25
+
26
+
27
+ class _YRoom(YRoom):
28
+ def __init__(
29
+ self,
30
+ contents: Contents,
31
+ file_id: FileId,
32
+ ystore_factory: YStoreFactory,
33
+ config: YRoomsConfig,
34
+ id: str,
35
+ sync: bool = True,
36
+ doc: Doc | None = None,
37
+ permissions: dict[str, list[str]] | None = None,
38
+ ) -> None:
39
+ super().__init__(id, sync, doc=doc)
40
+ self._contents = contents
41
+ self._file_id = file_id
42
+ self._ystore_factory = ystore_factory
43
+ self._config = config
44
+ self._close_room_cancel_scope: CancelScope | None = None
45
+ self._write_to_file_cancel_scope: CancelScope | None = None
46
+ self._id_of_file: str | None = None
47
+ self._can_write = permissions is None or "write" in permissions.get("yjs", [])
48
+
49
+ async def serve(self, client: AsyncChannel) -> None:
50
+ # cancel the closing of the room if it was scheduled:
51
+ if self._close_room_cancel_scope is not None:
52
+ self._close_room_cancel_scope.cancel()
53
+ self._close_room_cancel_scope = None
54
+ kwargs = {"id": self.id}
55
+ if self._id_of_file is not None:
56
+ file_path = await self._get_file_path(self._id_of_file)
57
+ assert file_path is not None
58
+ kwargs["file_path"] = file_path
59
+ logger.info("Client connected", **kwargs)
60
+ await super().serve(client)
61
+ logger.info("Client disconnected", **kwargs)
62
+
63
+ async def run(self, *, task_status: TaskStatus[None]) -> None:
64
+ kwargs = {"id": self.id}
65
+ if self.id.count(":") >= 2:
66
+ # it is a stored document (e.g. a notebook)
67
+ self._file_format, self._file_type, self._id_of_file = self.id.split(":", 2)
68
+ self._jupyter_ydoc = YDOCS.get(self._file_type, YFILE)(self.doc)
69
+ self._jupyter_ydoc.ystate["file_id"] = self._id_of_file
70
+ file_path = await self._get_file_path(self._id_of_file)
71
+ assert file_path is not None
72
+ kwargs["file_path"] = file_path
73
+ logger.info("Opening collaboration room", **kwargs)
74
+ model = await self._contents.read_content(file_path, True, self._file_format)
75
+ assert model.last_modified is not None
76
+ self._last_modified = to_datetime(model.last_modified)
77
+ updates_file_path = f".{self._file_type}:{self._id_of_file}.y"
78
+ async with self._ystore_factory(path=updates_file_path) as self._ystore:
79
+ # try to apply Y updates from the YStore for this document
80
+ try:
81
+ await self._ystore.apply_updates(self.doc)
82
+ read_from_source = False
83
+ except YDocNotFound:
84
+ # YDoc not found in the YStore, create the document from
85
+ # the source file (no change history)
86
+ read_from_source = True
87
+ logger.info("Document not found in YStore", file_path=file_path, id=self.id)
88
+ if not read_from_source:
89
+ # if YStore updates and source file are out-of-sync, resync updates
90
+ # with source
91
+ if self._jupyter_ydoc.source != model.content:
92
+ read_from_source = True
93
+ logger.info(
94
+ "Document in YStore differs from file content",
95
+ file_path=file_path,
96
+ id=self.id,
97
+ )
98
+ await self.task_group.start(self._write_to_ystore)
99
+ await self.task_group.start(self._write_to_file)
100
+ await self.task_group.start(self._watch_file)
101
+ if read_from_source:
102
+ self._jupyter_ydoc.source = model.content
103
+ await self._ystore.encode_state_as_update(self.doc)
104
+ logger.info("Document read from file", file_path=file_path, id=self.id)
105
+ else:
106
+ logger.info("Document read from YStore", file_path=file_path, id=self.id)
107
+
108
+ self._jupyter_ydoc.dirty = False
109
+ await super().run(task_status=task_status)
110
+ else:
111
+ logger.info("Opening collaboration room", **kwargs)
112
+ await super().run(task_status=task_status)
113
+
114
+ async def _write_to_ystore(self, *, task_status: TaskStatus[None]) -> None:
115
+ async with self.doc.events() as events:
116
+ task_status.started()
117
+ async for event in events:
118
+ await self._ystore.write(event.update)
119
+
120
+ async def _write_to_file(self, *, task_status: TaskStatus[None]) -> None:
121
+ async with self.doc.events() as events:
122
+ task_status.started()
123
+ async for event in events:
124
+ if self._write_to_file_cancel_scope is not None:
125
+ self._write_to_file_cancel_scope.cancel()
126
+ self._write_to_file_cancel_scope = None
127
+ self.task_group.start_soon(self._write_to_file_later)
128
+
129
+ async def _write_to_file_later(self) -> None:
130
+ with CancelScope() as self._write_to_file_cancel_scope:
131
+ await sleep(self._config.document_save_delay)
132
+ assert self._id_of_file is not None
133
+ file_path = await self._get_file_path(self._id_of_file)
134
+ assert file_path is not None
135
+ model = await self._contents.read_content(file_path, True, self._file_format)
136
+ if model.content != self._jupyter_ydoc.source:
137
+ # don't save if not needed
138
+ # this also prevents the dirty flag from bouncing between windows of
139
+ # the same document opened as different types (e.g. notebook/text editor)
140
+ content = {
141
+ "content": self._jupyter_ydoc.source,
142
+ "format": self._file_format,
143
+ "path": file_path,
144
+ "type": self._file_type,
145
+ }
146
+ with CancelScope(shield=True):
147
+ logger.info("Saving document", file_path=file_path, id=self.id)
148
+ await self._contents.write_content(content)
149
+ model = await self._contents.read_content(file_path, False)
150
+ assert model.last_modified is not None
151
+ self._last_modified = to_datetime(model.last_modified)
152
+ self._jupyter_ydoc.dirty = False
153
+
154
+ async def _watch_file(self, *, task_status: TaskStatus[None]) -> None:
155
+ assert self._id_of_file is not None
156
+ file_path = await self._get_file_path(self._id_of_file)
157
+ assert file_path is not None
158
+ logger.info("Watching file", path=file_path)
159
+ watcher = self._file_id.watch(file_path)
160
+ task_status.started()
161
+ async for changes in watcher:
162
+ new_file_path = await self._get_file_path(self._id_of_file)
163
+ assert new_file_path is not None
164
+ if new_file_path is None:
165
+ continue
166
+ if new_file_path != file_path:
167
+ # file was renamed
168
+ self._file_id.unwatch(file_path, watcher)
169
+ file_path = new_file_path
170
+ # break
171
+ await self._read_file(new_file_path)
172
+
173
+ async def _read_file(self, file_path: str) -> None:
174
+ model = await self._contents.read_content(file_path, False)
175
+ assert model.last_modified is not None
176
+ # do nothing if the file was saved by us
177
+ if self._last_modified < to_datetime(model.last_modified):
178
+ # the file was not saved by us, update the shared document
179
+ model = await self._contents.read_content(file_path, True, self._file_format)
180
+ assert model.last_modified is not None
181
+ self._jupyter_ydoc.source = model.content
182
+ self._jupyter_ydoc.dirty = False
183
+ logger.info("Document read from file", file_path=file_path, id=self.id)
184
+ await self._ystore.encode_state_as_update(self._doc)
185
+ self._last_modified = to_datetime(model.last_modified)
186
+
187
+ async def _get_file_path(self, id_of_file: str) -> str | None:
188
+ file_path = await self._file_id.get_path(id_of_file)
189
+ if file_path is None:
190
+ return None
191
+ if file_path != self._jupyter_ydoc.path:
192
+ self._jupyter_ydoc.path = file_path
193
+ return file_path
194
+
195
+ async def handle_message(self, message: bytes, client: AsyncChannel) -> None:
196
+ match message[0]:
197
+ case YMessageType.SYNC:
198
+ _message = message[1:]
199
+ if self._can_write or _message[0] not in {
200
+ YSyncMessageType.SYNC_UPDATE,
201
+ YSyncMessageType.SYNC_STEP2,
202
+ }:
203
+ reply = handle_sync_message(_message, self._doc)
204
+ if reply is not None:
205
+ await client.send(reply)
206
+ case YMessageType.AWARENESS:
207
+ for client in self.clients:
208
+ await client.send(message)
209
+
210
+ async def close(self) -> None:
211
+ with CancelScope() as self._close_room_cancel_scope:
212
+ kwargs = {"id": self.id}
213
+ if self._id_of_file is not None:
214
+ file_path = await self._get_file_path(self._id_of_file)
215
+ assert file_path is not None
216
+ kwargs["file_path"] = file_path
217
+ await sleep(self._config.document_cleanup_delay)
218
+ await super().close()
219
+ logger.info("Closed collaboration room", **kwargs)
220
+
221
+
222
+ class _YRooms(YRooms):
223
+ async def serve(self, websocket: AsyncChannel, permissions: dict[str, list[str]]) -> None:
224
+ room = await self.get_room(websocket.id, permissions=permissions)
225
+ await room.serve(websocket)
226
+
227
+
228
+ def to_datetime(iso_date: str) -> datetime:
229
+ return datetime.fromisoformat(iso_date.rstrip("Z"))