Skip to content

Commit

Permalink
load_subconfig supports profiles
Browse files Browse the repository at this point in the history
closes gh-523
  • Loading branch information
minrk committed Jun 17, 2011
1 parent 1d02df3 commit a7380bd
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 24 deletions.
29 changes: 23 additions & 6 deletions IPython/config/loader.py
Expand Up @@ -23,7 +23,7 @@
import sys

from IPython.external import argparse
from IPython.utils.path import filefind
from IPython.utils.path import filefind, get_ipython_dir

#-----------------------------------------------------------------------------
# Exceptions
Expand Down Expand Up @@ -269,23 +269,40 @@ def _find_file(self):
def _read_file_as_dict(self):
"""Load the config file into self.config, with recursive loading."""
# This closure is made available in the namespace that is used
# to exec the config file. This allows users to call
# to exec the config file. It allows users to call
# load_subconfig('myconfig.py') to load config files recursively.
# It needs to be a closure because it has references to self.path
# and self.config. The sub-config is loaded with the same path
# as the parent, but it uses an empty config which is then merged
# with the parents.
def load_subconfig(fname):
loader = PyFileConfigLoader(fname, self.path)

# If a profile is specified, the config file will be loaded
# from that profile

def load_subconfig(fname, profile=None):
# import here to prevent circular imports
from IPython.core.profiledir import ProfileDir, ProfileDirError
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
else:
path = self.path
loader = PyFileConfigLoader(fname, path)
try:
sub_config = loader.load_config()
except IOError:
# Pass silently if the sub config is not there. This happens
# when a user us using a profile, but not the default config.
# when a user s using a profile, but not the default config.
pass
else:
self.config._merge(sub_config)

# Again, this needs to be a closure and should be used in config
# files to get the config being loaded.
def get_config():
Expand Down
5 changes: 2 additions & 3 deletions IPython/config/profile/cluster/ipython_config.py
@@ -1,10 +1,9 @@
c = get_config()
app = c.IPythonApp
app = c.InteractiveShellApp

# This can be used at any point in a config file to load a sub config
# and merge it into the current one.
import os
load_subconfig(os.path.join('..','profile_default', 'ipython_config.py'))
load_subconfig('ipython_config.py', profile='default')

lines = """
from IPython.parallel import *
Expand Down
5 changes: 2 additions & 3 deletions IPython/config/profile/math/ipython_config.py
@@ -1,10 +1,9 @@
c = get_config()
app = c.IPythonApp
app = c.InteractiveShellApp

# This can be used at any point in a config file to load a sub config
# and merge it into the current one.
import os
load_subconfig(os.path.join('..','profile_default', 'ipython_config.py'))
load_subconfig('ipython_config.py', profile='default')

lines = """
import cmath
Expand Down
5 changes: 2 additions & 3 deletions IPython/config/profile/pylab/ipython_config.py
@@ -1,10 +1,9 @@
c = get_config()
app = c.IPythonApp
app = c.InteractiveShellApp

# This can be used at any point in a config file to load a sub config
# and merge it into the current one.
import os
load_subconfig(os.path.join('..','profile_default', 'ipython_config.py'))
load_subconfig('ipython_config.py', profile='default')

lines = """
import matplotlib
Expand Down
5 changes: 2 additions & 3 deletions IPython/config/profile/pysh/ipython_config.py
@@ -1,10 +1,9 @@
c = get_config()
app = c.IPythonApp
app = c.InteractiveShellApp

# This can be used at any point in a config file to load a sub config
# and merge it into the current one.
import os
load_subconfig(os.path.join('..','profile_default', 'ipython_config.py'))
load_subconfig('ipython_config.py', profile='default')

c.InteractiveShell.prompt_in1 = '\C_LightGreen\u@\h\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> '
c.InteractiveShell.prompt_in2 = '\C_Green|\C_LightGreen\D\C_Green> '
Expand Down
9 changes: 4 additions & 5 deletions IPython/config/profile/sympy/ipython_config.py
@@ -1,10 +1,9 @@
c = get_config()
app = c.IPythonApp
app = c.InteractiveShellApp

# This can be used at any point in a config file to load a sub config
# and merge it into the current one.
import os
load_subconfig(os.path.join('..','profile_default', 'ipython_config.py'))
load_subconfig('ipython_config.py', profile='default')

lines = """
from __future__ import division
Expand All @@ -25,7 +24,7 @@

# Load the sympy_printing extension to enable nice printing of sympy expr's.
if hasattr(app, 'extensions'):
app.extensions.append('sympy_printing')
app.extensions.append('sympyprinting')
else:
app.extensions = ['sympy_printing']
app.extensions = ['sympyprinting']

1 change: 0 additions & 1 deletion IPython/core/profileapp.py
Expand Up @@ -194,7 +194,6 @@ def init_config_files(self):
app.profile = self.profile
app.init_profile_dir()
app.init_config_files()
print 'tic'

def stage_default_config_file(self):
pass
Expand Down

0 comments on commit a7380bd

Please sign in to comment.