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