##// END OF EJS Templates
Move IPython specific functions from utils to IPython.paths
Thomas Kluyver -
Show More
@@ -0,0 +1,149 b''
1 import os.path
2 import shutil
3 import tempfile
4 from warnings import warn
5
6 import IPython
7 from IPython.utils.importstring import import_item
8 from IPython.utils.path import (
9 get_home_dir, get_xdg_dir, get_xdg_cache_dir, compress_user, _writable_dir,
10 ensure_dir_exists, fs_encoding, filefind
11 )
12 from IPython.utils import py3compat
13
14 def get_ipython_dir():
15 """Get the IPython directory for this platform and user.
16
17 This uses the logic in `get_home_dir` to find the home directory
18 and then adds .ipython to the end of the path.
19 """
20
21 env = os.environ
22 pjoin = os.path.join
23
24
25 ipdir_def = '.ipython'
26
27 home_dir = get_home_dir()
28 xdg_dir = get_xdg_dir()
29
30 # import pdb; pdb.set_trace() # dbg
31 if 'IPYTHON_DIR' in env:
32 warn('The environment variable IPYTHON_DIR is deprecated. '
33 'Please use IPYTHONDIR instead.')
34 ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None))
35 if ipdir is None:
36 # not set explicitly, use ~/.ipython
37 ipdir = pjoin(home_dir, ipdir_def)
38 if xdg_dir:
39 # Several IPython versions (up to 1.x) defaulted to .config/ipython
40 # on Linux. We have decided to go back to using .ipython everywhere
41 xdg_ipdir = pjoin(xdg_dir, 'ipython')
42
43 if _writable_dir(xdg_ipdir):
44 cu = compress_user
45 if os.path.exists(ipdir):
46 warn(('Ignoring {0} in favour of {1}. Remove {0} to '
47 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
48 elif os.path.islink(xdg_ipdir):
49 warn(('{0} is deprecated. Move link to {1} to '
50 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
51 else:
52 warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir)))
53 shutil.move(xdg_ipdir, ipdir)
54
55 ipdir = os.path.normpath(os.path.expanduser(ipdir))
56
57 if os.path.exists(ipdir) and not _writable_dir(ipdir):
58 # ipdir exists, but is not writable
59 warn("IPython dir '{0}' is not a writable location,"
60 " using a temp directory.".format(ipdir))
61 ipdir = tempfile.mkdtemp()
62 elif not os.path.exists(ipdir):
63 parent = os.path.dirname(ipdir)
64 if not _writable_dir(parent):
65 # ipdir does not exist and parent isn't writable
66 warn("IPython parent '{0}' is not a writable location,"
67 " using a temp directory.".format(parent))
68 ipdir = tempfile.mkdtemp()
69
70 return py3compat.cast_unicode(ipdir, fs_encoding)
71
72
73 def get_ipython_cache_dir():
74 """Get the cache directory it is created if it does not exist."""
75 xdgdir = get_xdg_cache_dir()
76 if xdgdir is None:
77 return get_ipython_dir()
78 ipdir = os.path.join(xdgdir, "ipython")
79 if not os.path.exists(ipdir) and _writable_dir(xdgdir):
80 ensure_dir_exists(ipdir)
81 elif not _writable_dir(xdgdir):
82 return get_ipython_dir()
83
84 return py3compat.cast_unicode(ipdir, fs_encoding)
85
86
87 def get_ipython_package_dir():
88 """Get the base directory where IPython itself is installed."""
89 ipdir = os.path.dirname(IPython.__file__)
90 return py3compat.cast_unicode(ipdir, fs_encoding)
91
92
93 def get_ipython_module_path(module_str):
94 """Find the path to an IPython module in this version of IPython.
95
96 This will always find the version of the module that is in this importable
97 IPython package. This will always return the path to the ``.py``
98 version of the module.
99 """
100 if module_str == 'IPython':
101 return os.path.join(get_ipython_package_dir(), '__init__.py')
102 mod = import_item(module_str)
103 the_path = mod.__file__.replace('.pyc', '.py')
104 the_path = the_path.replace('.pyo', '.py')
105 return py3compat.cast_unicode(the_path, fs_encoding)
106
107 def locate_profile(profile='default'):
108 """Find the path to the folder associated with a given profile.
109
110 I.e. find $IPYTHONDIR/profile_whatever.
111 """
112 from IPython.core.profiledir import ProfileDir, ProfileDirError
113 try:
114 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
115 except ProfileDirError:
116 # IOError makes more sense when people are expecting a path
117 raise IOError("Couldn't find profile %r" % profile)
118 return pd.location
119
120 def get_security_file(filename, profile='default'):
121 """Return the absolute path of a security file given by filename and profile
122
123 This allows users and developers to find security files without
124 knowledge of the IPython directory structure. The search path
125 will be ['.', profile.security_dir]
126
127 Parameters
128 ----------
129
130 filename : str
131 The file to be found. If it is passed as an absolute path, it will
132 simply be returned.
133 profile : str [default: 'default']
134 The name of the profile to search. Leaving this unspecified
135 The file to be found. If it is passed as an absolute path, fname will
136 simply be returned.
137
138 Returns
139 -------
140 Raises :exc:`IOError` if file not found or returns absolute path to file.
141 """
142 # import here, because profiledir also imports from utils.path
143 from IPython.core.profiledir import ProfileDir
144 try:
145 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
146 except Exception:
147 # will raise ProfileDirError if no such profile
148 raise IOError("Profile %r not found")
149 return filefind(filename, ['.', pd.security_dir])
@@ -16,10 +16,8 b' import glob'
16 from warnings import warn
16 from warnings import warn
17 from hashlib import md5
17 from hashlib import md5
18
18
19 import IPython
20 from IPython.testing.skipdoctest import skip_doctest
19 from IPython.testing.skipdoctest import skip_doctest
21 from IPython.utils.process import system
20 from IPython.utils.process import system
22 from IPython.utils.importstring import import_item
23 from IPython.utils import py3compat
21 from IPython.utils import py3compat
24 from IPython.utils.decorators import undoc
22 from IPython.utils.decorators import undoc
25
23
@@ -256,111 +254,35 b' def get_xdg_cache_dir():'
256 return None
254 return None
257
255
258
256
257 @undoc
259 def get_ipython_dir():
258 def get_ipython_dir():
260 """Get the IPython directory for this platform and user.
259 warn("get_ipython_dir has moved to the IPython.paths module")
261
260 from IPython.paths import get_ipython_dir
262 This uses the logic in `get_home_dir` to find the home directory
261 return get_ipython_dir()
263 and then adds .ipython to the end of the path.
264 """
265
266 env = os.environ
267 pjoin = os.path.join
268
269
270 ipdir_def = '.ipython'
271
272 home_dir = get_home_dir()
273 xdg_dir = get_xdg_dir()
274
275 # import pdb; pdb.set_trace() # dbg
276 if 'IPYTHON_DIR' in env:
277 warn('The environment variable IPYTHON_DIR is deprecated. '
278 'Please use IPYTHONDIR instead.')
279 ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None))
280 if ipdir is None:
281 # not set explicitly, use ~/.ipython
282 ipdir = pjoin(home_dir, ipdir_def)
283 if xdg_dir:
284 # Several IPython versions (up to 1.x) defaulted to .config/ipython
285 # on Linux. We have decided to go back to using .ipython everywhere
286 xdg_ipdir = pjoin(xdg_dir, 'ipython')
287
288 if _writable_dir(xdg_ipdir):
289 cu = compress_user
290 if os.path.exists(ipdir):
291 warn(('Ignoring {0} in favour of {1}. Remove {0} to '
292 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
293 elif os.path.islink(xdg_ipdir):
294 warn(('{0} is deprecated. Move link to {1} to '
295 'get rid of this message').format(cu(xdg_ipdir), cu(ipdir)))
296 else:
297 warn('Moving {0} to {1}'.format(cu(xdg_ipdir), cu(ipdir)))
298 shutil.move(xdg_ipdir, ipdir)
299
300 ipdir = os.path.normpath(os.path.expanduser(ipdir))
301
302 if os.path.exists(ipdir) and not _writable_dir(ipdir):
303 # ipdir exists, but is not writable
304 warn("IPython dir '{0}' is not a writable location,"
305 " using a temp directory.".format(ipdir))
306 ipdir = tempfile.mkdtemp()
307 elif not os.path.exists(ipdir):
308 parent = os.path.dirname(ipdir)
309 if not _writable_dir(parent):
310 # ipdir does not exist and parent isn't writable
311 warn("IPython parent '{0}' is not a writable location,"
312 " using a temp directory.".format(parent))
313 ipdir = tempfile.mkdtemp()
314
315 return py3compat.cast_unicode(ipdir, fs_encoding)
316
317
262
263 @undoc
318 def get_ipython_cache_dir():
264 def get_ipython_cache_dir():
319 """Get the cache directory it is created if it does not exist."""
265 warn("get_ipython_cache_dir has moved to the IPython.paths module")
320 xdgdir = get_xdg_cache_dir()
266 from IPython.paths import get_ipython_cache_dir
321 if xdgdir is None:
267 return get_ipython_cache_dir()
322 return get_ipython_dir()
323 ipdir = os.path.join(xdgdir, "ipython")
324 if not os.path.exists(ipdir) and _writable_dir(xdgdir):
325 ensure_dir_exists(ipdir)
326 elif not _writable_dir(xdgdir):
327 return get_ipython_dir()
328
329 return py3compat.cast_unicode(ipdir, fs_encoding)
330
331
268
269 @undoc
332 def get_ipython_package_dir():
270 def get_ipython_package_dir():
333 """Get the base directory where IPython itself is installed."""
271 warn("get_ipython_package_dir has moved to the IPython.paths module")
334 ipdir = os.path.dirname(IPython.__file__)
272 from IPython.paths import get_ipython_package_dir
335 return py3compat.cast_unicode(ipdir, fs_encoding)
273 return get_ipython_package_dir()
336
337
274
275 @undoc
338 def get_ipython_module_path(module_str):
276 def get_ipython_module_path(module_str):
339 """Find the path to an IPython module in this version of IPython.
277 warn("get_ipython_module_path has moved to the IPython.paths module")
340
278 from IPython.paths import get_ipython_module_path
341 This will always find the version of the module that is in this importable
279 return get_ipython_module_path(module_str)
342 IPython package. This will always return the path to the ``.py``
343 version of the module.
344 """
345 if module_str == 'IPython':
346 return os.path.join(get_ipython_package_dir(), '__init__.py')
347 mod = import_item(module_str)
348 the_path = mod.__file__.replace('.pyc', '.py')
349 the_path = the_path.replace('.pyo', '.py')
350 return py3compat.cast_unicode(the_path, fs_encoding)
351
280
281 @undoc
352 def locate_profile(profile='default'):
282 def locate_profile(profile='default'):
353 """Find the path to the folder associated with a given profile.
283 warn("locate_profile has moved to the IPython.paths module")
354
284 from IPython.paths import locate_profile
355 I.e. find $IPYTHONDIR/profile_whatever.
285 return locate_profile(profile=profile)
356 """
357 from IPython.core.profiledir import ProfileDir, ProfileDirError
358 try:
359 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
360 except ProfileDirError:
361 # IOError makes more sense when people are expecting a path
362 raise IOError("Couldn't find profile %r" % profile)
363 return pd.location
364
286
365 def expand_path(s):
287 def expand_path(s):
366 """Expand $VARS and ~names in a string, like a shell
288 """Expand $VARS and ~names in a string, like a shell
@@ -453,36 +375,11 b' def filehash(path):'
453 with open(path, "rU") as f:
375 with open(path, "rU") as f:
454 return md5(py3compat.str_to_bytes(f.read())).hexdigest()
376 return md5(py3compat.str_to_bytes(f.read())).hexdigest()
455
377
378 @undoc
456 def get_security_file(filename, profile='default'):
379 def get_security_file(filename, profile='default'):
457 """Return the absolute path of a security file given by filename and profile
380 warn("get_security_file has moved to the IPython.paths module")
458
381 from IPython.paths import get_security_file
459 This allows users and developers to find security files without
382 return get_security_file(filename, profile=profile)
460 knowledge of the IPython directory structure. The search path
461 will be ['.', profile.security_dir]
462
463 Parameters
464 ----------
465
466 filename : str
467 The file to be found. If it is passed as an absolute path, it will
468 simply be returned.
469 profile : str [default: 'default']
470 The name of the profile to search. Leaving this unspecified
471 The file to be found. If it is passed as an absolute path, fname will
472 simply be returned.
473
474 Returns
475 -------
476 Raises :exc:`IOError` if file not found or returns absolute path to file.
477 """
478 # import here, because profiledir also imports from utils.path
479 from IPython.core.profiledir import ProfileDir
480 try:
481 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
482 except Exception:
483 # will raise ProfileDirError if no such profile
484 raise IOError("Profile %r not found")
485 return filefind(filename, ['.', pd.security_dir])
486
383
487
384
488 ENOLINK = 1998
385 ENOLINK = 1998
General Comments 0
You need to be logged in to leave comments. Login now