##// END OF EJS Templates
Merge pull request #4542 from ivanov/history-clear...
Min RK -
r13694:24e10561 merge
parent child Browse files
Show More
@@ -0,0 +1,1 b''
1 * bash completion updated with support for all ipython subcommands and flags, including nbconvert
@@ -0,0 +1,4 b''
1 * ``ipython history trim``: added ``--keep=<N>`` as an alias for the more verbose
2 ``--HistoryTrim.keep=<N>``
3 * new ``ipython history clear`` subcommand, which is the same as the newly supported
4 ``ipython history trim --keep=0``
@@ -12,13 +12,25 b' import sqlite3'
12 12 from IPython.config.application import Application
13 13 from IPython.core.application import BaseIPythonApplication
14 14 from IPython.utils.traitlets import Bool, Int, Dict
15 from IPython.utils.io import ask_yes_no
15 16
16 17 trim_hist_help = """Trim the IPython history database to the last 1000 entries.
17 18
18 19 This actually copies the last 1000 entries to a new database, and then replaces
19 the old file with the new.
20 the old file with the new. Use the `--keep=` argument to specify a number
21 other than 1000.
20 22 """
21 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
22 34 class HistoryTrim(BaseIPythonApplication):
23 35 description = trim_hist_help
24 36
@@ -30,9 +42,13 b' class HistoryTrim(BaseIPythonApplication):'
30 42
31 43 flags = Dict(dict(
32 44 backup = ({'HistoryTrim' : {'backup' : True}},
33 "Set Application.log_level to 0, maximizing log output."
45 backup.get_metadata('help')
34 46 )
35 47 ))
48
49 aliases=Dict(dict(
50 keep = 'HistoryTrim.keep'
51 ))
36 52
37 53 def start(self):
38 54 profile_dir = self.profile_dir.location
@@ -44,18 +60,19 b' class HistoryTrim(BaseIPythonApplication):'
44 60 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,)))
45 61 if len(inputs) <= self.keep:
46 62 print("There are already at most %d entries in the history database." % self.keep)
47 print("Not doing anything.")
63 print("Not doing anything. Use --keep= argument to keep fewer entries")
48 64 return
49 65
50 66 print("Trimming history to the most recent %d entries." % self.keep)
51 67
52 68 inputs.pop() # Remove the extra element we got to check the length.
53 69 inputs.reverse()
54 first_session = inputs[0][0]
55 outputs = list(con.execute('SELECT session, line, output FROM '
56 'output_history WHERE session >= ?', (first_session,)))
57 sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM '
58 'sessions WHERE session >= ?', (first_session,)))
70 if inputs:
71 first_session = inputs[0][0]
72 outputs = list(con.execute('SELECT session, line, output FROM '
73 'output_history WHERE session >= ?', (first_session,)))
74 sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM '
75 'sessions WHERE session >= ?', (first_session,)))
59 76 con.close()
60 77
61 78 # Create the new history database.
@@ -78,11 +95,12 b' class HistoryTrim(BaseIPythonApplication):'
78 95 new_db.commit()
79 96
80 97
81 with new_db:
82 # Add the recent history into the new database.
83 new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions)
84 new_db.executemany('insert into history values (?,?,?,?)', inputs)
85 new_db.executemany('insert into output_history values (?,?,?)', outputs)
98 if inputs:
99 with new_db:
100 # Add the recent history into the new database.
101 new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions)
102 new_db.executemany('insert into history values (?,?,?,?)', inputs)
103 new_db.executemany('insert into output_history values (?,?,?)', outputs)
86 104 new_db.close()
87 105
88 106 if self.backup:
@@ -98,6 +116,27 b' class HistoryTrim(BaseIPythonApplication):'
98 116
99 117 os.rename(new_hist_file, hist_file)
100 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)
101 140
102 141 class HistoryApp(Application):
103 142 name = u'ipython-history'
@@ -105,6 +144,7 b' class HistoryApp(Application):'
105 144
106 145 subcommands = Dict(dict(
107 146 trim = (HistoryTrim, HistoryTrim.description.splitlines()[0]),
147 clear = (HistoryClear, HistoryClear.description.splitlines()[0]),
108 148 ))
109 149
110 150 def start(self):
@@ -155,11 +155,13 b' class Tee(object):'
155 155 self.close()
156 156
157 157
158 def ask_yes_no(prompt,default=None):
158 def ask_yes_no(prompt, default=None, interrupt=None):
159 159 """Asks a question and returns a boolean (y/n) answer.
160 160
161 161 If default is given (one of 'y','n'), it is used if the user input is
162 empty. Otherwise the question is repeated until an answer is given.
162 empty. If interrupt is given (one of 'y','n'), it is used if the user
163 presses Ctrl-C. Otherwise the question is repeated until an answer is
164 given.
163 165
164 166 An EOF is treated as the default answer. If there is no default, an
165 167 exception is raised to prevent infinite loops.
@@ -174,7 +176,8 b' def ask_yes_no(prompt,default=None):'
174 176 if not ans: # response was an empty string
175 177 ans = default
176 178 except KeyboardInterrupt:
177 pass
179 if interrupt:
180 ans = interrupt
178 181 except EOFError:
179 182 if default in answers.keys():
180 183 ans = default
@@ -24,7 +24,7 b' _ipython()'
24 24 {
25 25 local cur=${COMP_WORDS[COMP_CWORD]}
26 26 local prev=${COMP_WORDS[COMP_CWORD - 1]}
27 local subcommands="notebook qtconsole console kernel profile locate history nbconvert"
27 local subcommands="notebook qtconsole console kernel profile locate history nbconvert "
28 28 local opts=""
29 29 if [ -z "$__ipython_complete_baseopts" ]; then
30 30 _ipython_get_flags baseopts
@@ -46,12 +46,21 b' _ipython()'
46 46
47 47 if [[ ${cur} == -* ]]; then
48 48 case $mode in
49 "notebook" | "qtconsole" | "console" | "kernel")
49 "notebook" | "qtconsole" | "console" | "kernel" | "nbconvert")
50 50 _ipython_get_flags $mode
51 51 opts=$"${opts} ${baseopts}"
52 52 ;;
53 "locate" | "history" | "profile")
53 "locate" | "profile")
54 54 _ipython_get_flags $mode
55 ;;
56 "history")
57 if [[ $COMP_CWORD -ge 3 ]]; then
58 # 'history trim' and 'history clear' covered by next line
59 _ipython_get_flags history\ "${COMP_WORDS[2]}"
60 else
61 _ipython_get_flags $mode
62
63 fi
55 64 opts=$"${opts}"
56 65 ;;
57 66 *)
@@ -62,10 +71,17 b' _ipython()'
62 71 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
63 72 return 0
64 73 elif [[ $mode == "profile" ]]; then
65 opts="list create locate"
74 opts="list create locate "
75 local IFS=$'\t\n'
66 76 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
67 77 elif [[ $mode == "history" ]]; then
68 opts="trim"
78 if [[ $COMP_CWORD -ge 3 ]]; then
79 # drop into flags
80 opts="--"
81 else
82 opts="trim clear "
83 fi
84 local IFS=$'\t\n'
69 85 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
70 86 elif [[ $mode == "locate" ]]; then
71 87 opts="profile"
General Comments 0
You need to be logged in to leave comments. Login now