Show More
@@ -1,126 +1,159 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 | from IPython.utils.io import ask_yes_no | |||
15 |
|
16 | |||
16 | trim_hist_help = """Trim the IPython history database to the last 1000 entries. |
|
17 | trim_hist_help = """Trim the IPython history database to the last 1000 entries. | |
17 |
|
18 | |||
18 | This actually copies the last 1000 entries to a new database, and then replaces |
|
19 | This actually copies the last 1000 entries to a new database, and then replaces | |
19 | the old file with the new. Use the `--keep=` argument to specify a number |
|
20 | the old file with the new. Use the `--keep=` argument to specify a number | |
20 | other than 1000. |
|
21 | other than 1000. | |
21 | """ |
|
22 | """ | |
22 |
|
23 | |||
|
24 | clear_hist_help = """Clear the IPython history database, deleting all entries. | |||
|
25 | ||||
|
26 | Because this is a destructive operation, IPython will prompt the user if they | |||
|
27 | really want to do this. Passing a `-f` flag will force clearing without a | |||
|
28 | prompt. | |||
|
29 | ||||
|
30 | This is an handy alias to `ipython history trim --keep=0` | |||
|
31 | """ | |||
|
32 | ||||
|
33 | ||||
23 | class HistoryTrim(BaseIPythonApplication): |
|
34 | class HistoryTrim(BaseIPythonApplication): | |
24 | description = trim_hist_help |
|
35 | description = trim_hist_help | |
25 |
|
36 | |||
26 | backup = Bool(False, config=True, |
|
37 | backup = Bool(False, config=True, | |
27 | help="Keep the old history file as history.sqlite.<N>") |
|
38 | help="Keep the old history file as history.sqlite.<N>") | |
28 |
|
39 | |||
29 | keep = Int(1000, config=True, |
|
40 | keep = Int(1000, config=True, | |
30 | help="Number of recent lines to keep in the database.") |
|
41 | help="Number of recent lines to keep in the database.") | |
31 |
|
42 | |||
32 | flags = Dict(dict( |
|
43 | flags = Dict(dict( | |
33 | backup = ({'HistoryTrim' : {'backup' : True}}, |
|
44 | backup = ({'HistoryTrim' : {'backup' : True}}, | |
34 | backup.get_metadata('help') |
|
45 | backup.get_metadata('help') | |
35 | ) |
|
46 | ) | |
36 | )) |
|
47 | )) | |
37 |
|
48 | |||
38 | aliases=Dict(dict( |
|
49 | aliases=Dict(dict( | |
39 | keep = 'HistoryTrim.keep' |
|
50 | keep = 'HistoryTrim.keep' | |
40 | )) |
|
51 | )) | |
41 |
|
52 | |||
42 | def start(self): |
|
53 | def start(self): | |
43 | profile_dir = self.profile_dir.location |
|
54 | profile_dir = self.profile_dir.location | |
44 | hist_file = os.path.join(profile_dir, 'history.sqlite') |
|
55 | hist_file = os.path.join(profile_dir, 'history.sqlite') | |
45 | con = sqlite3.connect(hist_file) |
|
56 | con = sqlite3.connect(hist_file) | |
46 |
|
57 | |||
47 | # Grab the recent history from the current database. |
|
58 | # Grab the recent history from the current database. | |
48 | inputs = list(con.execute('SELECT session, line, source, source_raw FROM ' |
|
59 | inputs = list(con.execute('SELECT session, line, source, source_raw FROM ' | |
49 | 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,))) |
|
60 | 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,))) | |
50 | if len(inputs) <= self.keep: |
|
61 | if len(inputs) <= self.keep: | |
51 | print("There are already at most %d entries in the history database." % self.keep) |
|
62 | print("There are already at most %d entries in the history database." % self.keep) | |
52 | print("Not doing anything. Use --keep= argument to keep fewer entries") |
|
63 | print("Not doing anything. Use --keep= argument to keep fewer entries") | |
53 | return |
|
64 | return | |
54 |
|
65 | |||
55 | print("Trimming history to the most recent %d entries." % self.keep) |
|
66 | print("Trimming history to the most recent %d entries." % self.keep) | |
56 |
|
67 | |||
57 | inputs.pop() # Remove the extra element we got to check the length. |
|
68 | inputs.pop() # Remove the extra element we got to check the length. | |
58 | inputs.reverse() |
|
69 | inputs.reverse() | |
59 | if inputs: |
|
70 | if inputs: | |
60 | first_session = inputs[0][0] |
|
71 | first_session = inputs[0][0] | |
61 | outputs = list(con.execute('SELECT session, line, output FROM ' |
|
72 | outputs = list(con.execute('SELECT session, line, output FROM ' | |
62 | 'output_history WHERE session >= ?', (first_session,))) |
|
73 | 'output_history WHERE session >= ?', (first_session,))) | |
63 | sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM ' |
|
74 | sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM ' | |
64 | 'sessions WHERE session >= ?', (first_session,))) |
|
75 | 'sessions WHERE session >= ?', (first_session,))) | |
65 | con.close() |
|
76 | con.close() | |
66 |
|
77 | |||
67 | # Create the new history database. |
|
78 | # Create the new history database. | |
68 | new_hist_file = os.path.join(profile_dir, 'history.sqlite.new') |
|
79 | new_hist_file = os.path.join(profile_dir, 'history.sqlite.new') | |
69 | i = 0 |
|
80 | i = 0 | |
70 | while os.path.exists(new_hist_file): |
|
81 | while os.path.exists(new_hist_file): | |
71 | # Make sure we don't interfere with an existing file. |
|
82 | # Make sure we don't interfere with an existing file. | |
72 | i += 1 |
|
83 | i += 1 | |
73 | new_hist_file = os.path.join(profile_dir, 'history.sqlite.new'+str(i)) |
|
84 | new_hist_file = os.path.join(profile_dir, 'history.sqlite.new'+str(i)) | |
74 | new_db = sqlite3.connect(new_hist_file) |
|
85 | new_db = sqlite3.connect(new_hist_file) | |
75 | new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer |
|
86 | new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer | |
76 | primary key autoincrement, start timestamp, |
|
87 | primary key autoincrement, start timestamp, | |
77 | end timestamp, num_cmds integer, remark text)""") |
|
88 | end timestamp, num_cmds integer, remark text)""") | |
78 | new_db.execute("""CREATE TABLE IF NOT EXISTS history |
|
89 | new_db.execute("""CREATE TABLE IF NOT EXISTS history | |
79 | (session integer, line integer, source text, source_raw text, |
|
90 | (session integer, line integer, source text, source_raw text, | |
80 | PRIMARY KEY (session, line))""") |
|
91 | PRIMARY KEY (session, line))""") | |
81 | new_db.execute("""CREATE TABLE IF NOT EXISTS output_history |
|
92 | new_db.execute("""CREATE TABLE IF NOT EXISTS output_history | |
82 | (session integer, line integer, output text, |
|
93 | (session integer, line integer, output text, | |
83 | PRIMARY KEY (session, line))""") |
|
94 | PRIMARY KEY (session, line))""") | |
84 | new_db.commit() |
|
95 | new_db.commit() | |
85 |
|
96 | |||
86 |
|
97 | |||
87 | if inputs: |
|
98 | if inputs: | |
88 | with new_db: |
|
99 | with new_db: | |
89 | # Add the recent history into the new database. |
|
100 | # Add the recent history into the new database. | |
90 | new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions) |
|
101 | new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions) | |
91 | new_db.executemany('insert into history values (?,?,?,?)', inputs) |
|
102 | new_db.executemany('insert into history values (?,?,?,?)', inputs) | |
92 | new_db.executemany('insert into output_history values (?,?,?)', outputs) |
|
103 | new_db.executemany('insert into output_history values (?,?,?)', outputs) | |
93 | new_db.close() |
|
104 | new_db.close() | |
94 |
|
105 | |||
95 | if self.backup: |
|
106 | if self.backup: | |
96 | i = 1 |
|
107 | i = 1 | |
97 | backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i) |
|
108 | backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i) | |
98 | while os.path.exists(backup_hist_file): |
|
109 | while os.path.exists(backup_hist_file): | |
99 | i += 1 |
|
110 | i += 1 | |
100 | backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i) |
|
111 | backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i) | |
101 | os.rename(hist_file, backup_hist_file) |
|
112 | os.rename(hist_file, backup_hist_file) | |
102 | print("Backed up longer history file to", backup_hist_file) |
|
113 | print("Backed up longer history file to", backup_hist_file) | |
103 | else: |
|
114 | else: | |
104 | os.remove(hist_file) |
|
115 | os.remove(hist_file) | |
105 |
|
116 | |||
106 | os.rename(new_hist_file, hist_file) |
|
117 | os.rename(new_hist_file, hist_file) | |
107 |
|
118 | |||
|
119 | class HistoryClear(HistoryTrim): | |||
|
120 | description = clear_hist_help | |||
|
121 | keep = Int(0, config=False, | |||
|
122 | help="Number of recent lines to keep in the database.") | |||
|
123 | ||||
|
124 | force = Bool(False, config=True, | |||
|
125 | help="Don't prompt user for confirmation") | |||
|
126 | ||||
|
127 | flags = Dict(dict( | |||
|
128 | force = ({'HistoryClear' : {'force' : True}}, | |||
|
129 | force.get_metadata('help')), | |||
|
130 | f = ({'HistoryTrim' : {'force' : True}}, | |||
|
131 | force.get_metadata('help') | |||
|
132 | ) | |||
|
133 | )) | |||
|
134 | aliases = Dict() | |||
|
135 | ||||
|
136 | def start(self): | |||
|
137 | if self.force or ask_yes_no("Really delete all ipython history? ", | |||
|
138 | default="no", interrupt="no"): | |||
|
139 | HistoryTrim.start(self) | |||
108 |
|
140 | |||
109 | class HistoryApp(Application): |
|
141 | class HistoryApp(Application): | |
110 | name = u'ipython-history' |
|
142 | name = u'ipython-history' | |
111 | description = "Manage the IPython history database." |
|
143 | description = "Manage the IPython history database." | |
112 |
|
144 | |||
113 | subcommands = Dict(dict( |
|
145 | subcommands = Dict(dict( | |
114 | trim = (HistoryTrim, HistoryTrim.description.splitlines()[0]), |
|
146 | trim = (HistoryTrim, HistoryTrim.description.splitlines()[0]), | |
|
147 | clear = (HistoryClear, HistoryClear.description.splitlines()[0]), | |||
115 | )) |
|
148 | )) | |
116 |
|
149 | |||
117 | def start(self): |
|
150 | def start(self): | |
118 | if self.subapp is None: |
|
151 | if self.subapp is None: | |
119 | print("No subcommand specified. Must specify one of: %s" % \ |
|
152 | print("No subcommand specified. Must specify one of: %s" % \ | |
120 | (self.subcommands.keys())) |
|
153 | (self.subcommands.keys())) | |
121 | print() |
|
154 | print() | |
122 | self.print_description() |
|
155 | self.print_description() | |
123 | self.print_subcommands() |
|
156 | self.print_subcommands() | |
124 | self.exit(1) |
|
157 | self.exit(1) | |
125 | else: |
|
158 | else: | |
126 | return self.subapp.start() |
|
159 | return self.subapp.start() |
General Comments 0
You need to be logged in to leave comments.
Login now