Example #1
0
def get_labextension_manifest_data_by_name(name):
    """Get the manifest data for a given lab extension folder
    """
    for exts in jupyter_path('labextensions'):
        full_dest = os.path.join(exts, name)
        if os.path.exists(full_dest):
            return get_labextension_manifest_data_by_folder(full_dest)
Example #2
0
def add_handlers(web_app, labextensions):
    """Add the appropriate handlers to the web app.
    """
    base_url = web_app.settings['base_url']
    prefix = ujoin(base_url, PREFIX)
    extension_prefix = ujoin(base_url, EXTENSION_PREFIX)
    page_config_data = web_app.settings.get('page_config_data', {})
    handlers = [
        (prefix + r'/?', LabHandler, {
            'labextensions': labextensions,
            'extension_prefix': extension_prefix,
            'page_config_data': page_config_data
        }),
        (prefix + r"/(.*)", FileFindHandler, {
            'path': BUILT_FILES
        }),
        (
            extension_prefix + r"/(.*)",
            FileFindHandler,
            {
                'path': jupyter_path('labextensions'),
                'no_cache_paths':
                ['/'],  # don't cache anything in labextensions
            })
    ]
    web_app.add_handlers(".*$", handlers)
def environ(
    monkeypatch,
    tmp_path,
    data_dir,
    config_dir,
):
    system_data_dir = tmp_path / 'system_data'
    system_config_dir = tmp_path / 'system_config'
    system_path = [str(system_data_dir)]
    system_config_path = [str(system_config_dir)]

    # Set global environments variable
    monkeypatch.setenv('JUPYTER_CONFIG_DIR', str(config_dir))
    monkeypatch.setenv('JUPYTER_DATA_DIR', str(data_dir))

    # Set paths for each extension.
    for mod in (paths, ):
        monkeypatch.setattr(mod, 'SYSTEM_JUPYTER_PATH', system_path)
        monkeypatch.setattr(mod, 'ENV_JUPYTER_PATH', [])
    for mod in (paths, extensions_base):
        monkeypatch.setattr(mod, 'SYSTEM_CONFIG_PATH', system_config_path)
        monkeypatch.setattr(mod, 'ENV_CONFIG_PATH', [])

    assert paths.jupyter_config_path() == [str(config_dir)
                                           ] + system_config_path
    assert extensions_base._get_config_dir(
        user=False) == str(system_config_dir)
    assert paths.jupyter_path() == [str(data_dir)] + system_path
Example #4
0
def test_jupyter_path():
    system_path = ['system', 'path']
    with no_config_env, patch.object(paths, 'SYSTEM_JUPYTER_PATH',
                                     system_path):
        path = jupyter_path()
    assert path[0] == jupyter_data_dir()
    assert path[-2:] == system_path
def find_nbmolviz_extension(extname):
    import jupyter_core.paths as jupypaths
    for extpath in jupypaths.jupyter_path('nbextensions'):
        mypath = os.path.join(extpath, extname)
        if os.path.lexists(mypath):
            return extpath
    else:
        return None
def find_nbmolviz_extension(extname):
    import jupyter_core.paths as jupypaths
    for extpath in jupypaths.jupyter_path('nbextensions'):
        mypath = os.path.join(extpath, extname)
        if os.path.lexists(mypath):
            return extpath
    else:
        return None
Example #7
0
 def get_prefix_root_dirs(self):
     # We look at the usual jupyter locations, and for development purposes also
     # relative to the package directory (first entry, meaning with highest precedence)
     root_dirs = []
     if DEV_MODE:
         root_dirs.append(os.path.abspath(os.path.join(ROOT, '..', '..', 'share', 'jupyter')))
     root_dirs.extend(jupyter_path())
     return root_dirs
Example #8
0
def test_jupyter_path_env():
    path_env = os.pathsep.join([
        pjoin('foo', 'bar'),
        pjoin('bar', 'baz', ''),  # trailing /
    ])

    with patch.dict('os.environ', {'JUPYTER_PATH': path_env}):
        path = jupyter_path()
    assert path[:2] == [pjoin('foo', 'bar'), pjoin('bar', 'baz')]
Example #9
0
def test_jupyter_path_env():
    path_env = os.pathsep.join([
        pjoin('foo', 'bar'),
        pjoin('bar', 'baz', ''), # trailing /
    ])
    
    with patch.dict('os.environ', {'JUPYTER_PATH': path_env}):
        path = jupyter_path()
    assert path[:2] == [pjoin('foo', 'bar'), pjoin('bar', 'baz')]
