Skip to content

Commit 4f4fa76

Browse files
authored
Merge pull request #240 from manics/jupyter-server
Depend on jupyter-server only, compatibility with jupyter server >= 2, notebook < 7
2 parents 8f9dd93 + bb2e7bd commit 4f4fa76

File tree

6 files changed

+81
-9
lines changed

6 files changed

+81
-9
lines changed

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
jupyter-packaging>=0.10
22
nbclassic
3+
notebook>=5.5,<7
34
packaging
45
pytest
56
pytest-cov

nbgitpuller/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from .version import __version__ # noqa
2-
from .handlers import SyncHandler, UIHandler, LegacyInteractRedirectHandler, LegacyGitSyncRedirectHandler
32
from .pull import GitPuller # noqa
4-
from notebook.utils import url_path_join
3+
from jupyter_server.utils import url_path_join
54
from tornado.web import StaticFileHandler
65
import os
76

@@ -33,6 +32,19 @@ def _load_jupyter_server_extension(app):
3332
- notebook: https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Distributing%20Jupyter%20Extensions%20as%20Python%20Packages.html#Example---Server-extension
3433
- jupyter_server: https://jupyter-server.readthedocs.io/en/latest/developers/extensions.html
3534
"""
35+
# identify base handler by app class
36+
# must do this before importing from .handlers
37+
from ._compat import get_base_handler
38+
39+
get_base_handler(app)
40+
41+
from .handlers import (
42+
SyncHandler,
43+
UIHandler,
44+
LegacyInteractRedirectHandler,
45+
LegacyGitSyncRedirectHandler,
46+
)
47+
3648
web_app = app.web_app
3749
base_url = url_path_join(web_app.settings['base_url'], 'git-pull')
3850
handlers = [

nbgitpuller/_compat.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Import base Handler classes from Jupyter Server or Notebook
2+
3+
Must be called before importing .handlers to ensure the correct base classes
4+
"""
5+
import warnings
6+
7+
_JupyterHandler = None
8+
9+
10+
def get_base_handler(app=None):
11+
"""Get the base JupyterHandler class to use
12+
13+
Inferred from app class (either jupyter_server or notebook app)
14+
"""
15+
global _JupyterHandler
16+
if _JupyterHandler is not None:
17+
return _JupyterHandler
18+
if app is None:
19+
warnings.warn(
20+
"Guessing base JupyterHandler class. Specify an app to ensure the right JupyterHandler is used.",
21+
stacklevel=2,
22+
)
23+
from jupyter_server.base.handlers import JupyterHandler
24+
return JupyterHandler
25+
26+
top_modules = {cls.__module__.split(".", 1)[0] for cls in app.__class__.mro()}
27+
if "jupyter_server" in top_modules:
28+
from jupyter_server.base.handlers import JupyterHandler
29+
30+
_JupyterHandler = JupyterHandler
31+
return _JupyterHandler
32+
if "notebook" in top_modules:
33+
from notebook.base.handlers import IPythonHandler
34+
35+
_JupyterHandler = IPythonHandler
36+
return _JupyterHandler
37+
38+
warnings.warn(f"Failed to detect base JupyterHandler class for {app}.", stacklevel=2)
39+
from jupyter_server.base.handlers import JupyterHandler
40+
return JupyterHandler

nbgitpuller/handlers.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import traceback
33
import urllib.parse
44

5-
from notebook.base.handlers import IPythonHandler
65
import threading
76
import json
87
import os
@@ -11,14 +10,17 @@
1110

1211
from .pull import GitPuller
1312
from .version import __version__
13+
from ._compat import get_base_handler
14+
15+
JupyterHandler = get_base_handler()
1416

1517

1618
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(
1719
os.path.join(os.path.dirname(__file__), 'templates')
1820
),
1921
)
2022

21-
class SyncHandler(IPythonHandler):
23+
class SyncHandler(JupyterHandler):
2224
def __init__(self, *args, **kwargs):
2325
super().__init__(*args, **kwargs)
2426

@@ -132,7 +134,7 @@ def pull():
132134
self.git_lock.release()
133135

134136

135-
class UIHandler(IPythonHandler):
137+
class UIHandler(JupyterHandler):
136138
@web.authenticated
137139
async def get(self):
138140
app_env = os.getenv('NBGITPULLER_APP', default='notebook')
@@ -169,7 +171,7 @@ async def get(self):
169171
await self.flush()
170172

171173

172-
class LegacyGitSyncRedirectHandler(IPythonHandler):
174+
class LegacyGitSyncRedirectHandler(JupyterHandler):
173175
"""
174176
The /git-pull endpoint was previously exposed /git-sync.
175177
@@ -185,7 +187,7 @@ async def get(self):
185187
self.redirect(new_url)
186188

187189

188-
class LegacyInteractRedirectHandler(IPythonHandler):
190+
class LegacyInteractRedirectHandler(JupyterHandler):
189191
"""
190192
The /git-pull endpoint was previously exposed /interact.
191193

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
author='Peter Veerman, YuviPanda',
3333
author_email='[email protected]',
3434
cmdclass=cmdclass,
35-
description='Notebook Extension to do one-way synchronization of git repositories',
35+
description='Jupyter Extension to do one-way synchronization of git repositories',
3636
long_description=open('README.md').read(),
3737
long_description_content_type='text/markdown',
3838
packages=find_packages(),
3939
include_package_data=True,
4040
platforms='any',
41-
install_requires=['notebook>=5.5.0', 'jupyter_server>=1.10.1', 'tornado'],
41+
install_requires=['jupyter_server>=1.10.1', 'tornado'],
4242
data_files=[
4343
('etc/jupyter/jupyter_server_config.d', ['nbgitpuller/etc/jupyter_server_config.d/nbgitpuller.json']),
4444
('etc/jupyter/jupyter_notebook_config.d', ['nbgitpuller/etc/jupyter_notebook_config.d/nbgitpuller.json'])

tests/test_api.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ def test_clone_default(jupyterdir, jupyter_server):
118118
assert os.path.isdir(os.path.join(target_path, '.git'))
119119

120120

121+
def test_clone_auth(jupyterdir, jupyter_server):
122+
"""
123+
Tests use of 'repo' and 'branch' parameters.
124+
"""
125+
with Remote() as remote, Pusher(remote) as pusher:
126+
pusher.push_file('README.md', 'Testing some content')
127+
print(f'path: {remote.path}')
128+
params = {
129+
'repo': remote.path,
130+
'branch': 'master',
131+
'token': 'wrong',
132+
}
133+
r = request_api(params)
134+
# no token, redirect to login
135+
assert r.code == 302
136+
137+
121138
def test_clone_targetpath(jupyterdir, jupyter_server):
122139
"""
123140
Tests use of 'targetpath' parameter.

0 commit comments

Comments
 (0)