Skip to content

Commit 045d8e3

Browse files
committed
rework into newer extension style
1 parent f415428 commit 045d8e3

File tree

7 files changed

+43
-127
lines changed

7 files changed

+43
-127
lines changed

nbgitpuller/__init__.py

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,8 @@
1-
from .version import __version__ # noqa
2-
from .pull import GitPuller # noqa
3-
from jupyter_server.utils import url_path_join
4-
from tornado.web import StaticFileHandler
5-
import os
1+
from .application import NbGitPuller
62

73

84
def _jupyter_server_extension_points():
9-
"""
10-
This function is detected by `notebook` and `jupyter_server` because they
11-
are explicitly configured to inspect the nbgitpuller module for it. That
12-
explicit configuration is passed via setup.py's declared data_files.
13-
14-
Returns a list of dictionaries with metadata describing where to find the
15-
`_load_jupyter_server_extension` function.
16-
"""
175
return [{
186
'module': 'nbgitpuller',
7+
'app': NbGitPuller
198
}]
20-
21-
22-
def _load_jupyter_server_extension(app):
23-
"""
24-
This function is a hook for `notebook` and `jupyter_server` that we use to
25-
register additional endpoints to be handled by nbgitpuller.
26-
27-
Note that as this function is used as a hook for both notebook and
28-
jupyter_server, the argument passed may be a NotebookApp or a ServerApp.
29-
30-
Related documentation:
31-
- notebook: https://jupyter-notebook.readthedocs.io/en/stable/extending/handlers.htmland
32-
- notebook: https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Distributing%20Jupyter%20Extensions%20as%20Python%20Packages.html#Example---Server-extension
33-
- jupyter_server: https://jupyter-server.readthedocs.io/en/latest/developers/extensions.html
34-
"""
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-
48-
web_app = app.web_app
49-
base_url = url_path_join(web_app.settings['base_url'], 'git-pull')
50-
handlers = [
51-
(url_path_join(base_url, 'api'), SyncHandler),
52-
(base_url, UIHandler),
53-
(url_path_join(web_app.settings['base_url'], 'git-sync'), LegacyGitSyncRedirectHandler),
54-
(url_path_join(web_app.settings['base_url'], 'interact'), LegacyInteractRedirectHandler),
55-
(
56-
url_path_join(base_url, 'static', '(.*)'),
57-
StaticFileHandler,
58-
{'path': os.path.join(os.path.dirname(__file__), 'static')}
59-
)
60-
]
61-
# FIXME: See note on how to stop relying on settings to pass information:
62-
# https://github.com/jupyterhub/nbgitpuller/pull/242#pullrequestreview-854968180
63-
#
64-
web_app.settings['nbapp'] = app
65-
web_app.add_handlers('.*', handlers)
66-
67-
68-
# For compatibility with both notebook and jupyter_server, we define
69-
# _jupyter_server_extension_paths alongside _jupyter_server_extension_points.
70-
#
71-
# "..._paths" is used by notebook and still supported by jupyter_server as of
72-
# jupyter_server 1.13.3, but was renamed to "..._points" in jupyter_server
73-
# 1.0.0.
74-
#
75-
_jupyter_server_extension_paths = _jupyter_server_extension_points
76-
77-
# For compatibility with both notebook and jupyter_server, we define both
78-
# load_jupyter_server_extension alongside _load_jupyter_server_extension.
79-
#
80-
# "load..." is used by notebook and "_load..." is used by jupyter_server.
81-
#
82-
load_jupyter_server_extension = _load_jupyter_server_extension

nbgitpuller/_compat.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

