Exemplo n.º 1
0
def test_get_ipython_dir_7():
    """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
    home_dir = os.path.normpath(os.path.expanduser('~'))
    with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \
            patch.object(paths, '_writable_dir', return_value=True):
        ipdir = paths.get_ipython_dir()
    nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
Exemplo n.º 2
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 yap_ipython session or 'default'.

    Returns
    -------
    str : The absolute path of the connection file.
    """

    import warnings
    warnings.warn(
        """yap_kernel.find_connection_file is deprecated, use jupyter_client.find_connection_file""",
        DeprecationWarning,
        stacklevel=2)
    from yap_ipython.core.application import BaseYAPApplication 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 yap_ipython app
        if IPApp.initialized():
            app = IPApp.instance()
            profile_dir = app.profile_dir
        else:
            # not running in yap_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])
Exemplo n.º 3
0
def test_get_ipython_dir_1():
    """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
    env_ipdir = os.path.join("someplace", ".ipython")
    with patch.object(paths, '_writable_dir', return_value=True), \
            modified_env({'IPYTHONDIR': env_ipdir}):
        ipdir = paths.get_ipython_dir()

    nt.assert_equal(ipdir, env_ipdir)
Exemplo n.º 4
0
 def _ipython_dir_default(self):
     d = get_ipython_dir()
     self._ipython_dir_changed({
         'name': 'ipython_dir',
         'old': d,
         'new': d,
     })
     return d
Exemplo n.º 5
0
def test_get_ipython_dir_8():
    """test_get_ipython_dir_8, test / home directory"""
    with patch.object(paths, '_writable_dir', lambda path: bool(path)), \
            patch.object(paths, 'get_xdg_dir', return_value=None), \
            modified_env({
                'IPYTHON_DIR': None,
                'IPYTHONDIR': None,
                'HOME': '/',
            }):
        nt.assert_equal(paths.get_ipython_dir(), '/.ipython')
Exemplo n.º 6
0
 def load_subconfig(self, fname, path=None, profile=None):
     if profile is not None:
         try:
             profile_dir = ProfileDir.find_profile_dir_by_name(
                     get_ipython_dir(),
                     profile,
             )
         except ProfileDirError:
             return
         path = profile_dir.location
     return super(ProfileAwareConfigLoader, self).load_subconfig(fname, path=path)
Exemplo n.º 7
0
def test_get_ipython_dir_2():
    """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
    with patch_get_home_dir('someplace'), \
            patch.object(paths, 'get_xdg_dir', return_value=None), \
            patch.object(paths, '_writable_dir', return_value=True), \
            patch('os.name', "posix"), \
            modified_env({'IPYTHON_DIR': None,
                          'IPYTHONDIR': None,
                          'XDG_CONFIG_HOME': None
                         }):
        ipdir = paths.get_ipython_dir()

    nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
Exemplo n.º 8
0
def test_get_ipython_dir_6():
    """test_get_ipython_dir_6, use home over XDG if defined and neither exist."""
    xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
    os.mkdir(xdg)
    shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
    print(paths._writable_dir)
    with patch_get_home_dir(HOME_TEST_DIR), \
            patch.object(paths, 'get_xdg_dir', return_value=xdg), \
            patch('os.name', 'posix'), \
            modified_env({
                'IPYTHON_DIR': None,
                'IPYTHONDIR': None,
                'XDG_CONFIG_HOME': None,
            }), warnings.catch_warnings(record=True) as w:
        ipdir = paths.get_ipython_dir()

    nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython'))
    nt.assert_equal(len(w), 0)
