Skip to content

Commit

Permalink
Add BaseWatchfiles class
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Apr 15, 2022
1 parent 1bec904 commit 7bd9f99
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
13 changes: 10 additions & 3 deletions jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from datetime import datetime

import nbformat
import watchfiles
from anyio.to_thread import run_sync
from jupyter_core.paths import exists, is_file_hidden, is_hidden
from send2trash import send2trash
Expand All @@ -22,7 +23,7 @@

from .filecheckpoints import AsyncFileCheckpoints, FileCheckpoints
from .fileio import AsyncFileManagerMixin, FileManagerMixin
from .manager import AsyncContentsManager, ContentsManager
from .manager import AsyncContentsManager, BaseWatchfiles, ContentsManager

try:
from os.path import samefile
Expand All @@ -33,9 +34,15 @@
_script_exporter = None


class Watchfiles(BaseWatchfiles):
watch = watchfiles.watch
awatch = watchfiles.awatch
Change = watchfiles.Change


class FileContentsManager(FileManagerMixin, ContentsManager):

import watchfiles
watchfiles = Watchfiles

root_dir = Unicode(config=True)

Expand Down Expand Up @@ -549,7 +556,7 @@ def get_kernel_path(self, path, model=None):

class AsyncFileContentsManager(FileContentsManager, AsyncFileManagerMixin, AsyncContentsManager):

import watchfiles
watchfiles = Watchfiles

@default("checkpoints_class")
def _checkpoints_class_default(self):
Expand Down
29 changes: 13 additions & 16 deletions jupyter_server/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import re
import warnings
from enum import IntEnum
from fnmatch import fnmatch

from nbformat import ValidationError, sign
Expand Down Expand Up @@ -34,11 +35,19 @@
copy_pat = re.compile(r"\-Copy\d*\.")


class NoWatchfilesAPI:
pass
class BaseWatchfiles:
"""File system change notifyer API
Override these attributes in subclasses if the file system supports file change notifications.
"""

def watch(*paths, **kwargs):
raise NotImplementedError

async def awatch(*paths, **kwargs):
raise NotImplementedError

NOWATCHFILESAPI = NoWatchfilesAPI()
Change = IntEnum


class ContentsManager(LoggingConfigurable):
Expand Down Expand Up @@ -416,19 +425,7 @@ def rename_file(self, old_path, new_path):
# ContentsManager API part 2: methods that have useable default
# implementations, but can be overridden in subclasses.

@property
def watchfiles(self):
"""File system change notifyer
Override this method in subclasses if the file system supports change notifications.
Returns
-------
api : class
The supported API for file system change notifications. Loosely follows the API of the
watchfiles Python package (can be a subset of it).
"""
return NOWATCHFILESAPI
watchfiles = BaseWatchfiles

def delete(self, path):
"""Delete a file/directory and any associated checkpoints."""
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ install_requires =
terminado>=0.8.3
tornado>=6.1.0
traitlets>=5.1
watchfiles>=0.13
watchfiles==0.13
websocket-client

[options.extras_require]
Expand Down

0 comments on commit 7bd9f99

Please sign in to comment.