Ejemplo n.º 1
0
 def _mathjax_url_default(self):
     if not self.enable_mathjax:
         return u''
     static_url_prefix = self.tornado_settings.get("static_url_prefix",
                      url_path_join(self.base_url, "static")
     )
     
     # try local mathjax, either in nbextensions/mathjax or static/mathjax
     for (url_prefix, search_path) in [
         (url_path_join(self.base_url, "nbextensions"), self.nbextensions_path),
         (static_url_prefix, self.static_file_path),
     ]:
         self.log.debug("searching for local mathjax in %s", search_path)
         try:
             mathjax = filefind(os.path.join('mathjax', 'MathJax.js'), search_path)
         except IOError:
             continue
         else:
             url = url_path_join(url_prefix, u"mathjax/MathJax.js")
             self.log.info("Serving local MathJax from %s at %s", mathjax, url)
             return url
     
     # no local mathjax, serve from CDN
     url = u"https://cdn.mathjax.org/mathjax/latest/MathJax.js"
     self.log.info("Using MathJax from CDN: %s", url)
     return url
Ejemplo n.º 2
0
def find_connection_file(filename='kernel-*.json', path=None):
    """find a connection file, and return its absolute path.

    The current working directory and the profile's security
    directory will be searched for the file if it is not given by
    absolute path.

    If profile is unspecified, then the current running application's
    profile will be used, or 'default', if not run from IPython.

    If the argument does not match an existing file, it will be interpreted as a
    fileglob, and the matching file in the profile's security dir with
    the latest access time will be used.

    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    path : str or list of strs[optional]
        Paths in which to search for connection files.

    Returns
    -------
    str : The absolute path of the connection file.
    """
    if path is None:
        path = ['.']
    if isinstance(path, string_types):
        path = [path]
    
    try:
        # first, try explicit name
        return filefind(filename, path)
    except IOError:
        pass

    # not found by full name

    if '*' in filename:
        # given as a glob already
        pat = filename
    else:
        # accept any substring match
        pat = '*%s*' % filename
    
    matches = []
    for p in path:
        matches.extend(glob.glob(os.path.join(p, pat)))
    
    if not matches:
        raise IOError("Could not find %r in %r" % (filename, path))
    elif len(matches) == 1:
        return matches[0]
    else:
        # get most recent match, by access time:
        return sorted(matches, key=lambda f: os.stat(f).st_atime)[-1]
Ejemplo n.º 3
0
 def get_absolute_path(cls, roots, path):
     """locate a file to serve on our static file search path"""
     with cls._lock:
         if path in cls._static_paths:
             return cls._static_paths[path]
         try:
             abspath = os.path.abspath(filefind(path, roots))
         except IOError:
             # IOError means not found
             return ''
         
         cls._static_paths[path] = abspath
         return abspath
Ejemplo n.º 4
0
 def init_connection_file(self):
     """find the connection file, and load the info if found.
     
     The current working directory and the current profile's security
     directory will be searched for the file if it is not given by
     absolute path.
     
     When attempting to connect to an existing kernel and the `--existing`
     argument does not match an existing file, it will be interpreted as a
     fileglob, and the matching file in the current profile's security dir
     with the latest access time will be used.
     
     After this method is called, self.connection_file contains the *full path*
     to the connection file, never just its name.
     """
     if self.existing:
         try:
             cf = find_connection_file(self.existing, [self.runtime_dir])
         except Exception:
             self.log.critical("Could not find existing kernel connection file %s", self.existing)
             self.exit(1)
         self.log.debug("Connecting to existing kernel: %s" % cf)
         self.connection_file = cf
     else:
         # not existing, check if we are going to write the file
         # and ensure that self.connection_file is a full path, not just the shortname
         try:
             cf = find_connection_file(self.connection_file, [self.runtime_dir])
         except Exception:
             # file might not exist
             if self.connection_file == os.path.basename(self.connection_file):
                 # just shortname, put it in security dir
                 cf = os.path.join(self.runtime_dir, self.connection_file)
             else:
                 cf = self.connection_file
             self.connection_file = cf
     try:
         self.connection_file = filefind(self.connection_file, ['.', self.runtime_dir])
     except IOError:
         self.log.debug("Connection File not found: %s", self.connection_file)
         return
     
     # should load_connection_file only be used for existing?
     # as it is now, this allows reusing ports if an existing
     # file is requested
     try:
         self.load_connection_file()
     except Exception:
         self.log.error("Failed to load connection file: %r", self.connection_file, exc_info=True)
         self.exit(1)
Ejemplo n.º 5
0
 def init_connection_file(self):
     if not self.connection_file:
         self.connection_file = "kernel-%s.json"%os.getpid()
     try:
         self.connection_file = filefind(self.connection_file, ['.', self.connection_dir])
     except IOError:
         self.log.debug("Connection file not found: %s", self.connection_file)
         # This means I own it, so I will clean it up:
         atexit.register(self.cleanup_connection_file)
         return
     try:
         self.load_connection_file()
     except Exception:
         self.log.error("Failed to load connection file: %r", self.connection_file, exc_info=True)
         self.exit(1)
Ejemplo n.º 6
0
def get_connection_file(app=None):
    """Return the path to the connection file of an app

    Parameters
    ----------
    app : IPKernelApp instance [optional]
        If unspecified, the currently running app will be used
    """
    if app is None:
        from ipykernel.kernelapp import IPKernelApp
        if not IPKernelApp.initialized():
            raise RuntimeError("app not specified, and not in a running Kernel")

        app = IPKernelApp.instance()
    return filefind(app.connection_file, ['.', app.connection_dir])
