<commit_before># -*- coding: utf-8 -*-

import argparse
import errno
import os
from os.path import join, isdir
try:
    from inspect import signature
except ImportError:
    from funcsigs import signature
from jupyter_core.paths import jupyter_config_dir
from notebook.nbextensions import install_nbextension
from notebook.services.config import ConfigManager


class StaticPathNotFound(Exception):
    pass


def mkdir_p(path):
    """ 'mkdir -p' in Python """
    try:
        os.makedirs(path)
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and isdir(path):
            pass
        else:
            raise


def enable(directory, **kwargs):
    """
    Enable the extension on every notebook
    """
    if "prefix" in kwargs:
        path = join(kwargs["prefix"], "etc", "jupyter")
    else:
        path = jupyter_config_dir()
    cm = ConfigManager(config_dir=path)
    mkdir_p(cm.config_dir)

    for key, filename in {'notebook': 'main.js', 'tree': 'tree.js', 'edit': 'edit.js'}.iteritems():
        if filename in os.listdir(directory):
            cm.update(
                key, {
                    "load_extensions": {
                        "{}/{}".format(kwargs['name'], key): True
                    }
                }
            )
    print(' '.join(['Enabling', kwargs['name'], '\033[92m', '✔' + '\033[0m']))
    enable_server_extension(**kwargs)


def enable_server_extension(**kwargs):
    if "prefix" in kwargs:
        path = join(kwargs["prefix"], "etc", "jupyter")
    else:
        path = jupyter_config_dir()
    fn = os.path.join(path, 'jupyter_notebook_config.py')
    with open(fn, 'r+') as fh:
        lines = fh.read()
        if kwargs['name'] not in lines:
            fh.seek(0, 2)
            fh.write('\n')
            fh.write(
                "c.NotebookApp.server_extensions.append('{}.nbextension')".format(kwargs['name']))


def _install_args(**kwargs):
    kwargs['verbose'] = 0
    if kwargs['verbose']:
        kwargs['verbose'] = 2
    kwargs["destination"] = kwargs['name']
    del kwargs['enable']
    del kwargs['name']
    del kwargs['version']
    return kwargs


def install(directory, **kwargs):
    """Install the nbextension assets and optionally enables the
       nbextension and server extension for every run.
    Parameters
    ----------
    directory: path
    **kwargs: keyword arguments
        Other keyword arguments passed to the install_nbextension command
    """<commit_msg>Enable server extensions<commit_after># -*- coding: utf-8 -*-

import argparse
import errno
import os
from os.path import join, isdir
try:
    from inspect import signature
except ImportError:
    from funcsigs import signature
from jupyter_core.paths import jupyter_config_dir
from notebook.nbextensions import install_nbextension
from notebook.services.config import ConfigManager


class StaticPathNotFound(Exception):
    pass


def mkdir_p(path):
    """ 'mkdir -p' in Python """
    try:
        os.makedirs(path)
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and isdir(path):
            pass
        else:
            raise


def enable(directory, **kwargs):
    """
    Enable the extension on every notebook
    """
    if "prefix" in kwargs:
        path = join(kwargs["prefix"], "etc", "jupyter")
    else:
        path = jupyter_config_dir()
    cm = ConfigManager(config_dir=path)
    mkdir_p(cm.config_dir)

    for key, filename in {'notebook': 'main.js', 'tree': 'tree.js', 'edit': 'edit.js'}.iteritems():
        if filename in os.listdir(directory):
            cm.update(
                key, {
                    "load_extensions": {
                        "{}/{}".format(kwargs['name'], key): True
                    }
                }
            )
    print(' '.join(['Enabling', kwargs['name'], '\033[92m', '✔' + '\033[0m']))

    try:
        __import__(kwargs['name'])
        enable_server_extension(**kwargs)
    except ImportError:
        pass


def enable_server_extension(**kwargs):
    if "prefix" in kwargs:
        path = join(kwargs["prefix"], "etc", "jupyter")
    else:
        path = jupyter_config_dir()
    cm = ConfigManager(config_dir=path)
    cfg = cm.get("jupyter_notebook_config")
    server_extensions = (
        cfg.setdefault("NotebookApp", {})
        .setdefault("server_extensions", [])
    )
    if "{}.nbextension".format(kwargs['name']) not in server_extensions:
        cfg["NotebookApp"]["server_extensions"] += ["{}.nbextension".format(kwargs['name'])]
    cm.update("jupyter_notebook_config", cfg)


def _install_args(**kwargs):
    kwargs['verbose'] = 0
    if kwargs['verbose']:
        kwargs['verbose'] = 2
    kwargs["destination"] = kwargs['name']
    del kwargs['enable']
    del kwargs['name']
    del kwargs['version']
    return kwargs


def install(directory, **kwargs):
    """Install the nbextension assets and optionally enables the
       nbextension and server extension for every run.
    Parameters
    ----------
    directory: path
    **kwargs: keyword arguments
        Other keyword arguments passed to the install_nbextension command
    """
{'fraction_non_alphanumeric': 0.13352769679300291, 'fraction_numerical': 0.0058309037900874635, 'mean_word_length': 2.573611111111111, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2222205', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1535, 'n_words': 393}