def nbextension_ordered_paths():
    import jupyter_core.paths as jupypaths
    jupyter_searchpath = jupypaths.jupyter_path()

    paths = [('user', jupypaths.jupyter_data_dir()),
             ('environment', jupypaths.ENV_JUPYTER_PATH[0]),
             ('system', jupypaths.SYSTEM_JUPYTER_PATH[0])]

    paths.sort(key=lambda x: jupyter_searchpath.index(x[1]))
    return collections.OrderedDict(paths)
Example #11
0
def test_paths():
    output = get_jupyter_output('--paths')
    for d in (jupyter_config_dir(), jupyter_data_dir(), jupyter_runtime_dir()):
        assert d in output
    for key in ('config', 'data', 'runtime'):
        assert ('%s:' % key) in output

    for path in (jupyter_config_path(), jupyter_path()):
        for d in path:
            assert d in output
Example #12
0
def get_extension_path(*parts):
    '''
    Searches all known jupyter extension paths for the referenced directory.
    Returns the first hit or None if not found.
    '''
    ext_path = pjoin(*parts)
    for root_path in jupyter_path():
        full_path = pjoin(root_path, 'nbextensions', ext_path)
        if os.path.exists(full_path):
            return full_path
def get_extension_path(*parts):
    '''
    Searches all known jupyter extension paths for the referenced directory.
    Returns the first hit or None if not found.
    '''
    ext_path = pjoin(*parts)
    for root_path in jupyter_path():
        full_path = pjoin(root_path, 'nbextensions', ext_path)
        if os.path.exists(full_path):
            return full_path
def nbextension_ordered_paths():
    import jupyter_core.paths as jupypaths
    jupyter_searchpath = jupypaths.jupyter_path()

    paths = [('user', jupypaths.jupyter_data_dir()),
             ('environment', jupypaths.ENV_JUPYTER_PATH[0]),
             ('system', jupypaths.SYSTEM_JUPYTER_PATH[0])]

    paths.sort(key=lambda x: jupyter_searchpath.index(x[1]))
    return collections.OrderedDict(paths)
Example #15
0
def test_paths():
    output = get_jupyter_output("--paths")
    for d in (jupyter_config_dir(), jupyter_data_dir(), jupyter_runtime_dir()):
        assert d in output
    for key in ("config", "data", "runtime"):
        assert ("%s:" % key) in output

    for path in (jupyter_config_path(), jupyter_path()):
        for d in path:
            assert d in output
Example #16
0
def test_paths():
    output = get_jupyter_output('--paths')
    for d in (jupyter_config_dir(), jupyter_data_dir(), jupyter_runtime_dir()):
        assert d in output
    for key in ('config', 'data', 'runtime'):
        assert ('%s:' % key) in output
    
    for path in (jupyter_config_path(), jupyter_path()):
        for d in path:
            assert d in output
Example #17
0
def _default_root_dirs():
    # We look at the usual jupyter locations, and for development purposes also
    # relative to the package directory (first entry, meaning with highest precedence)
    root_dirs = []
    if DEV_MODE:
        root_dirs.append(os.path.abspath(os.path.join(ROOT, '..', 'share', 'jupyter')))
    if nbconvert.exporters.templateexporter.DEV_MODE:
        root_dirs.append(os.path.abspath(os.path.join(nbconvert.exporters.templateexporter.ROOT, '..', '..', 'share', 'jupyter')))
    root_dirs.extend(jupyter_path())

    return root_dirs
Example #18
0
 def nbextensions_path(self):
     """The path to look for Javascript notebook extensions"""
     path = self.extra_nbextensions_path + jupyter_path('nbextensions')
     # FIXME: remove IPython nbextensions path after a migration period
     try:
         from IPython.paths import get_ipython_dir
     except ImportError:
         pass
     else:
         path.append(os.path.join(get_ipython_dir(), 'nbextensions'))
     return path
Example #19
0
 def nbextensions_path(self):
     """The path to look for Javascript notebook extensions"""
     path = self.extra_nbextensions_path + jupyter_path('nbextensions')
     # FIXME: remove IPython nbextensions path after a migration period
     try:
         from IPython.paths import get_ipython_dir
     except ImportError:
         pass
     else:
         path.append(os.path.join(get_ipython_dir(), 'nbextensions'))
     return path
Example #20
0
    def namespace_exists(self):
        is_valid_namespace = False

        all_metadata_dirs = jupyter_path(
            os.path.join('metadata', self.namespace))
        for d in all_metadata_dirs:
            if os.path.isdir(d):
                is_valid_namespace = True
                break

        return is_valid_namespace
