<commit_before># -*- coding: utf-8 -*-
# Copyright (c) IPython-Contrib Team.

"""Jupyter server extension to enable, disable and configure nbextensions."""

from __future__ import unicode_literals

import json
import os.path
import posixpath
import re
import subprocess
import sys

import yaml
from notebook.base.handlers import IPythonHandler
from notebook.notebookapp import NotebookApp
from notebook.utils import url_path_join as ujoin
from notebook.utils import path2url
from tornado import web
from traitlets.config.configurable import MultipleInstanceError
from yaml.scanner import ScannerError

# attempt to use LibYaml if available
try:
    from yaml import CSafeLoader as SafeLoader
except ImportError:
    from yaml import SafeLoader

absolute_url_re = re.compile(r'^(f|ht)tps?://')


def get_nbextensions_path():
    """
    Return the nbextensions search path.

    gets the search path for
      - the current NotebookApp instance
    or, if we can't instantiate one
      - the default config used, found by spawning a subprocess
    """
    # Attempt to get the path for the currently-running config, or the default
    # if one isn't running.
    # If there's already a non-NotebookApp traitlets app running, e.g. when
    # we're inside an IPython kernel there's a KernelApp instantiated, then
    # attempting to get a NotebookApp instance will raise a
    # MultipleInstanceError.
    try:
        return NotebookApp.instance().nbextensions_path
    except MultipleInstanceError:
        # So, we spawn a new python process to get paths for the default config
        cmd = "from {0} import {1}; [print(p) for p in {1}()]".format(
            get_nbextensions_path.__module__, get_nbextensions_path.__name__)

        return subprocess.check_output([
            sys.executable, '-c', cmd
        ]).decode(sys.stdout.encoding).split('\n')


def get_configurable_nbextensions(
        nbextension_dirs=None, exclude_dirs=('mathjax',), log=None):
    """Build a list of configurable nbextensions based on YAML descriptor files.

    descriptor files must:
      - be located under one of nbextension_dirs
      - have the extension '.yaml'
      - containing (at minimum) the following keys:<commit_msg>get nbextensions_path from web_app.settings if possible ensure the themysto nbextensions directory definitely appears in the path<commit_after># -*- coding: utf-8 -*-
# Copyright (c) IPython-Contrib Team.

"""Jupyter server extension to enable, disable and configure nbextensions."""

from __future__ import unicode_literals

import json
import os.path
import posixpath
import re
import subprocess
import sys

import yaml
from notebook.base.handlers import IPythonHandler
from notebook.notebookapp import NotebookApp
from notebook.utils import url_path_join as ujoin
from notebook.utils import path2url
from tornado import web
from traitlets.config.configurable import MultipleInstanceError
from yaml.scanner import ScannerError

from ..nbextensions_injector import nbext_dir

# attempt to use LibYaml if available
try:
    from yaml import CSafeLoader as SafeLoader
except ImportError:
    from yaml import SafeLoader

absolute_url_re = re.compile(r'^(f|ht)tps?://')


def get_nbextensions_path():
    """
    Return the nbextensions search path.

    gets the search path for
      - the current NotebookApp instance
    or, if we can't instantiate one
      - the default config used, found by spawning a subprocess
    """
    # Attempt to get the path for the currently-running server, or the default
    # if one isn't running.
    # If there's already a non-NotebookApp traitlets app running, e.g. when
    # we're inside an IPython kernel there's a KernelApp instantiated, then
    # attempting to get a NotebookApp instance will raise a
    # MultipleInstanceError.
    try:
        nbapp = NotebookApp.instance()
    except MultipleInstanceError:
        # So, we spawn a new python process to get paths for the default config
        cmd = "from {0} import {1}; [print(p) for p in {1}()]".format(
            get_nbextensions_path.__module__, get_nbextensions_path.__name__)

        nbext_path = subprocess.check_output([
            sys.executable, '-c', cmd
        ]).decode(sys.stdout.encoding).split('\n')
    else:
        # try to get web_app.settings if possible (e.g. when a NotebookApp
        # instance is running correctly).
        try:
            webapp = nbapp.web_app
        except AttributeError:
            nbext_path = nbapp.nbextensions_path
        else:
            nbext_path = webapp.settings['nbextensions_path']
    finally:
        # We may need to insert our nbext_dir into the path if we're created
        # the NotebookApp instance, as in this case, the server extension
        # themysto.nbextensions_injector won't have loaded to modify the
        # nbapp.web_app.settings
        our_nbext_dir = nbext_dir()
        if our_nbext_dir not in nbext_path:
            nbext_path.append(our_nbext_dir)
        return nbext_path


def get_configurable_nbextensions(
        nbextension_dirs=None, exclude_dirs=('mathjax',), log=None):
    """Build a list of configurable nbextensions based on YAML descriptor files.

    descriptor files must:
      - be located under one of nbextension_dirs
      - have the extension '.yaml'
      - containing (at minimum) the following keys:
{'fraction_non_alphanumeric': 0.06977179199401422, 'fraction_numerical': 0.0018705574261129816, 'mean_word_length': 3.3436230706742487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2362441', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1528, 'n_words': 553}