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

Generate sync methods from async ones #1329

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
5 changes: 4 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

# NB: In this config we exclude the example and tests notebooks
# from the code hooks (black, flake8, etc) as we want to keep
# the text representation of the test notebooks unchanged
# the text representation of the test notebooks unchanged, plus
# the (sync) contentsmanager.py which is generated from the async one.
exclude: >
(?x)^(
demo/.*|
tests/data/notebooks/.*|
src/jupytext/sync_pairs.py|
src/jupytext/sync_contentsmanager.py|
)$
repos:

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Jupytext ChangeLog
----------

**Changed**
- Jupytext's default contents manager is now derived from the asynchronous AsyncLargeFileManager. Thanks to [Darshan Poudel](https://github.com/Darshan808) for making this finally happen ([#1328](https://github.com/mwouts/jupytext/pull/1328))!
- We have updated the JupyterLab extension dependencies ([#1300](https://github.com/mwouts/jupytext/pull/1300)). Thanks to [Mahendra Paipuri](https://github.com/mahendrapaipuri) for this PR!


Expand Down
38 changes: 16 additions & 22 deletions jupyterlab/jupyterlab_jupytext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import asyncio

from jupytext.reraise import reraise

try:
from jupytext.contentsmanager import build_jupytext_contents_manager_class
except ImportError as err:
build_jupytext_contents_manager = reraise(err)
from jupytext.async_contentsmanager import build_async_jupytext_contents_manager_class
from jupytext.sync_contentsmanager import build_sync_jupytext_contents_manager_class


def load_jupyter_server_extension(app): # pragma: no cover
Expand All @@ -22,26 +18,24 @@ def load_jupyter_server_extension(app): # pragma: no cover
# The server extension call is too late!
# The contents manager was set at NotebookApp.init_configurables

# If possible, we derive a Jupytext CM from the current CM
base_class = app.contents_manager_class
if asyncio.iscoroutinefunction(base_class.get):
app.log.warning(
f"[Jupytext Server Extension] Async contents managers like {base_class.__name__} "
"are not supported at the moment "
"(https://github.com/mwouts/jupytext/issues/1020). "
"We will derive a contents manager from LargeFileManager instead."
)
from jupyter_server.services.contents.largefilemanager import ( # noqa
LargeFileManager,
)

base_class = LargeFileManager

asynchronous = asyncio.iscoroutinefunction(base_class.get)
app.log.info(
"[Jupytext Server Extension] Deriving a JupytextContentsManager "
"from {}".format(base_class.__name__)
"[Jupytext Server Extension] Deriving "
+ ("an Async" if asynchronous else "a ")
+ "TextFileContentsManager from "
+ base_class.__name__
)
app.contents_manager_class = build_jupytext_contents_manager_class(base_class)

if asyncio.iscoroutinefunction(base_class.get):
app.contents_manager_class = build_async_jupytext_contents_manager_class(
base_class
)
else:
app.contents_manager_class = build_sync_jupytext_contents_manager_class(
base_class
)

try:
# And rerun selected init steps from https://github.com/jupyter/notebook/blob/
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ classifiers = [
]
dependencies = [
"nbformat",
"jupyter_server",
"mdit-py-plugins",
"markdown-it-py>=1.0",
"packaging",
Expand All @@ -50,6 +51,7 @@ Documentation = "https://jupytext.readthedocs.io"
# Test related dependencies
test = [
"pytest",
"pytest-asyncio",
"pytest-xdist",
"pytest-randomly"
]
Expand Down Expand Up @@ -205,6 +207,7 @@ markers = [
"requires_ir_kernel",
"skip_on_windows",
"pre_commit",
"asyncio"
]
filterwarnings = [
# Uncomment this "error" to turn all unfiltered warnings into errors
Expand Down
23 changes: 11 additions & 12 deletions src/jupytext/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
"""Read and write Jupyter notebooks as text files"""

from .async_contentsmanager import (
AsyncTextFileContentsManager,
build_async_jupytext_contents_manager_class,
)
from .formats import NOTEBOOK_EXTENSIONS, get_format_implementation, guess_format
from .jupytext import read, reads, write, writes
from .reraise import reraise
from .sync_contentsmanager import (
TextFileContentsManager,
build_sync_jupytext_contents_manager_class,
)
from .version import __version__

try:
from .contentsmanager import build_jupytext_contents_manager_class
except ImportError as err:
build_jupytext_contents_manager_class = reraise(err)

try:
from .contentsmanager import TextFileContentsManager
except ImportError as err:
TextFileContentsManager = reraise(err)

__all__ = [
"read",
"write",
Expand All @@ -24,6 +21,8 @@
"guess_format",
"get_format_implementation",
"TextFileContentsManager",
"build_jupytext_contents_manager_class",
"AsyncTextFileContentsManager",
"build_sync_jupytext_contents_manager_class",
"build_async_jupytext_contents_manager_class",
"__version__",
]
Loading
Loading