Example #21
0
 def post(self, action):
     func = ACTIONS.get(action, None)
     if func:
         args = self.get_arguments('args')
         if args:
             jpath_old = os.environ.get('JUPYTER_PATH')
             func(*args)
             # If JUPYTER_PATH has been modified by func
             # the kernel directory list is updated.
             if jpath_old != os.environ.get('JUPYTER_PATH'):
                 self.kernel_spec_manager.kernel_dirs = jupyter_path('kernels')
             self.finish(json.dumps('SUCCESS'))
def test_jupyter_path_user_site():
    with no_config_env, patch.object(site, 'ENABLE_USER_SITE', True):
        path = jupyter_path()

    # deduplicated expected values
    values = list(dict.fromkeys([
        jupyter_data_dir(),
        os.path.join(site.getuserbase(), 'share', 'jupyter'),
        paths.ENV_JUPYTER_PATH[0]
    ]))
    for p,v in zip(path, values):
        assert p == v
Example #23
0
 def start(self):
     if not self.extra_args:
         for nbext in jupyter_path('nbextensions'):
             if os.path.exists(nbext):
                 print("Notebook extensions in %s:" % nbext)
                 for ext in os.listdir(nbext):
                     print(u"    %s" % ext)
     else:
         try:
             self.install_extensions()
         except ArgumentConflict as e:
             print(str(e), file=sys.stderr)
             self.exit(1)
Example #24
0
 def _add_template_path(self, proposal):
     """
     We want to inherit from HTML template, and have template under
     `./templates/` so append it to the search path. (see next section)
     """
     our_path = os.path.join(os.path.dirname(__file__), "templates")
     base_paths = jupyter_path("nbconvert", "templates", "base")
     if our_path not in proposal['value']:
         proposal['value'].append(our_path)
     for base_path in base_paths:
         if base_path not in proposal['value']:
             proposal['value'].append(base_path)
     return proposal['value']
Example #25
0
 def start(self):
     if not self.extra_args:
         for nbext in jupyter_path('nbextensions'):
             if os.path.exists(nbext):
                 print("Notebook extensions in %s:" % nbext)
                 for ext in os.listdir(nbext):
                     print(u"    %s" % ext)
     else:
         try:
             self.install_extensions()
         except ArgumentConflict as e:
             print(str(e), file=sys.stderr)
             self.exit(1)
Example #26
0
def find_lab_theme(theme_name):
    """
    Find a JupyterLab theme location by name.

    Parameters
    ----------
    theme_name : str
        The name of the labextension theme you want to find.

    Raises
    ------
    ValueError
        If the theme was not found, or if it was not specific enough.

    Returns
    -------
    theme_name: str
        Full theme name (with scope, if any)
    labextension_path : Path
        The path to the found labextension on the system.
    """
    paths = jupyter_path("labextensions")

    matching_themes = []
    theme_path = None
    for path in paths:
        for (dirpath, dirnames, filenames) in os.walk(path):
            # If it's a federated labextension that contains themes
            if "package.json" in filenames and "themes" in dirnames:
                # TODO Find the theme name in the JS code instead?
                # TODO Find if it's a light or dark theme?
                with open(Path(dirpath) / "package.json",
                          encoding="utf-8") as fobj:
                    labext_name = json.loads(fobj.read())["name"]

                if labext_name == theme_name or theme_name in labext_name.split(
                        "/"):
                    matching_themes.append(labext_name)

                    full_theme_name = labext_name
                    theme_path = Path(dirpath) / "themes" / labext_name

    if len(matching_themes) == 0:
        raise ValueError(f'Could not find lab theme "{theme_name}"')

    if len(matching_themes) > 1:
        raise ValueError(
            f'Found multiple themes matching "{theme_name}": {matching_themes}. '
            "Please be more specific about which theme you want to use.")

    return full_theme_name, theme_path
Example #27
0
def setup():
    """Copy templates to one of the nbconvert directories."""
    from jupyter_core.paths import jupyter_path

    template_path = Path(resource_filename('nbtabs', 'templates'))
    paths = jupyter_path()

    print("Possible paths:")
    print("\n".join("%s: %s" % (ii, path) for ii, path in enumerate(paths)))
    path_indx = input("Select path: ").strip()

    dst_path = Path(paths[int(path_indx)]) / 'nbconvert' / 'templates' / 'html'
    for src_fname in template_path.glob("*tpl"):
        shutil.copy2(src_fname, dst_path / src_fname.name)
