Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add YDrive #370

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Support remote directory listing
  • Loading branch information
davidbrochart committed Jan 12, 2024
commit b12fbff265db9d60872435f85529954b25188abc
15 changes: 11 additions & 4 deletions plugins/yjs/fps_yjs/ydocs/ydrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def _callback(self, events):
for event in events:
if isinstance(event, MapEvent):
current = self._ycontent
for path in event.path:
current = current[path]
for p in event.path:
current = current[p]
for key, val in event.keys.items():
action = val.get("action")
if action == "delete":
Expand All @@ -101,13 +101,17 @@ def _callback(self, events):
self._task_group.start_soon(self._try_create_directory, path)
else:
self._task_group.start_soon(self._try_create_file, path)
elif action == "update":
if key == "populate" and not val["oldValue"] and val["newValue"]:
path = "/".join(event.path[1::2])
self._task_group.start_soon(self.ls, path)

@property
def version(self) -> str:
return "1.0.0"

def _new_dir_content(self) -> Map:
return Map({"is_dir": True, "content": None})
return Map({"is_dir": True, "populate": False, "content": None})

def _new_file_content(self, content: Content | None = None) -> Map:
if content is None:
Expand Down Expand Up @@ -153,7 +157,10 @@ async def _get_directory_content(self, path: Path) -> Map:

async def _maybe_populate_dir(self, path: Path, content: Map):
if content["content"] is None:
content["content"] = await self._get_directory_content(path)
with content.doc.transaction():
content["content"] = await self._get_directory_content(path)
if not content["populate"]:
content["populate"] = True

async def _get(self, path: Path | str | None = None) -> Map:
path = Path() if path is None else Path(path)
Expand Down
19 changes: 19 additions & 0 deletions plugins/yjs/tests/test_ydocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ async def test_ydrive():
await ydrive.ls("doesnt_exist")
assert str(exc_info.value) == "404: Item not found"

# remote ydrive.ls()
assert not ydrive._ycontent["populate"]
assert ydrive._ycontent["content"] is None
ydrive._ycontent["populate"] = True
await assert_with_timeout(lambda: ydrive._ycontent["content"] is not None)
assert "file0" in ydrive._ycontent["content"]
assert "file1" in ydrive._ycontent["content"]
assert "dir0" in ydrive._ycontent["content"]
assert "dir1" in ydrive._ycontent["content"]

assert not ydrive._ycontent["content"]["dir0"]["populate"]
assert ydrive._ycontent["content"]["dir0"]["content"] is None
ydrive._ycontent["content"]["dir0"]["populate"] = True
await assert_with_timeout(
lambda: ydrive._ycontent["content"]["dir0"]["content"] is not None
)
assert len(ydrive._ycontent["content"]["dir0"]["content"]) == 1
assert "file2" in ydrive._ycontent["content"]["dir0"]["content"]

root_dir = await ydrive.ls()
assert "file0" in root_dir
assert "file1" in root_dir
Expand Down
Loading