##// END OF EJS Templates
HistoryAccessor can automatically find a history file for a standard profile.
Thomas Kluyver -
Show More
@@ -25,6 +25,7 b' from IPython.config.configurable import Configurable'
25 25
26 26 from IPython.testing.skipdoctest import skip_doctest
27 27 from IPython.utils import io
28 from IPython.utils.path import locate_profile
28 29 from IPython.utils.traitlets import Bool, Dict, Instance, Int, CInt, List, Unicode
29 30 from IPython.utils.warn import warn
30 31
@@ -43,10 +44,20 b' class HistoryAccessor(Configurable):'
43 44 # The SQLite database
44 45 db = Instance(sqlite3.Connection)
45 46
46 def __init__(self, hist_file=u'', shell=None, config=None, **traits):
47 def __init__(self, profile='default', hist_file=u'', shell=None, config=None, **traits):
47 48 """Create a new history accessor.
48 49
49 hist_file must be given, either as an argument or through config.
50 Parameters
51 ----------
52 profile : str
53 The name of the profile from which to open history.
54 hist_file : str
55 Path to an SQLite history database stored by IPython. If specified,
56 hist_file overrides profile.
57 shell :
58 InteractiveShell object, for use by HistoryManager subclass
59 config :
60 Config object. hist_file can also be set through this.
50 61 """
51 62 # We need a pointer back to the shell for various tasks.
52 63 super(HistoryAccessor, self).__init__(shell=shell, config=config,
@@ -54,7 +65,7 b' class HistoryAccessor(Configurable):'
54 65
55 66 if self.hist_file == u'':
56 67 # No one has set the hist_file, yet.
57 self.hist_file = self._get_hist_file_name(hist_file)
68 self.hist_file = self._get_hist_file_name(profile)
58 69
59 70 try:
60 71 self.init_db()
@@ -70,9 +81,18 b' class HistoryAccessor(Configurable):'
70 81 # The hist_file is probably :memory: or something else.
71 82 raise
72 83
73 def _get_hist_file_name(self, hist_file=None):
74 "Override to produce a default history file name."
75 raise NotImplementedError("No default history file")
84 def _get_hist_file_name(self, profile='default'):
85 """Find the history file for the given profile name.
86
87 This is overridden by the HistoryManager subclass, to use the shell's
88 active profile.
89
90 Parameters
91 ----------
92 profile : str
93 The name of a profile which has a history file.
94 """
95 return os.path.join(locate_profile(profile), 'history.sqlite')
76 96
77 97 def init_db(self):
78 98 """Connect to the database, and create tables if necessary."""
@@ -332,7 +352,11 b' class HistoryManager(HistoryAccessor):'
332 352
333 353 self.new_session()
334 354
335 def _get_hist_file_name(self, hist_file=None):
355 def _get_hist_file_name(self, profile=None):
356 """Get default history file name based on the Shell's profile.
357
358 The profile parameter is ignored, but must exist for compatibility with
359 the parent class."""
336 360 profile_dir = self.shell.profile_dir.location
337 361 return os.path.join(profile_dir, 'history.sqlite')
338 362
@@ -372,6 +372,18 b' def get_ipython_module_path(module_str):'
372 372 the_path = the_path.replace('.pyo', '.py')
373 373 return py3compat.cast_unicode(the_path, fs_encoding)
374 374
375 def locate_profile(profile='default'):
376 """Find the path to the folder associated with a given profile.
377
378 I.e. find $IPYTHON_DIR/profile_whatever.
379 """
380 from IPython.core.profiledir import ProfileDir, ProfileDirError
381 try:
382 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
383 except ProfileDirError:
384 # IOError makes more sense when people are expecting a path
385 raise IOError("Couldn't find profile %r" % profile)
386 return pd.location
375 387
376 388 def expand_path(s):
377 389 """Expand $VARS and ~names in a string, like a shell
General Comments 0
You need to be logged in to leave comments. Login now