def pdf_capture(static_path, capture_server_class=None):
    """ Starts a tornado server which serves all of the jupyter path locations
        as well as the working directory
    """
    settings = {
        "static_path": static_path
    }

    handlers = [
        (r"/(.*)", tornado.web.StaticFileHandler, {
            "path": settings['static_path']
        })
    ]

    # add the jupyter static paths
    for path in jupyter_path():
        handlers += [
            (r"/static/(.*)", tornado.web.StaticFileHandler, {
                "path": os.path.join(path, "static")
            })
        ]

    app = tornado.web.Application(handlers, **settings)

    if capture_server_class is None:
        server = CaptureServer(app)
    else:
        _module, _klass = capture_server_class.split(":")
        server = getattr(import_module(_module), _klass)(app)

    # can't pass this to the constructor for some reason...
    server.static_path = static_path

    # add the parsed, normalized notebook
    with open(os.path.join(static_path, "notebook.ipynb")) as fp:
        server.notebook = nbformat.read(fp, IPYNB_VERSION)

    ioloop = IOLoop()
    # server.capture will be called when the ioloop is bored for the first time
    ioloop.add_callback(server.capture)
    # connect to a port
    server.listen(PORT)

    try:
        # run forever
        ioloop.start()
    except KeyboardInterrupt:
        # this is probably not the best way to escape, but works for now
        print("Successfully created PDF")
Example #29
0
    def init_webapp(self):
        super().init_webapp()

        # Handle labextension assets
        web_app = self.web_app
        base_url = web_app.settings['base_url']
        handlers = []

        # Temporary addition for testing
        self.extra_labextensions_path += [os.path.join(HERE, 'labextensions')]

        labextensions_path = self.extra_labextensions_path + jupyter_path('labextensions')
        labextensions_url = ujoin(base_url, "example", r"labextensions/(.*)")
        handlers.append(
            (labextensions_url, FileFindHandler, {
                'path': labextensions_path,
                'no_cache_paths': ['/'], # don't cache anything in labextensions
            }))

        # Handle requests for the list of settings. Make slash optional.
        settings_path = ujoin(base_url, 'example', 'api', 'settings')
        settings_config = {
            'app_settings_dir': self.lab_config.app_settings_dir,
            'schemas_dir': self.lab_config.schemas_dir,
            'settings_dir': self.lab_config.user_settings_dir,
            'labextensions_path': labextensions_path
        }

        handlers.append((ujoin(settings_path, '?'), SettingsHandler, settings_config))

        # Handle requests for an individual set of settings.
        setting_path = ujoin(
            settings_path, '(?P<schema_name>.+)')
        handlers.append((setting_path, SettingsHandler, settings_config))

        # Handle requests for themes
        themes_path = ujoin(base_url, 'example', 'api', 'themes', '(.*)')
        handlers.append((
            themes_path,
            ThemesHandler,
            {
                'themes_url': themes_path,
                'path': self.lab_config.themes_dir,
                'labextensions_path': labextensions_path,
                'no_cache_paths': ['/']
            }
        ))

        web_app.add_handlers('.*$', handlers)
Example #30
0
 def _kernel_dirs_default(self):
     dirs = jupyter_path("kernels")
     # At some point, we should stop adding .ipython/kernels to the path,
     # but the cost to keeping it is very small.
     try:
         from IPython.paths import get_ipython_dir  # type: ignore
     except ImportError:
         try:
             from IPython.utils.path import get_ipython_dir  # type: ignore
         except ImportError:
             # no IPython, no ipython dir
             get_ipython_dir = None
     if get_ipython_dir is not None:
         dirs.append(os.path.join(get_ipython_dir(), "kernels"))
     return dirs
Example #31
0
 def _kernel_dirs_default(self):
     dirs = jupyter_path("kernels")
     # At some point, we should stop adding .ipython/kernels to the path,
     # but the cost to keeping it is very small.
     try:
         from IPython.paths import get_ipython_dir
     except ImportError:
         try:
             from IPython.utils.path import get_ipython_dir
         except ImportError:
             # no IPython, no ipython dir
             get_ipython_dir = None
     if get_ipython_dir is not None:
         dirs.append(os.path.join(get_ipython_dir(), "kernels"))
     return dirs
def discover_dictionaries(server_app: JupyterApp):
    data_paths = jupyter_path('dictionaries')
    system = platform.system()
    if system in OS_SPECIFIC_PATHS:
        data_paths.extend(OS_SPECIFIC_PATHS[system])
    # TODO: maybe use server_app.data_dir?

    server_app.log.info(
        f"Looking for hunspell dictionaries for spellchecker in {data_paths}")
    dictionaries = []
    for path in data_paths:
        dictionaries.extend(_scan_for_dictionaries(path, server_app.log))

    server_app.log.info(
        f"Located hunspell dictionaries for spellchecker: {dictionaries}")
    return dictionaries
