Skip to content

Commit

Permalink
whitespace + pep8 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsaj committed May 23, 2011
1 parent 87f8362 commit ea440c9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 76 deletions.
97 changes: 52 additions & 45 deletions IPython/utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

fs_encoding = sys.getfilesystemencoding()


def _cast_unicode(s, enc=None):
"""Turn 8-bit strings into unicode."""
if isinstance(s, bytes):
Expand All @@ -54,10 +55,11 @@ def _get_long_path_name(path):
try:
import ctypes
except ImportError:
raise ImportError('you need to have ctypes installed for this to work')
raise ImportError('you need to have ctypes installed '
'for this to work')
_GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
_GetLongPathName.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p,
ctypes.c_uint ]
ctypes.c_uint]

buf = ctypes.create_unicode_buffer(260)
rv = _GetLongPathName(path, buf, 260)
Expand Down Expand Up @@ -88,7 +90,7 @@ def get_py_filename(name):
if os.path.isfile(name):
return name
else:
raise IOError,'File `%s` not found.' % name
raise IOError('File `%s` not found.' % name)


def filefind(filename, path_dirs=None):
Expand All @@ -107,7 +109,7 @@ def filefind(filename, path_dirs=None):
Will find the file in the users home directory. This function does not
automatically try any paths, such as the cwd or the user's home directory.
Parameters
----------
filename : str
Expand All @@ -118,31 +120,32 @@ def filefind(filename, path_dirs=None):
put into a sequence and the searched. If a sequence, walk through
each element and join with ``filename``, calling :func:`expandvars`
and :func:`expanduser` before testing for existence.
Returns
-------
Raises :exc:`IOError` or returns absolute path to file.
"""

# If paths are quoted, abspath gets confused, strip them...
filename = filename.strip('"').strip("'")
# If the input is an absolute path, just check it exists
if os.path.isabs(filename) and os.path.isfile(filename):
return filename

if path_dirs is None:
path_dirs = ("",)
elif isinstance(path_dirs, basestring):
path_dirs = (path_dirs,)

for path in path_dirs:
if path == '.': path = os.getcwd()
if path == '.':
path = os.getcwd()
testname = expand_path(os.path.join(path, filename))
if os.path.isfile(testname):
return os.path.abspath(testname)
raise IOError("File %r does not exist in any of the search paths: %r" %
(filename, path_dirs) )

raise IOError("File %r does not exist in any of the search paths: %r" %
(filename, path_dirs))


class HomeDirError(Exception):
Expand All @@ -160,7 +163,7 @@ def get_home_dir():
- Registry hack for My Documents
- %HOME%: rare, but some people with unix-like setups may have defined it
* On Dos C:\
Currently only Posix and NT are implemented, a HomeDirError exception is
raised for all other OSes.
"""
Expand All @@ -171,12 +174,13 @@ def get_home_dir():
# first, check py2exe distribution root directory for _ipython.
# This overrides all. Normally does not exist.

if hasattr(sys, "frozen"): #Is frozen by py2exe
if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file
if hasattr(sys, "frozen"): # Is frozen by py2exe
# libraries compressed to zip-file
if '\\library.zip\\' in IPython.__file__.lower():
root, rest = IPython.__file__.lower().split('library.zip')
else:
root=os.path.join(os.path.split(IPython.__file__)[0],"../../")
root=os.path.abspath(root).rstrip('\\')
else:
root = os.path.join(os.path.split(IPython.__file__)[0], "../../")
root = os.path.abspath(root).rstrip('\\')
if isdir(os.path.join(root, '_ipython')):
os.environ["IPYKITROOT"] = root
return _cast_unicode(root, fs_encoding)
Expand All @@ -191,7 +195,7 @@ def get_home_dir():
# still knows it - reported once as:
# https://github.com/ipython/ipython/issues/154
from subprocess import Popen, PIPE
homedir = Popen('echo $HOME', shell=True,
homedir = Popen('echo $HOME', shell=True,
stdout=PIPE).communicate()[0].strip()
if homedir:
return _cast_unicode(homedir, fs_encoding)
Expand All @@ -204,7 +208,7 @@ def get_home_dir():
# For some strange reason all of these return 'nt' for os.name.
# First look for a network home directory. This will return the UNC
# path (\\server\\Users\%username%) not the mapped path (Z:\). This
# is needed when running IPython on cluster where all paths have to
# is needed when running IPython on cluster where all paths have to
# be UNC.
try:
homedir = env['HOMESHARE']
Expand All @@ -216,7 +220,7 @@ def get_home_dir():

# Now look for a local home directory
try:
homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
homedir = os.path.join(env['HOMEDRIVE'], env['HOMEPATH'])
except KeyError:
pass
else:
Expand All @@ -239,7 +243,7 @@ def get_home_dir():
wreg.HKEY_CURRENT_USER,
"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
)
homedir = wreg.QueryValueEx(key,'Personal')[0]
homedir = wreg.QueryValueEx(key, 'Personal')[0]
key.Close()
except:
pass
Expand All @@ -263,41 +267,44 @@ def get_home_dir():
# Desperate, may do absurd things in classic MacOS. May work under DOS.
return u'C:\\'
else:
raise HomeDirError('No valid home directory could be found for your OS')
raise HomeDirError('No valid home directory could be found '
'for your OS')