nbgitpuller/application.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from .version import __version__ # noqa
2+
from .pull import GitPuller # noqa
3+
from jupyter_server.extension.application import ExtensionApp
4+
import os
5+
6+
7+
class NbGitPuller(ExtensionApp):
8+
name = 'git-pull'
9+
load_other_extensions = True
10+
11+
static_paths = [
12+
os.path.join(os.path.dirname(__file__), 'static')
13+
]
14+
15+
def initialize_handlers(self):
16+
from .handlers import (
17+
SyncHandler,
18+
UIHandler,
19+
LegacyInteractRedirectHandler,
20+
LegacyGitSyncRedirectHandler,
21+
)
22+
23+
# Extend the self.handlers trait
24+
self.handlers.extend([
25+
(rf'/{self.name}/api', SyncHandler),
26+
(rf'/{self.name}', UIHandler),
27+
(rf'/{self.name}/git-sync', LegacyGitSyncRedirectHandler),
28+
(rf'/{self.name}/interact', LegacyInteractRedirectHandler),
29+
])

nbgitpuller/handlers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@
77
import os
88
from queue import Queue, Empty
99
import jinja2
10+
from jupyter_server.base.handlers import JupyterHandler
11+
from jupyter_server.extension.handler import ExtensionHandlerMixin
1012

1113
from .pull import GitPuller
1214
from .version import __version__
13-
from ._compat import get_base_handler
14-
15-
JupyterHandler = get_base_handler()
1615

1716

1817
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(
1918
os.path.join(os.path.dirname(__file__), 'templates')
2019
),
2120
)
2221

23-
class SyncHandler(JupyterHandler):
22+
class SyncHandler(ExtensionHandlerMixin, JupyterHandler):
2423
def __init__(self, *args, **kwargs):
2524
super().__init__(*args, **kwargs)
2625

26+
self.log.info(f'Config {self.config}')
27+
2728
# We use this lock to make sure that only one sync operation
2829
# can be happening at a time. Git doesn't like concurrent use!
2930
if 'git_lock' not in self.settings:
@@ -84,7 +85,7 @@ async def get(self):
8485
self.set_header('content-type', 'text/event-stream')
8586
self.set_header('cache-control', 'no-cache')
8687

87-
gp = GitPuller(repo, repo_dir, branch=branch, depth=depth, parent=self.settings['nbapp'])
88+
gp = GitPuller(repo, repo_dir, branch, depth=depth, **self.config)
8889

8990
q = Queue()
9091

nbgitpuller/pull.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,18 @@ def _depth_default(self):
6969
where the GitPuller class hadn't been loaded already."""
7070
return int(os.environ.get('NBGITPULLER_DEPTH', 1))
7171

72-
def __init__(self, git_url, repo_dir, **kwargs):
72+
def __init__(self, git_url, repo_dir, branch, **kwargs):
7373
assert git_url
7474

7575
self.git_url = git_url
76-
self.branch_name = kwargs.pop("branch")
76+
self.repo_dir = repo_dir
77+
self.branch_name = branch
7778

7879
if self.branch_name is None:
7980
self.branch_name = self.resolve_default_branch()
8081
elif not self.branch_exists(self.branch_name):
8182
raise ValueError(f"Branch: {self.branch_name} -- not found in repo: {self.git_url}")
8283

83-
self.repo_dir = repo_dir
8484
newargs = {k: v for k, v in kwargs.items() if v is not None}
8585
super(GitPuller, self).__init__(**newargs)
8686

@@ -361,7 +361,7 @@ def main():
361361
for line in GitPuller(
362362
args.git_url,
363363
args.repo_dir,
364-
branch=args.branch_name if args.branch_name else None
364+
args.branch_name if args.branch_name else None
365365
).pull():
366366
print(line)
367367

nbgitpuller/templates/status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
{% block script %}
3737
{{super()}}
38-
<script src="{{ base_url }}git-pull/static/dist/bundle.js"></script>
38+
<script src="{{ base_url }}/static/git-pull/dist/bundle.js"></script>
3939
{% endblock %}
4040

4141
{% block stylesheet %}

tests/repohelpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from uuid import uuid4
99

1010
from packaging.version import Version as V
11-
from nbgitpuller import GitPuller
11+
from nbgitpuller.pull import GitPuller
1212

1313

1414
class Repository:

0 commit comments

Comments
 (0)