Example #33
0
 def add_lab_handlers(self, webapp):
     """Add the lab-specific handlers to the tornado app."""
     base_url = webapp.settings['base_url']
     webapp.add_handlers(".*$",
         [(ujoin(base_url, h[0]),) + h[1:] for h in default_handlers])
     labextension_handler = (
         r"%s/(.*)" % EXTENSION_PREFIX, FileFindHandler, {
             'path': jupyter_path('labextensions'),
             'no_cache_paths': ['/'],  # don't cache anything in labbextensions
         }
     )
     webapp.add_handlers(".*$", [labextension_handler])
     base_dir = os.path.realpath(os.path.join(HERE, '..'))
     dev_mode = os.path.exists(os.path.join(base_dir, '.git'))
     if dev_mode:
         self.log.info(DEV_NOTE_NPM)
Example #34
0
def initialize_new_notebook_environment():
    if _in_virtual_environment():
        _logger.error("This action is blocked because a specific python environment is active.")
        # Stop this program runtime and return the exit status code.
        sys.exit(1)

    try:
        from jupyter_core.paths import jupyter_path
    except ImportError:
        jupyter_path = _get_data_path

    # Find and remove all python kernels from the working notebook server.
    for path in jupyter_path("kernels"):
        if os.path.exists(path) and os.path.isdir(path):
            _remove_dir(path)

    # Add the main python kernel to the working notebook server on the current machine.
    _create_new_kernel("python{0}".format(sys.version_info[0]))
Example #35
0
def find_labextension(name):
    """Find a labextension path

    Looks across all of the labextension directories.

    Returns the first path where the extension is found,
    or None if not found.

    Parameters
    ----------
    name : str
        The name of the extension.
    """
    for exts in jupyter_path('labextensions'):
        full_dest = os.path.join(exts, name)
        if os.path.exists(full_dest):
            return full_dest
    return None
Example #36
0
def _find_uninstall_nbextension(filename, logger=None):
    """Remove nbextension files from the first location they are found.

    Returns True if files were removed, False otherwise.
    """
    filename = cast_unicode_py2(filename)
    for nbext in jupyter_path('nbextensions'):
        path = pjoin(nbext, filename)
        if os.path.lexists(path):
            if logger:
                logger.info("Removing: %s" % path)
            if os.path.isdir(path) and not os.path.islink(path):
                shutil.rmtree(path)
            else:
                os.remove(path)
            return True

    return False
Example #37
0
 def add_lab_handlers(self, webapp):
     """Add the lab-specific handlers to the tornado app."""
     base_url = webapp.settings['base_url']
     webapp.add_handlers(".*$", [(ujoin(base_url, h[0]), ) + h[1:]
                                 for h in default_handlers])
     labextension_handler = (
         r"%s/(.*)" % EXTENSION_PREFIX,
         FileFindHandler,
         {
             'path': jupyter_path('labextensions'),
             'no_cache_paths':
             ['/'],  # don't cache anything in labbextensions
         })
     webapp.add_handlers(".*$", [labextension_handler])
     base_dir = os.path.realpath(os.path.join(HERE, '..'))
     dev_mode = os.path.exists(os.path.join(base_dir, '.git'))
     if dev_mode:
         self.log.info(DEV_NOTE_NPM)
def _find_uninstall_nbextension(filename, logger=None):
    """Remove nbextension files from the first location they are found.

    Returns True if files were removed, False otherwise.
    """
    filename = cast_unicode_py2(filename)
    for nbext in jupyter_path('nbextensions'):
        path = pjoin(nbext, filename)
        if os.path.lexists(path):
            if logger:
                logger.info("Removing: %s" % path)
            if os.path.isdir(path) and not os.path.islink(path):
                shutil.rmtree(path)
            else:
                os.remove(path)
            return True

    return False
