Skip to content

Commit

Permalink
Use anyio instead of aiopath
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Nov 22, 2022
1 parent d661f73 commit 558a6d5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [ '3.10' ]
python-version: [ '3.7', '3.8', '3.9', '3.10' ]

steps:
- name: Checkout
Expand Down
21 changes: 11 additions & 10 deletions plugins/contents/fps_contents/fileid.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import asyncio
import logging
from pathlib import Path
from typing import Callable, Dict, List, Optional
from uuid import uuid4

import aiosqlite
from aiopath import AsyncPath # type: ignore
from anyio import Path
from fps.logging import get_configured_logger # type: ignore
from watchfiles import Change, awatch


watchfiles_logger = get_configured_logger("watchfiles.main")
watchfiles_logger.setLevel(logging.CRITICAL)
logger = get_configured_logger("contents")
Expand All @@ -34,7 +32,8 @@ def notify(self, change):


class Singleton(type):
_instances = {}
_instances: Dict = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
Expand Down Expand Up @@ -72,7 +71,7 @@ async def get_path(self, idx: str) -> Optional[str]:
async def index(self, path: str) -> Optional[str]:
await self.initialized.wait()
async with aiosqlite.connect(self.db_path) as db:
apath = AsyncPath(path)
apath = Path(path)
if not await apath.exists():
return None

Expand All @@ -93,7 +92,7 @@ async def watch_files(self):

# index files
async with aiosqlite.connect(self.db_path) as db:
async for path in AsyncPath().rglob("*"):
async for path in Path().rglob("*"):
idx = uuid4().hex
mtime = (await path.stat()).st_mtime
await db.execute("INSERT INTO fileids VALUES (?, ?, ?)", (idx, str(path), mtime))
Expand All @@ -105,7 +104,7 @@ async def watch_files(self):
added_paths = []
for change, changed_path in changes:
# get relative path
changed_path = AsyncPath(changed_path).relative_to(Path().absolute())
changed_path = Path(changed_path).relative_to(await Path().absolute())
changed_path_str = str(changed_path)

if change == Change.deleted:
Expand Down Expand Up @@ -134,7 +133,9 @@ async def watch_files(self):
logger.debug("File %s is not indexed, ignoring", changed_path_str)
continue
mtime = (await changed_path.stat()).st_mtime
await db.execute("UPDATE fileids SET mtime = ? WHERE path = ?", (mtime, changed_path_str))
await db.execute(
"UPDATE fileids SET mtime = ? WHERE path = ?", (mtime, changed_path_str)
)

for path in deleted_paths + added_paths:
await db.execute("DELETE FROM fileids WHERE path = ?", (path,))
Expand All @@ -143,7 +144,7 @@ async def watch_files(self):
for change in changes:
changed_path = change[1]
# get relative path
changed_path = str(Path(changed_path).relative_to(Path().absolute()))
changed_path = str(Path(changed_path).relative_to(await Path().absolute()))
for watcher in self.watchers.get(changed_path, []):
watcher.notify(change)

Expand All @@ -163,7 +164,7 @@ async def get_mtime(path, db) -> Optional[float]:
# deleted file is not in database, shouldn't happen
return None
try:
mtime = (await AsyncPath(path).stat()).st_mtime
mtime = (await Path(path).stat()).st_mtime
except FileNotFoundError:
return None
return mtime
Expand Down
2 changes: 1 addition & 1 deletion plugins/contents/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "fps_contents"
description = "An FPS plugin for the contents API"
keywords = [ "jupyter", "server", "fastapi", "pluggy", "plugins",]
requires-python = ">=3.7"
dependencies = [ "fps >=0.0.8", "fps-auth-base", "anyio", "watchfiles >=0.16.1,<1", "aiosqlite >=0.17.0,<1", "aiopath >=0.6.11,<1"]
dependencies = [ "fps >=0.0.8", "fps-auth-base", "anyio", "watchfiles >=0.16.1,<1", "aiosqlite >=0.17.0,<1", "anyio>=3.6.2,<4"]
dynamic = [ "version",]
[[project.authors]]
name = "Jupyter Development Team"
Expand Down
2 changes: 1 addition & 1 deletion plugins/lab/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "fps_lab"
description = "An FPS plugin for the JupyterLab/RetroLab API"
keywords = [ "jupyter", "server", "fastapi", "pluggy", "plugins",]
requires-python = ">=3.7"
dependencies = [ "fps >=0.0.8", "fps-auth-base", "fps-frontend", "aiofiles", "babel", "json5",]
dependencies = [ "fps >=0.0.8", "fps-auth-base", "fps-frontend", "babel", "json5",]
dynamic = [ "version",]
[[project.authors]]
name = "Jupyter Development Team"
Expand Down
10 changes: 8 additions & 2 deletions plugins/yjs/fps_yjs/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,14 @@ async def on_message(self, message: bytes) -> bool:
async def watch_file(self):
if has_watch:
file_format, file_type, file_path = await self.get_file_info()
async for changes in get_watch()(file_path):
await self.maybe_load_document()
while True:
async for changes in get_watch()(file_path):
file_format, file_type, new_file_path = await self.get_file_info()
if new_file_path != file_path:
# file was renamed
file_path = new_file_path
break
await self.maybe_load_document()
else:
# contents plugin doesn't provide watcher, fall back to polling
poll_interval = 1 # FIXME: pass in config
Expand Down

0 comments on commit 558a6d5

Please sign in to comment.