Skip to content

Commit

Permalink
[2895114] _get_data_path: improve py2exe handling and readability.
Browse files Browse the repository at this point in the history
Also convert some imports to absolute.

svn path=/trunk/matplotlib/; revision=8434
  • Loading branch information
efiring committed Jun 14, 2010
1 parent ec438ca commit cb5b37b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
42 changes: 25 additions & 17 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@

import sys, os, tempfile

from rcsetup import defaultParams, validate_backend, validate_toolbar
from rcsetup import validate_cairo_format
from matplotlib.rcsetup import (defaultParams,
validate_backend,
validate_toolbar,
validate_cairo_format)

major, minor1, minor2, s, tmp = sys.version_info
_python24 = major>=2 and minor1>=4
Expand Down Expand Up @@ -478,27 +480,32 @@ def _get_data_path():
return path

path = os.sep.join([os.path.dirname(__file__), 'mpl-data'])
if os.path.isdir(path): return path
if os.path.isdir(path):
return path

# setuptools' namespace_packages may highjack this init file
# so need to try something known to be in matplotlib, not basemap
import matplotlib.afm
path = os.sep.join([os.path.dirname(matplotlib.afm.__file__), 'mpl-data'])
if os.path.isdir(path): return path
if os.path.isdir(path):
return path

# py2exe zips pure python, so still need special check
if getattr(sys,'frozen',None):
path = os.path.join(os.path.split(sys.path[0])[0], 'mpl-data')
if os.path.isdir(path): return path
else:
# Try again assuming we need to step up one more directory
path = os.path.join(os.path.split(os.path.split(sys.path[0])[0])[0],
'mpl-data')
if os.path.isdir(path): return path
else:
# Try again assuming sys.path[0] is a dir not a exe
path = os.path.join(sys.path[0], 'mpl-data')
if os.path.isdir(path): return path
exe_path = os.path.dirname(sys.executable)
path = os.path.join(exe_path, 'mpl-data')
if os.path.isdir(path):
return path

# Try again assuming we need to step up one more directory
path = os.path.join(os.path.split(exe_path)[0], 'mpl-data')
if os.path.isdir(path):
return path

# Try again assuming sys.path[0] is a dir not a exe
path = os.path.join(sys.path[0], 'mpl-data')
if os.path.isdir(path):
return path

raise RuntimeError('Could not find the matplotlib data files')

Expand Down Expand Up @@ -813,11 +820,12 @@ def rcdefaults():
if NEWCONFIG:
#print "importing from reorganized config system!"
try:
from config import rcParams, rcdefaults, mplConfig, save_config
from matplotlib.config import (rcParams, rcdefaults,
mplConfig, save_config)
verbose.set_level(rcParams['verbose.level'])
verbose.set_fileo(rcParams['verbose.fileo'])
except:
from config import rcParams, rcdefaults
from matplotlib.config import rcParams, rcdefaults

_use_error_msg = """ This call to matplotlib.use() has no effect
because the the backend has already been chosen;
Expand Down
40 changes: 25 additions & 15 deletions lib/matplotlib/config/cutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import warnings

# matplotlib imports
from verbose import verbose
from rcsetup import defaultParams
from matplotlib.verbose import verbose
from matplotlib.rcsetup import defaultParams

def is_string_like(obj):
try: obj + ''
Expand Down Expand Up @@ -89,6 +89,10 @@ def _get_configdir():

get_configdir = verbose.wrap('CONFIGDIR=%s', _get_configdir, always=False)

def _get_data_path():
'get the path to matplotlib data'

## The following is duplicated in matplotlib.__init__
def _get_data_path():
'get the path to matplotlib data'

Expand All @@ -99,30 +103,36 @@ def _get_data_path():
return path

path = os.sep.join([os.path.dirname(__file__), 'mpl-data'])
if os.path.isdir(path): return path
if os.path.isdir(path):
return path

# setuptools' namespace_packages may highjack this init file
# so need to try something known to be in matplotlib, not basemap
import matplotlib.afm
path = os.sep.join([os.path.dirname(matplotlib.afm.__file__), 'mpl-data'])
if os.path.isdir(path): return path
if os.path.isdir(path):
return path

# py2exe zips pure python, so still need special check
if getattr(sys,'frozen',None):
path = os.path.join(os.path.split(sys.path[0])[0], 'mpl-data')
if os.path.isdir(path): return path
else:
# Try again assuming we need to step up one more directory
path = os.path.join(os.path.split(os.path.split(sys.path[0])[0])[0],
'mpl-data')
if os.path.isdir(path): return path
else:
# Try again assuming sys.path[0] is a dir not a exe
path = os.path.join(sys.path[0], 'mpl-data')
if os.path.isdir(path): return path
exe_path = os.path.dirname(sys.executable)
path = os.path.join(exe_path, 'mpl-data')
if os.path.isdir(path):
return path

# Try again assuming we need to step up one more directory
path = os.path.join(os.path.split(exe_path)[0], 'mpl-data')
if os.path.isdir(path):
return path

# Try again assuming sys.path[0] is a dir not a exe
path = os.path.join(sys.path[0], 'mpl-data')
if os.path.isdir(path):
return path

raise RuntimeError('Could not find the matplotlib data files')


def _get_data_path_cached():
if defaultParams['datapath'][0] is None:
defaultParams['datapath'][0] = _get_data_path()
Expand Down

0 comments on commit cb5b37b

Please sign in to comment.