Exemplo n.º 9
0
def test_get_ipython_dir_5():
    """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
    with patch_get_home_dir(HOME_TEST_DIR), \
            patch('os.name', 'posix'):
        try:
            os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise

        with modified_env({
                'IPYTHON_DIR': None,
                'IPYTHONDIR': None,
                'XDG_CONFIG_HOME': XDG_TEST_DIR,
        }):
            ipdir = paths.get_ipython_dir()

        nt.assert_equal(ipdir, IP_TEST_DIR)
Exemplo n.º 10
0
def test_get_ipython_dir_3():
    """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist."""
    tmphome = TemporaryDirectory()
    try:
        with patch_get_home_dir(tmphome.name), \
                patch('os.name', 'posix'), \
                modified_env({
                    'IPYTHON_DIR': None,
                    'IPYTHONDIR': None,
                    'XDG_CONFIG_HOME': XDG_TEST_DIR,
                }), warnings.catch_warnings(record=True) as w:
            ipdir = paths.get_ipython_dir()

        nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython"))
        if sys.platform != 'darwin':
            nt.assert_equal(len(w), 1)
            nt.assert_in('Moving', str(w[0]))
    finally:
        tmphome.cleanup()
Exemplo n.º 11
0
def test_get_ipython_cache_dir():
    with modified_env({'HOME': HOME_TEST_DIR}):
        if os.name == 'posix' and sys.platform != 'darwin':
            # test default
            os.makedirs(os.path.join(HOME_TEST_DIR, ".cache"))
            with modified_env({'XDG_CACHE_HOME': None}):
                ipdir = paths.get_ipython_cache_dir()
            nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"),
                            ipdir)
            assert_isdir(ipdir)

            # test env override
            with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}):
                ipdir = paths.get_ipython_cache_dir()
            assert_isdir(ipdir)
            nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython"))
        else:
            nt.assert_equal(paths.get_ipython_cache_dir(),
                            paths.get_ipython_dir())
Exemplo n.º 12
0
def test_not_writable_ipdir():
    tmpdir = tempfile.mkdtemp()
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env.pop('XDG_CONFIG_HOME', None)
    env['HOME'] = tmpdir
    ipdir = os.path.join(tmpdir, '.ipython')
    os.mkdir(ipdir, 0o555)
    try:
        open(os.path.join(ipdir, "_foo_"), 'w').close()
    except IOError:
        pass
    else:
        # I can still write to an unwritable dir,
        # assume I'm root and skip the test
        raise SkipTest("I can't create directories that I can't write to")
    with AssertPrints('is not a writable location', channel='stderr'):
        ipdir = paths.get_ipython_dir()
    env.pop('IPYTHON_DIR', None)
Exemplo n.º 13
0
def test_get_ipython_dir_4():
    """test_get_ipython_dir_4, warn if XDG and home both exist."""
    with patch_get_home_dir(HOME_TEST_DIR), \
            patch('os.name', 'posix'):
        try:
            os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython'))
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

        with modified_env({
                'IPYTHON_DIR': None,
                'IPYTHONDIR': None,
                'XDG_CONFIG_HOME': XDG_TEST_DIR,
        }), warnings.catch_warnings(record=True) as w:
            ipdir = paths.get_ipython_dir()

        nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython"))
        if sys.platform != 'darwin':
            nt.assert_equal(len(w), 1)
            nt.assert_in('Ignoring', str(w[0]))
Exemplo n.º 14
0
def get_ipython_dir():
    warn(
        "get_ipython_dir has moved to the yap_ipython.paths module since yap_ipython 4.0.",
        stacklevel=2)
    from yap_ipython.paths import get_ipython_dir
    return get_ipython_dir()
Exemplo n.º 15
0
class ProfileList(Application):
    name = u'ipython-profile'
    description = list_help
    examples = _list_examples

    aliases = Dict({
        'ipython-dir': 'ProfileList.ipython_dir',
        'log-level': 'Application.log_level',
    })
    flags = Dict(
        dict(debug=({
            'Application': {
                'log_level': 0
            }
        }, "Set Application.log_level to 0, maximizing log output.")))

    ipython_dir = Unicode(get_ipython_dir(),
                          help="""
        The name of the yap_ipython directory. This directory is used for logging
        configuration (through profiles), history storage, etc. The default
        is usually $HOME/.ipython. This options can also be specified through
        the environment variable IPYTHONDIR.
        """).tag(config=True)

    def _print_profiles(self, profiles):
        """print list of profiles, indented."""
        for profile in profiles:
            print('    %s' % profile)

    def list_profile_dirs(self):
        profiles = list_bundled_profiles()
        if profiles:
            print()
            print("Available profiles in yap_ipython:")
            self._print_profiles(profiles)
            print()
            print("    The first request for a bundled profile will copy it")
            print("    into your yap_ipython directory (%s)," %
                  self.ipython_dir)
            print("    where you can customize it.")

        profiles = list_profiles_in(self.ipython_dir)
        if profiles:
            print()
            print("Available profiles in %s:" % self.ipython_dir)
            self._print_profiles(profiles)

        profiles = list_profiles_in(os.getcwd())
        if profiles:
            print()
            print("Available profiles in current directory (%s):" %
                  os.getcwd())
            self._print_profiles(profiles)

        print()
        print("To use any of the above profiles, start yap_ipython with:")
        print("    ipython --profile=<name>")
        print()

    def start(self):
        self.list_profile_dirs()
Exemplo n.º 16
0
def test_filefind():
    """Various tests for filefind"""
    f = tempfile.NamedTemporaryFile()
    # print 'fname:',f.name
    alt_dirs = paths.get_ipython_dir()
    t = path.filefind(f.name, alt_dirs)