Ejemplo n.º 7
0
def find_connection_file(filename='kernel-*.json', profile=None):
    """find a connection file, and return its absolute path.

    The current working directory and the profile's security
    directory will be searched for the file if it is not given by
    absolute path.

    If profile is unspecified, then the current running application's
    profile will be used, or 'default', if not run from IPython.

    If the argument does not match an existing file, it will be interpreted as a
    fileglob, and the matching file in the profile's security dir with
    the latest access time will be used.

    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    profile : str [optional]
        The name of the profile to use when searching for the connection file,
        if different from the current IPython session or 'default'.

    Returns
    -------
    str : The absolute path of the connection file.
    """
    from IPython.core.application import BaseIPythonApplication as IPApp
    try:
        # quick check for absolute path, before going through logic
        return filefind(filename)
    except IOError:
        pass

    if profile is None:
        # profile unspecified, check if running from an IPython app
        if IPApp.initialized():
            app = IPApp.instance()
            profile_dir = app.profile_dir
        else:
            # not running in IPython, use default profile
            profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), 'default')
    else:
        # find profiledir by profile name:
        profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
    security_dir = profile_dir.security_dir
    
    return jupyter_client.find_connection_file(filename, path=['.', security_dir])
Ejemplo n.º 8
0
 def init_connection_file(self):
     if not self.connection_file:
         self.connection_file = "kernel-%s.json" % os.getpid()
     try:
         self.connection_file = filefind(self.connection_file, [".", self.connection_dir])
     except IOError:
         self.log.debug("Connection file not found: %s", self.connection_file)
         # This means I own it, and I'll create it in this directory:
         ensure_dir_exists(os.path.dirname(self.abs_connection_file), 0o700)
         # Also, I will clean it up:
         atexit.register(self.cleanup_connection_file)
         return
     try:
         self.load_connection_file()
     except Exception:
         self.log.error("Failed to load connection file: %r", self.connection_file, exc_info=True)
         self.exit(1)
Ejemplo n.º 9
0
def find_connection_file(filename='kernel-*.json', profile=None):
    """DEPRECATED: find a connection file, and return its absolute path.
    
    THIS FUNCION IS DEPRECATED. Use juptyer_client.find_connection_file instead.

    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    profile : str [optional]
        The name of the profile to use when searching for the connection file,
        if different from the current IPython session or 'default'.

    Returns
    -------
    str : The absolute path of the connection file.
    """
    
    import warnings
    warnings.warn("""ipykernel.find_connection_file is deprecated, use jupyter_client.find_connection_file""",
        DeprecationWarning, stacklevel=2)
    from IPython.core.application import BaseIPythonApplication as IPApp
    try:
        # quick check for absolute path, before going through logic
        return filefind(filename)
    except IOError:
        pass

    if profile is None:
        # profile unspecified, check if running from an IPython app
        if IPApp.initialized():
            app = IPApp.instance()
            profile_dir = app.profile_dir
        else:
            # not running in IPython, use default profile
            profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), 'default')
    else:
        # find profiledir by profile name:
        profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
    security_dir = profile_dir.security_dir
    
    return jupyter_client.find_connection_file(filename, path=['.', security_dir])
Ejemplo n.º 10
0
 def _find_file(self):
     """Try to find the file by searching the paths."""
     self.full_filename = filefind(self.filename, self.path)
Ejemplo n.º 11
0
 def _find_file(self):
     """Try to find the file by searching the paths."""
     self.full_filename = filefind(self.filename, self.path)
Ejemplo n.º 12
0
    def init_connection_file(self):
        """find the connection file, and load the info if found.

        The current working directory and the current profile's security
        directory will be searched for the file if it is not given by
        absolute path.

        When attempting to connect to an existing kernel and the `--existing`
        argument does not match an existing file, it will be interpreted as a
        fileglob, and the matching file in the current profile's security dir
        with the latest access time will be used.

        After this method is called, self.connection_file contains
        the *full path* to the connection file, never just its name.
        """
        if self.existing:
            self.shell_port = 0
            self.iopub_port = 0
            self.stdin_port = 0
            self.hb_port = 0
            try:
                cf = find_connection_file(
                    self.existing, ['.', self.runtime_dir])
            except IOError:
                self.log.critical("Could not find existing \
                                  kernel connection file %s", self.existing)

            self.log.debug("Connecting to existing kernel: %s" % cf)
            self.connection_file = cf
        else:
            # not existing, check if we are going to write the file
            # and ensure that self.connection_file is a full path,
            # not just the shortname
            try:
                cf = find_connection_file(
                    self.connection_file, [self.runtime_dir])
            except Exception:
                # file might not exist
                if self.connection_file == os.path.basename(
                                                    self.connection_file):
                    # just shortname, put it in security dir
                    cf = os.path.join(self.runtime_dir, self.connection_file)
                else:
                    cf = self.connection_file
                self.connection_file = cf
        try:
            self.connection_file = filefind(
                self.connection_file, ['.', self.runtime_dir])
        except IOError:
            self.log.debug(
                "Connection File not found: %s", self.connection_file)
            return

        # should load_connection_file only be used for existing?
        # as it is now, this allows reusing ports if an existing
        # file is requested
        try:
            self.load_connection_file()
        except Exception:
            self.log.error("Failed to load connection file: %r",
                           self.connection_file, exc_info=True)