Example #39
0
def get_spec(argv=None):
  global logger
  from jupyter_core.paths import jupyter_path

  spec_parser = argparse.ArgumentParser()
  spec_parser.add_argument('kernel_name', help='Name of jupyter registered kernel to start (folder name)')
  spec_args = spec_parser.parse_args(argv)

  kernel_name = spec_args.kernel_name

  if os.path.isabs(kernel_name):
    kernel_spec = kernel_name
  else:
    kernel_spec = None
    for d in ['.'] + jupyter_path('kernels'):
      logger.debug('Searching in directory %s', d)
      if os.path.isfile(os.path.join(d, kernel_name, 'kernel.json')):
        kernel_spec = os.path.join(d, kernel_name, 'kernel.json')
        break

    assert kernel_spec is not None, \
        'Kernel specification file %s not found!' % kernel_name

  logger.info('Loading kernel specification file %s', kernel_spec)

  with open(kernel_spec, mode='r') as spec_fs:
    spec = json.load(spec_fs)

  # Ensure specification is the correct version
  spec = _check_spec(kernel_spec, spec)

  args = spec['argv']

  assert args[1:3] == ['-m', 'remote_kernel'], \
      'Kernel spec %s is not a remote_kernel specification' % kernel_spec

  # Remove the jupyter supplied connection_file specification
  if '-f' in args and args[args.index('-f') + 1] == '{connection_file}':
    idx = args.index('-f')
    del args[idx: idx+2]
  elif '-f={connection_file}' in args:
    del args[args.index('-f={connection_file}')]

  return args[3:]
    def setUp(self):
        self.tempdirs = []
        self._mock_extensions = []

        self.test_dir = self.tempdir()
        self.data_dir = os.path.join(self.test_dir, 'data')
        self.config_dir = os.path.join(self.test_dir, 'config')
        self.system_data_dir = os.path.join(self.test_dir, 'system_data')
        self.system_config_dir = os.path.join(self.test_dir, 'system_config')
        self.system_path = [self.system_data_dir]
        self.system_config_path = [self.system_config_dir]

        self.patches = []
        p = patch.dict(
            'os.environ', {
                'JUPYTER_CONFIG_DIR': self.config_dir,
                'JUPYTER_DATA_DIR': self.data_dir,
            })
        self.patches.append(p)
        for mod in (paths, nbextensions):
            p = patch.object(mod, 'SYSTEM_JUPYTER_PATH', self.system_path)
            self.patches.append(p)
            p = patch.object(mod, 'ENV_JUPYTER_PATH', [])
            self.patches.append(p)
        for mod in (paths, extensions):
            p = patch.object(mod, 'SYSTEM_CONFIG_PATH',
                             self.system_config_path)
            self.patches.append(p)
            p = patch.object(mod, 'ENV_CONFIG_PATH', [])
            self.patches.append(p)
        # avoid adding the user site to the config paths with jupyter-core >= 4.9
        # https://github.com/jupyter/jupyter_core/pull/242
        p = patch.object(site, 'ENABLE_USER_SITE', False)
        self.patches.append(p)
        for p in self.patches:
            p.start()
            self.addCleanup(p.stop)
        # verify our patches
        self.assertEqual(paths.jupyter_config_path(),
                         [self.config_dir] + self.system_config_path)
        self.assertEqual(extensions._get_config_dir(user=False),
                         self.system_config_dir)
        self.assertEqual(paths.jupyter_path(),
                         [self.data_dir] + self.system_path)
Example #41
0
    def _load_metadata_resources(self,
                                 name=None,
                                 validate_metadata=True,
                                 include_invalid=False):
        """Loads metadata files with .json suffix and return requested items.
           if 'name' is provided, the single file is loaded and returned, else
           all files ending in '.json' are loaded and returned in a list.
        """
        resources = []
        if self.namespace_exists():
            all_metadata_dirs = jupyter_path(
                os.path.join('metadata', self.namespace))
            for metadata_dir in all_metadata_dirs:
                if os.path.isdir(metadata_dir):
                    for f in os.listdir(metadata_dir):
                        path = os.path.join(metadata_dir, f)
                        if path.endswith(".json"):
                            if name:
                                if os.path.splitext(
                                        os.path.basename(path))[0] == name:
                                    return self._load_from_resource(
                                        path,
                                        validate_metadata=validate_metadata)
                            else:
                                metadata = None
                                try:
                                    metadata = self._load_from_resource(
                                        path,
                                        validate_metadata=validate_metadata,
                                        include_invalid=include_invalid)
                                except Exception:
                                    pass  # Ignore ValidationError and others when loading all resources
                                if metadata is not None:
                                    resources.append(metadata)
        else:  # namespace doesn't exist, treat as KeyError
            raise KeyError("Metadata namespace '{}' was not found!".format(
                self.namespace))

        if name:  # If we're looking for a single metadata and we're here, then its not found
            raise KeyError(
                "Metadata '{}' in namespace '{}' was not found!".format(
                    name, self.namespace))

        return resources
Example #42
0
def pdf_capture(static_path, capture_server_class=None):
    """ Starts a tornado server which serves all of the jupyter path locations
        as well as the working directory
    """
    settings = {"static_path": static_path}

    handlers = [(r"/(.*)", tornado.web.StaticFileHandler, {
        "path": settings['static_path']
    })]

    # add the jupyter static paths
    for path in jupyter_path():
        handlers += [(r"/static/(.*)", tornado.web.StaticFileHandler, {
            "path": os.path.join(path, "static")
        })]

    app = tornado.web.Application(handlers, **settings)

    if capture_server_class is None:
        server = CaptureServer(app)
    else:
        _module, _klass = capture_server_class.split(":")
        server = getattr(import_module(_module), _klass)(app)

    # can't pass this to the constructor for some reason...
    server.static_path = static_path

    # add the parsed, normalized notebook
    with open(os.path.join(static_path, "notebook.ipynb")) as fp:
        server.notebook = nbformat.read(fp, IPYNB_VERSION)

    ioloop = IOLoop()
    # server.capture will be called when the ioloop is bored for the first time
    ioloop.add_callback(server.capture)
    # connect to a port
    server.listen(PORT)

    try:
        # run forever
        ioloop.start()
    except KeyboardInterrupt:
        # this is probably not the best way to escape, but works for now
        print("Successfully created PDF")
