Skip to content

Commit 76b1b58

Browse files
committed
Add file system change notifyer
1 parent 02e5f3a commit 76b1b58

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

jupyter_server/services/contents/fileio.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from tornado.web import HTTPError
1616
from traitlets import Bool
1717
from traitlets.config import Configurable
18+
from watchfiles import * # noqa
1819

1920
from jupyter_server.utils import to_api_path, to_os_path
2021

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ install_requires =
5353
terminado>=0.8.3
5454
tornado>=6.1.0
5555
traitlets>=5.1
56+
watchfiles>=0.12
5657
websocket-client
5758

5859
[options.extras_require]

tests/services/contents/test_fileio.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import asyncio
12
import os
23
import stat
34
import sys
45

56
import pytest
67

7-
from jupyter_server.services.contents.fileio import atomic_writing
8+
from jupyter_server.services.contents.fileio import Change, atomic_writing, awatch
89

910
umask = 0
1011

@@ -121,3 +122,33 @@ def test_atomic_writing_newlines(tmp_path):
121122
with open(path, newline="") as f:
122123
read = f.read()
123124
assert read == text
125+
126+
127+
async def test_watch_directory(tmp_path):
128+
stop_event = asyncio.Event()
129+
130+
async def stop_soon():
131+
await asyncio.sleep(0.4)
132+
stop_event.set()
133+
134+
async def change_dir():
135+
await asyncio.sleep(0.1)
136+
(tmp_path / "file0").write_text("test0")
137+
await asyncio.sleep(0.1)
138+
(tmp_path / "file0").write_text("test1")
139+
await asyncio.sleep(0.1)
140+
(tmp_path / "file0").unlink()
141+
142+
tasks = [asyncio.create_task(stop_soon()), asyncio.create_task(change_dir())]
143+
144+
changes = []
145+
async for change in awatch(tmp_path, stop_event=stop_event, step=1):
146+
changes.append(change)
147+
148+
assert changes == [
149+
{(Change.added, str(tmp_path / "file0"))},
150+
{(Change.modified, str(tmp_path / "file0"))},
151+
{(Change.deleted, str(tmp_path / "file0"))},
152+
]
153+
154+
await asyncio.gather(*tasks)

0 commit comments

Comments
 (0)