Skip to content

Commit

Permalink
Add ability to have custom actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Udoff committed Jul 21, 2020
1 parent 2d95391 commit da9fe3f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
30 changes: 29 additions & 1 deletion jupyterlab_git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@
"""
# need this in order to show version in `jupyter serverextension list`
from ._version import __version__
from traitlets import Type
from traitlets.config import Configurable

from jupyterlab_git.handlers import setup_handlers
from jupyterlab_git.git import Git

class GitCustomActions(object):
"""
Class to handle custom actions. This is a base class that is expected to be replaced via the jupyter_notebook_config
"""

async def post_init(self, cwd):
"""
:param cwd: Directory where git init was run
"""
pass

class JupyterLabGitConfig(Configurable):
"""
Config options for jupyterlab_git
Modeled after: https://github.com/jupyter/jupyter_server/blob/9dd2a9a114c045cfd8fd8748400c6a697041f7fa/jupyter_server/serverapp.py#L1040
"""

# See: https://traitlets.readthedocs.io/en/stable/trait_types.html#classes-and-instances
custom_git_actions_class = Type(
default_value=GitCustomActions,
config=True,
help='Delegate custom git actions'
)

def _jupyter_server_extension_paths():
"""Declare the Jupyter server extension paths.
Expand All @@ -16,6 +42,8 @@ def _jupyter_server_extension_paths():
def load_jupyter_server_extension(nbapp):
"""Load the Jupyter server extension.
"""
git = Git(nbapp.web_app.settings['contents_manager'])

user_custom_actions = JupyterLabGitConfig(config=nbapp.config).custom_git_actions_class()
git = Git(nbapp.web_app.settings['contents_manager'], user_custom_actions)
nbapp.web_app.settings["git"] = git
setup_handlers(nbapp.web_app)
11 changes: 7 additions & 4 deletions jupyterlab_git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ class Git:
A single parent class containing all of the individual git methods in it.
"""

def __init__(self, contents_manager):
def __init__(self, contents_manager, user_custom_actions):
self.contents_manager = contents_manager
self.root_dir = os.path.expanduser(contents_manager.root_dir)
self.user_custom_actions = user_custom_actions

async def config(self, top_repo_path, **kwargs):
"""Get or set Git options.
Expand Down Expand Up @@ -299,7 +300,7 @@ async def status(self, current_path):
for line in filter(lambda l: len(l) > 0, strip_and_split(text_output)):
diff, name = line.rsplit("\t", maxsplit=1)
are_binary[name] = diff.startswith("-\t-")

result = []
line_iterable = (line for line in strip_and_split(my_output) if line)
for line in line_iterable:
Expand Down Expand Up @@ -870,9 +871,11 @@ async def init(self, current_path):
Execute git init command & return the result.
"""
cmd = ["git", "init"]
cwd = os.path.join(self.root_dir, current_path)
code, _, error = await execute(
cmd, cwd=os.path.join(self.root_dir, current_path)
cmd, cwd=cwd
)
await self.user_custom_actions.post_init(cwd)

if code != 0:
return {"code": code, "command": " ".join(cmd), "message": error}
Expand Down Expand Up @@ -1061,7 +1064,7 @@ async def _is_binary(self, filename, ref, top_repo_path):
Returns:
bool: Is file binary?
Raises:
HTTPError: if git command failed
"""
Expand Down

0 comments on commit da9fe3f

Please sign in to comment.