Example #43
0
def add_handlers(web_app, labextensions):
    """Add the appropriate handlers to the web app.
    """
    base_url = web_app.settings['base_url']
    prefix = ujoin(base_url, PREFIX)
    extension_prefix = ujoin(base_url, EXTENSION_PREFIX)
    handlers = [
        (prefix + r'/?', LabHandler, {
            'labextensions': labextensions,
            'extension_prefix': extension_prefix
        }),
        (prefix + r"/(.*)", FileFindHandler, {
            'path': BUILT_FILES
        }),
        (extension_prefix + r"/(.*)", FileFindHandler, {
            'path': jupyter_path('labextensions'),
            'no_cache_paths': ['/'],  # don't cache anything in labextensions
        })
    ]
    web_app.add_handlers(".*$", handlers)
Example #44
0
def load_jupyter_server_extension(nbapp):
    from jupyter_core.paths import jupyter_path
    from .labapp import get_labextensions

    base_dir = os.path.realpath(os.path.join(HERE, '..'))
    dev_mode = os.path.exists(os.path.join(base_dir, '.git'))
    if dev_mode:
        nbapp.log.info(DEV_NOTE_NPM)
    nbapp.log.info('JupyterLab alpha preview extension loaded from %s' % HERE)
    webapp = nbapp.web_app
    webapp.labextensions = get_labextensions(parent=nbapp)
    base_url = webapp.settings['base_url']
    webapp.add_handlers(".*$",
        [(ujoin(base_url, h[0]),) + h[1:] for h in default_handlers])
    labextension_handler = (
        r"%s/(.*)" % EXTENSION_PREFIX, FileFindHandler, {
            'path': jupyter_path('labextensions'),
            'no_cache_paths': ['/'],  # don't cache anything in labbextensions
        }
    )
    webapp.add_handlers(".*$", [labextension_handler])
Example #45
0
def validate_nbextension(require, logger=None):
    """Validate a named nbextension.

    Looks across all of the nbextension directories.

    Returns a list of warnings.

    require : str
        require.js path used to load the extension
    logger : Jupyter logger [optional]
        Logger instance to use
    """
    warnings = []
    infos = []

    js_exists = False
    for exts in jupyter_path('nbextensions'):
        # Does the Javascript entrypoint actually exist on disk?
        js = u"{}.js".format(os.path.join(exts, *require.split("/")))
        js_exists = os.path.exists(js)
        if js_exists:
            break

    require_tmpl = u"        - require? {} {}"
    if js_exists:
        infos.append(require_tmpl.format(GREEN_OK, require))
    else:
        warnings.append(require_tmpl.format(RED_X, require))
    
    if logger:
        if warnings:
            logger.warning(u"      - Validating: problems found:")
            for msg in warnings:
                logger.warning(msg)
            for msg in infos:
                logger.info(msg)
        else:
            logger.info(u"      - Validating: {}".format(GREEN_OK))
    
    return warnings
    def setUp(self):
        self.tempdirs = []
        self._mock_extensions = []

        self.test_dir = self.tempdir()
        self.data_dir = os.path.join(self.test_dir, 'data')
        self.config_dir = os.path.join(self.test_dir, 'config')
        self.system_data_dir = os.path.join(self.test_dir, 'system_data')
        self.system_config_dir = os.path.join(self.test_dir, 'system_config')
        self.system_path = [self.system_data_dir]
        self.system_config_path = [self.system_config_dir]
        
        self.patches = []
        p = patch.dict('os.environ', {
            'JUPYTER_CONFIG_DIR': self.config_dir,
            'JUPYTER_DATA_DIR': self.data_dir,
        })
        self.patches.append(p)
        for mod in (paths, nbextensions):
            p = patch.object(mod,
                'SYSTEM_JUPYTER_PATH', self.system_path)
            self.patches.append(p)
            p = patch.object(mod,
                'ENV_JUPYTER_PATH', [])
            self.patches.append(p)
        for mod in (paths, extensions):
            p = patch.object(mod,
                'SYSTEM_CONFIG_PATH', self.system_config_path)
            self.patches.append(p)
            p = patch.object(mod,
                'ENV_CONFIG_PATH', [])
            self.patches.append(p)
        for p in self.patches:
            p.start()
            self.addCleanup(p.stop)
        # verify our patches
        self.assertEqual(paths.jupyter_config_path(), [self.config_dir] + self.system_config_path)
        self.assertEqual(extensions._get_config_dir(user=False), self.system_config_dir)
        self.assertEqual(paths.jupyter_path(), [self.data_dir] + self.system_path)
