##// END OF EJS Templates
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ipython branch. This is not a true merge in the formal sense because all history is not coming over with the files. For a detailed history of the added files, please see the ipython1-dev branch or the svn repository on scipy.org that ipython1-dev came from. More specifically, here is what I have done in this commit: 1) Moved the following by hand ipython1.config -> IPython.config ipython1.kernel -> IPython.kernel ipython1.external -> IPython.external ipython1.core -> IPython.kernel.core ipython1.testutils -> IPython.testing ipython1.tools -> IPython.tools 2) Moved IPython.tools.guid -> IPython1.external.guid 3) Renamed: ipython1 -> IPython IPython.core -> IPython.kernel.core IPython.testutils -> IPython.testing 4) Then did a "bzr add" for all the new stuff. That is all folks!

File last commit:

r1234:52b55407
r1234:52b55407
Show More
cutils.py
99 lines | 3.6 KiB | text/x-python | PythonLexer
# encoding: utf-8
"""Configuration-related utilities for all IPython."""
__docformat__ = "restructuredtext en"
#-------------------------------------------------------------------------------
# Copyright (C) 2008 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------
import os
import sys
#---------------------------------------------------------------------------
# Normal code begins
#---------------------------------------------------------------------------
class HomeDirError(Exception):
pass
def get_home_dir():
"""Return the closest possible equivalent to a 'home' directory.
We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
Currently only Posix and NT are implemented, a HomeDirError exception is
raised for all other OSes. """
isdir = os.path.isdir
env = os.environ
try:
homedir = env['HOME']
if not isdir(homedir):
# in case a user stuck some string which does NOT resolve to a
# valid path, it's as good as if we hadn't foud it
raise KeyError
return homedir
except KeyError:
if os.name == 'posix':
raise HomeDirError,'undefined $HOME, IPython can not proceed.'
elif os.name == 'nt':
# For some strange reason, win9x returns 'nt' for os.name.
try:
homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
if not isdir(homedir):
homedir = os.path.join(env['USERPROFILE'])
if not isdir(homedir):
raise HomeDirError
return homedir
except:
try:
# Use the registry to get the 'My Documents' folder.
import _winreg as wreg
key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
homedir = wreg.QueryValueEx(key,'Personal')[0]
key.Close()
if not isdir(homedir):
e = ('Invalid "Personal" folder registry key '
'typically "My Documents".\n'
'Value: %s\n'
'This is not a valid directory on your system.' %
homedir)
raise HomeDirError(e)
return homedir
except HomeDirError:
raise
except:
return 'C:\\'
elif os.name == 'dos':
# Desperate, may do absurd things in classic MacOS. May work under DOS.
return 'C:\\'
else:
raise HomeDirError,'support for your operating system not implemented.'
def get_ipython_dir():
ipdir_def = '.ipython'
home_dir = get_home_dir()
ipdir = os.path.abspath(os.environ.get('IPYTHONDIR',
os.path.join(home_dir,ipdir_def)))
return ipdir
def import_item(key):
"""
Import and return bar given the string foo.bar.
"""
package = '.'.join(key.split('.')[0:-1])
obj = key.split('.')[-1]
execString = 'from %s import %s' % (package, obj)
exec execString
exec 'temp = %s' % obj
return temp