##// END OF EJS Templates
add --keep alias for history trim
Paul Ivanov -
Show More
@@ -1,119 +1,123 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 An application for managing IPython history.
3 An application for managing IPython history.
4
4
5 To be invoked as the `ipython history` subcommand.
5 To be invoked as the `ipython history` subcommand.
6 """
6 """
7 from __future__ import print_function
7 from __future__ import print_function
8
8
9 import os
9 import os
10 import sqlite3
10 import sqlite3
11
11
12 from IPython.config.application import Application
12 from IPython.config.application import Application
13 from IPython.core.application import BaseIPythonApplication
13 from IPython.core.application import BaseIPythonApplication
14 from IPython.utils.traitlets import Bool, Int, Dict
14 from IPython.utils.traitlets import Bool, Int, Dict
15
15
16 trim_hist_help = """Trim the IPython history database to the last 1000 entries.
16 trim_hist_help = """Trim the IPython history database to the last 1000 entries.
17
17
18 This actually copies the last 1000 entries to a new database, and then replaces
18 This actually copies the last 1000 entries to a new database, and then replaces
19 the old file with the new.
19 the old file with the new.
20 """
20 """
21
21
22 class HistoryTrim(BaseIPythonApplication):
22 class HistoryTrim(BaseIPythonApplication):
23 description = trim_hist_help
23 description = trim_hist_help
24
24
25 backup = Bool(False, config=True,
25 backup = Bool(False, config=True,
26 help="Keep the old history file as history.sqlite.<N>")
26 help="Keep the old history file as history.sqlite.<N>")
27
27
28 keep = Int(1000, config=True,
28 keep = Int(1000, config=True,
29 help="Number of recent lines to keep in the database.")
29 help="Number of recent lines to keep in the database.")
30
30
31 flags = Dict(dict(
31 flags = Dict(dict(
32 backup = ({'HistoryTrim' : {'backup' : True}},
32 backup = ({'HistoryTrim' : {'backup' : True}},
33 "Keep the old history file as history.sqlite.<N>"
33 backup.get_metadata('help')
34 )
34 )
35 ))
35 ))
36
37 aliases=Dict(dict(
38 keep = 'HistoryTrim.keep'
39 ))
36
40
37 def start(self):
41 def start(self):
38 profile_dir = self.profile_dir.location
42 profile_dir = self.profile_dir.location
39 hist_file = os.path.join(profile_dir, 'history.sqlite')
43 hist_file = os.path.join(profile_dir, 'history.sqlite')
40 con = sqlite3.connect(hist_file)
44 con = sqlite3.connect(hist_file)
41
45
42 # Grab the recent history from the current database.
46 # Grab the recent history from the current database.
43 inputs = list(con.execute('SELECT session, line, source, source_raw FROM '
47 inputs = list(con.execute('SELECT session, line, source, source_raw FROM '
44 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,)))
48 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,)))
45 if len(inputs) <= self.keep:
49 if len(inputs) <= self.keep:
46 print("There are already at most %d entries in the history database." % self.keep)
50 print("There are already at most %d entries in the history database." % self.keep)
47 print("Not doing anything.")
51 print("Not doing anything. Use --keep= argument to keep fewer entries")
48 return
52 return
49
53
50 print("Trimming history to the most recent %d entries." % self.keep)
54 print("Trimming history to the most recent %d entries." % self.keep)
51
55
52 inputs.pop() # Remove the extra element we got to check the length.
56 inputs.pop() # Remove the extra element we got to check the length.
53 inputs.reverse()
57 inputs.reverse()
54 first_session = inputs[0][0]
58 first_session = inputs[0][0]
55 outputs = list(con.execute('SELECT session, line, output FROM '
59 outputs = list(con.execute('SELECT session, line, output FROM '
56 'output_history WHERE session >= ?', (first_session,)))
60 'output_history WHERE session >= ?', (first_session,)))
57 sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM '
61 sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM '
58 'sessions WHERE session >= ?', (first_session,)))
62 'sessions WHERE session >= ?', (first_session,)))
59 con.close()
63 con.close()
60
64
61 # Create the new history database.
65 # Create the new history database.
62 new_hist_file = os.path.join(profile_dir, 'history.sqlite.new')
66 new_hist_file = os.path.join(profile_dir, 'history.sqlite.new')
63 i = 0
67 i = 0
64 while os.path.exists(new_hist_file):
68 while os.path.exists(new_hist_file):
65 # Make sure we don't interfere with an existing file.
69 # Make sure we don't interfere with an existing file.
66 i += 1
70 i += 1
67 new_hist_file = os.path.join(profile_dir, 'history.sqlite.new'+str(i))
71 new_hist_file = os.path.join(profile_dir, 'history.sqlite.new'+str(i))
68 new_db = sqlite3.connect(new_hist_file)
72 new_db = sqlite3.connect(new_hist_file)
69 new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
73 new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
70 primary key autoincrement, start timestamp,
74 primary key autoincrement, start timestamp,
71 end timestamp, num_cmds integer, remark text)""")
75 end timestamp, num_cmds integer, remark text)""")
72 new_db.execute("""CREATE TABLE IF NOT EXISTS history
76 new_db.execute("""CREATE TABLE IF NOT EXISTS history
73 (session integer, line integer, source text, source_raw text,
77 (session integer, line integer, source text, source_raw text,
74 PRIMARY KEY (session, line))""")
78 PRIMARY KEY (session, line))""")
75 new_db.execute("""CREATE TABLE IF NOT EXISTS output_history
79 new_db.execute("""CREATE TABLE IF NOT EXISTS output_history
76 (session integer, line integer, output text,
80 (session integer, line integer, output text,
77 PRIMARY KEY (session, line))""")
81 PRIMARY KEY (session, line))""")
78 new_db.commit()
82 new_db.commit()
79
83
80
84
81 with new_db:
85 with new_db:
82 # Add the recent history into the new database.
86 # Add the recent history into the new database.
83 new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions)
87 new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions)
84 new_db.executemany('insert into history values (?,?,?,?)', inputs)
88 new_db.executemany('insert into history values (?,?,?,?)', inputs)
85 new_db.executemany('insert into output_history values (?,?,?)', outputs)
89 new_db.executemany('insert into output_history values (?,?,?)', outputs)
86 new_db.close()
90 new_db.close()
87
91
88 if self.backup:
92 if self.backup:
89 i = 1
93 i = 1
90 backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i)
94 backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i)
91 while os.path.exists(backup_hist_file):
95 while os.path.exists(backup_hist_file):
92 i += 1
96 i += 1
93 backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i)
97 backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i)
94 os.rename(hist_file, backup_hist_file)
98 os.rename(hist_file, backup_hist_file)
95 print("Backed up longer history file to", backup_hist_file)
99 print("Backed up longer history file to", backup_hist_file)
96 else:
100 else:
97 os.remove(hist_file)
101 os.remove(hist_file)
98
102
99 os.rename(new_hist_file, hist_file)
103 os.rename(new_hist_file, hist_file)
100
104
101
105
102 class HistoryApp(Application):
106 class HistoryApp(Application):
103 name = u'ipython-history'
107 name = u'ipython-history'
104 description = "Manage the IPython history database."
108 description = "Manage the IPython history database."
105
109
106 subcommands = Dict(dict(
110 subcommands = Dict(dict(
107 trim = (HistoryTrim, HistoryTrim.description.splitlines()[0]),
111 trim = (HistoryTrim, HistoryTrim.description.splitlines()[0]),
108 ))
112 ))
109
113
110 def start(self):
114 def start(self):
111 if self.subapp is None:
115 if self.subapp is None:
112 print("No subcommand specified. Must specify one of: %s" % \
116 print("No subcommand specified. Must specify one of: %s" % \
113 (self.subcommands.keys()))
117 (self.subcommands.keys()))
114 print()
118 print()
115 self.print_description()
119 self.print_description()
116 self.print_subcommands()
120 self.print_subcommands()
117 self.exit(1)
121 self.exit(1)
118 else:
122 else:
119 return self.subapp.start()
123 return self.subapp.start()
General Comments 0
You need to be logged in to leave comments. Login now