Example #47
0
 def nbextensions_path(self):
     """The path to look for Javascript notebook extensions"""
     path = self.extra_nbextensions_path + jupyter_path('nbextensions')
     # FIXME: remove IPython nbextensions path once migration is setup
     path.append(os.path.join(get_ipython_dir(), 'nbextensions'))
     return path
def get_nbextension_path():
    """Find the path to the declarativewidgets nbextension"""
    for abspath in jupyter_path("nbextensions", "declarativewidgets"):
        if os.path.exists(abspath):
            return abspath
Example #49
0
 def labextensions_path(self):
     """The path to look for JupyterLab extensions"""
     return self.extra_labextensions_path + jupyter_path('labextensions')
Example #50
0
 def get_jupyter_path(self, path):
     return paths.jupyter_path(path)
Example #51
0
 def _kernel_dirs_default(self):
     dirs = jupyter_path('kernels')
     # FIXME: pending migration, include kernelspecs in .ipython:
     from IPython.paths import get_ipython_dir
     dirs.append(os.path.join(get_ipython_dir(), 'kernels'))
     return dirs
Example #52
0
def test_jupyter_path():
    system_path = ['system', 'path']
    with no_config_env, patch.object(paths, 'SYSTEM_JUPYTER_PATH', system_path):
        path = jupyter_path()
    assert path[0] == jupyter_data_dir()
    assert path[-2:] == system_path
Example #53
0
def test_jupyter_path_subdir():
    path = jupyter_path('sub1', 'sub2')
    for p in path:
        assert p.endswith(pjoin('', 'sub1', 'sub2'))
Example #54
0
def test_jupyter_path_sys_prefix():
    with patch.object(paths, 'ENV_JUPYTER_PATH', ['sys_prefix']):
        path = jupyter_path()
    assert 'sys_prefix' in path
Example #55
0
    def setUp(self):
        # Any TemporaryDirectory objects appended to this list will be cleaned
        # up at the end of the test run.
        self.tempdirs = []
        self._mock_extensions = []

        @self.addCleanup
        def cleanup_tempdirs():
            for d in self.tempdirs:
                d.cleanup()

        self.src = self.tempdir()
        self.name = 'mockextension'
        self.files = files = [
            pjoin(u'ƒile'),
            pjoin(u'∂ir', u'ƒile1'),
            pjoin(u'∂ir', u'∂ir2', u'ƒile2'),
        ]
        for file in files:
            fullpath = os.path.join(self.src, file)
            parent = os.path.dirname(fullpath)
            if not os.path.exists(parent):
                os.makedirs(parent)
            touch(fullpath)

        self.test_dir = self.tempdir()
        self.data_dir = os.path.join(self.test_dir, 'data')
        self.config_dir = os.path.join(self.test_dir, 'config')
        self.system_data_dir = os.path.join(self.test_dir, 'system_data')
        self.system_config_dir = os.path.join(self.test_dir, 'system_config')
        self.system_path = [self.system_data_dir]
        self.system_config_path = [self.system_config_dir]

        self.system_labext = os.path.join(self.system_data_dir, 'labextensions')

        self.patches = []
        p = patch.dict('os.environ', {
            'JUPYTER_CONFIG_DIR': self.config_dir,
            'JUPYTER_DATA_DIR': self.data_dir,
        })
        self.patches.append(p)
        for mod in (paths, labextensions):
            p = patch.object(mod,
                'SYSTEM_JUPYTER_PATH', self.system_path)
            self.patches.append(p)
            p = patch.object(mod,
                'ENV_JUPYTER_PATH', [])
            self.patches.append(p)
        for mod in (paths, labextensions):
            p = patch.object(mod,
                'SYSTEM_CONFIG_PATH', self.system_config_path)
            self.patches.append(p)
            p = patch.object(mod,
                'ENV_CONFIG_PATH', [])
            self.patches.append(p)
        for p in self.patches:
            p.start()
            self.addCleanup(p.stop)

        # verify our patches
        self.assertEqual(paths.jupyter_config_path(), [self.config_dir] + self.system_config_path)
        self.assertEqual(labextensions._get_config_dir(user=False), os.path.join(self.system_config_dir, CONFIG_DIR))
        self.assertEqual(paths.jupyter_path(), [self.data_dir] + self.system_path)