def get_xdg_dir():
"""Return the XDG_CONFIG_HOME, if it is defined and exists, else None.
This is only for posix (Linux,Unix,OS X, etc) systems.
"""

isdir = os.path.isdir
env = os.environ

if os.name == 'posix':
# Linux, Unix, AIX, OS X
# use ~/.config if not set OR empty
xdg = env.get("XDG_CONFIG_HOME", None) or os.path.join(get_home_dir(), '.config')
xdg = env.get("XDG_CONFIG_HOME", None) or \
os.path.join(get_home_dir(), '.config')
if xdg and isdir(xdg):
return _cast_unicode(xdg, fs_encoding)

return None


def get_ipython_dir():
"""Get the IPython directory for this platform and user.
This uses the logic in `get_home_dir` to find the home directory
and the adds .ipython to the end of the path.
"""

env = os.environ
pjoin = os.path.join
exists = os.path.exists

ipdir_def = '.ipython'
xdg_def = 'ipython'

home_dir = get_home_dir()
xdg_dir = get_xdg_dir()
# import pdb; pdb.set_trace() # dbg
Expand All @@ -308,12 +315,12 @@ def get_ipython_dir():
if xdg_dir:
# use XDG, as long as the user isn't already
# using $HOME/.ipython and *not* XDG/ipython

xdg_ipdir = pjoin(xdg_dir, xdg_def)

if exists(xdg_ipdir) or not exists(home_ipdir):
ipdir = xdg_ipdir

if ipdir is None:
# not using XDG
ipdir = home_ipdir
Expand Down Expand Up @@ -348,7 +355,7 @@ def expand_path(s):
"""Expand $VARS and ~names in a string, like a shell
:Examples:
In [2]: os.environ['FOO']='test'
In [3]: expand_path('variable FOO is $FOO')
Expand All @@ -359,18 +366,18 @@ def expand_path(s):
# the $ to get (\\server\share\%username%). I think it considered $
# alone an empty var. But, we need the $ to remains there (it indicates
# a hidden share).
if os.name=='nt':
if os.name == 'nt':
s = s.replace('$\\', 'IPYTHON_TEMP')
s = os.path.expandvars(os.path.expanduser(s))
if os.name=='nt':
if os.name == 'nt':
s = s.replace('IPYTHON_TEMP', '$\\')
return s


def target_outdated(target,deps):
def target_outdated(target, deps):
"""Determine whether a target is out of date.
target_outdated(target,deps) -> 1/0
target_outdated(target, deps) -> 1/0
deps: list of filenames which MUST exist.
target: single filename which may or may not exist.
Expand All @@ -391,17 +398,18 @@ def target_outdated(target,deps):
return 0


def target_update(target,deps,cmd):
def target_update(target, deps, cmd):
"""Update a target with a given command given a list of dependencies.
target_update(target,deps,cmd) -> runs cmd if target is outdated.
target_update(target, deps, cmd) -> runs cmd if target is outdated.
This is just a wrapper around target_outdated() which calls the given
command if target is outdated."""

if target_outdated(target,deps):
if target_outdated(target, deps):
system(cmd)


def check_for_old_config(ipython_dir=None):
"""Check for old config files, and present a warning if they exist.
Expand All @@ -421,5 +429,4 @@ def check_for_old_config(ipython_dir=None):
The IPython configuration system has changed as of 0.11, and this file will be ignored.
See http://ipython.github.com/ipython-doc/dev/config for details on the new config system.
The current default config file is 'ipython_config.py', where you can suppress these
warnings with `Global.ignore_old_config = True`."""%f)

warnings with `Global.ignore_old_config = True`.""" % f)

0 comments on commit ea440c9

Please sign in to comment.