From 1adf19e9731e98a0138e331d9ff7f8a9d6f8f3fd Mon Sep 17 00:00:00 2001 From: Marc Udoff Date: Tue, 21 Jul 2020 19:37:24 -0400 Subject: [PATCH] Add ability to have custom actions --- jupyterlab_git/__init__.py | 30 +++++++++++++++++++++++++++++- jupyterlab_git/git.py | 11 +++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/jupyterlab_git/__init__.py b/jupyterlab_git/__init__.py index 64a32ccab..facab3601 100644 --- a/jupyterlab_git/__init__.py +++ b/jupyterlab_git/__init__.py @@ -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. @@ -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) diff --git a/jupyterlab_git/git.py b/jupyterlab_git/git.py index af52ce548..13dce97aa 100644 --- a/jupyterlab_git/git.py +++ b/jupyterlab_git/git.py @@ -142,9 +142,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. @@ -301,7 +302,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: @@ -872,9 +873,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} @@ -1065,7 +1068,7 @@ async def _is_binary(self, filename, ref, top_repo_path): Returns: bool: Is file binary? - + Raises: HTTPError: if git command failed """