Skip to content

Commit

Permalink
Add YDrive
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jan 8, 2024
1 parent 5e7a982 commit 8faf8ef
Show file tree
Hide file tree
Showing 10 changed files with 464 additions and 98 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ jobs:
python3 -m pip install --upgrade pip
python3 -m pip install hatch
- name: Create jupyterlab-auth dev environment
run: hatch env create dev.jupyterlab-auth
- name: Create jupyterlab-auth and jupyterlab-noauth dev environments
run: |
hatch env create dev.jupyterlab-auth
hatch env create dev.jupyterlab-noauth
- name: Run tests
run: hatch run dev.jupyterlab-auth:test
run: |
hatch run dev.jupyterlab-noauth:pytest plugins/yjs/tests -v --color=yes
hatch run dev.jupyterlab-auth:test
24 changes: 20 additions & 4 deletions jupyverse_api/jupyverse_api/contents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import asyncio
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union

from fastapi import APIRouter, Depends, Request, Response

from jupyverse_api import Router

from ..app import App
from ..auth import Auth, User
from .models import Checkpoint, Content, SaveContent
from .models import Checkpoint, Content, CreateContent, SaveContent


class FileIdManager(ABC):
stop_watching_files: asyncio.Event
stopped_watching_files: asyncio.Event
Change: Any

@abstractmethod
async def get_path(self, file_id: str) -> str:
Expand Down Expand Up @@ -55,7 +56,8 @@ async def create_content(
request: Request,
user: User = Depends(auth.current_user(permissions={"contents": ["write"]})),
) -> Content:
return await self.create_content(path, request, user)
create_content = CreateContent(**(await request.json()))
return await self.create_content(path, create_content, user)

@router.get("/api/contents")
async def get_root_content(
Expand Down Expand Up @@ -134,11 +136,25 @@ async def create_checkpoint(
async def create_content(
self,
path: Optional[str],
request: Request,
create_content: CreateContent,
user: User,
) -> Content:
...

@abstractmethod
async def create_file(
self,
path: str,
) -> None:
...

@abstractmethod
async def create_directory(
self,
path: str,
) -> None:
...

@abstractmethod
async def get_root_content(
self,
Expand Down
24 changes: 18 additions & 6 deletions plugins/contents/fps_contents/fileid.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class FileIdManager(metaclass=Singleton):
initialized: asyncio.Event
watchers: Dict[str, List[Watcher]]
lock: asyncio.Lock
Change = Change

def __init__(self, db_path: str = ".fileid.db"):
def __init__(self, db_path: str = ".fileid.db", root_dir: str = "."):
self.db_path = db_path
self.root_dir = Path(root_dir)
self.initialized = asyncio.Event()
self.watchers = {}
self.watch_files_task = asyncio.create_task(self.watch_files())
Expand Down Expand Up @@ -78,6 +80,7 @@ async def index(self, path: str) -> Optional[str]:
return idx

async def watch_files(self):
self.root_dir = await self.root_dir.resolve()
async with self.lock:
async with aiosqlite.connect(self.db_path) as db:
await db.execute("DROP TABLE IF EXISTS fileids")
Expand All @@ -90,7 +93,7 @@ async def watch_files(self):
# index files
async with self.lock:
async with aiosqlite.connect(self.db_path) as db:
async for path in Path().rglob("*"):
async for path in self.root_dir.rglob("*"):
idx = uuid4().hex
mtime = (await path.stat()).st_mtime
await db.execute(
Expand All @@ -99,14 +102,16 @@ async def watch_files(self):
await db.commit()
self.initialized.set()

async for changes in awatch(".", stop_event=self.stop_watching_files):
async for changes in awatch(self.root_dir, stop_event=self.stop_watching_files):
async with self.lock:
async with aiosqlite.connect(self.db_path) as db:
deleted_paths = set()
added_paths = set()
for change, changed_path in changes:
# get relative path
changed_path = Path(changed_path).relative_to(await Path().absolute())
changed_path = Path(changed_path).relative_to(
await self.root_dir.absolute()
)
changed_path_str = str(changed_path)

if change == Change.deleted:
Expand Down Expand Up @@ -156,9 +161,16 @@ async def watch_files(self):
for change in changes:
changed_path = change[1]
# get relative path
relative_changed_path = str(Path(changed_path).relative_to(await Path().absolute()))
relative_changed_path = Path(changed_path).relative_to(
await self.root_dir.absolute()
)
relative_change = (change[0], relative_changed_path)
for watcher in self.watchers.get(relative_changed_path, []):
all_watchers = []
for path, watchers in self.watchers.items():
p = Path(path)
if p == relative_changed_path or p in relative_changed_path.parents:
all_watchers += watchers
for watcher in all_watchers:
watcher.notify(relative_change)

self.stopped_watching_files.set()
Expand Down
Loading

0 comments on commit 8faf8ef

Please sign in to comment.