##// 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 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.
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 class HistoryTrim(BaseIPythonApplication):
34 class HistoryTrim(BaseIPythonApplication):
23 description = trim_hist_help
35 description = trim_hist_help
24
36
@@ -30,9 +42,13 b' class HistoryTrim(BaseIPythonApplication):'
30
42
31 flags = Dict(dict(
43 flags = Dict(dict(
32 backup = ({'HistoryTrim' : {'backup' : True}},
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 def start(self):
53 def start(self):
38 profile_dir = self.profile_dir.location
54 profile_dir = self.profile_dir.location
@@ -44,18 +60,19 b' class HistoryTrim(BaseIPythonApplication):'
44 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,)))
60 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,)))
45 if len(inputs) <= self.keep:
61 if len(inputs) <= self.keep:
46 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)
47 print("Not doing anything.")
63 print("Not doing anything. Use --keep= argument to keep fewer entries")
48 return
64 return
49
65
50 print("Trimming history to the most recent %d entries." % self.keep)
66 print("Trimming history to the most recent %d entries." % self.keep)
51
67
52 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.
53 inputs.reverse()
69 inputs.reverse()
54 first_session = inputs[0][0]
70 if inputs:
55 outputs = list(con.execute('SELECT session, line, output FROM '
71 first_session = inputs[0][0]
56 'output_history WHERE session >= ?', (first_session,)))
72 outputs = list(con.execute('SELECT session, line, output FROM '
57 sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM '
73 'output_history WHERE session >= ?', (first_session,)))
58 'sessions WHERE session >= ?', (first_session,)))
74 sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM '
75 'sessions WHERE session >= ?', (first_session,)))
59 con.close()
76 con.close()
60
77
61 # Create the new history database.
78 # Create the new history database.
@@ -78,11 +95,12 b' class HistoryTrim(BaseIPythonApplication):'
78 new_db.commit()
95 new_db.commit()
79
96
80
97
81 with new_db:
98 if inputs:
82 # Add the recent history into the new database.
99 with new_db:
83 new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions)
100 # Add the recent history into the new database.
84 new_db.executemany('insert into history values (?,?,?,?)', inputs)
101 new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions)
85 new_db.executemany('insert into output_history values (?,?,?)', outputs)
102 new_db.executemany('insert into history values (?,?,?,?)', inputs)
103 new_db.executemany('insert into output_history values (?,?,?)', outputs)
86 new_db.close()
104 new_db.close()
87
105
88 if self.backup:
106 if self.backup:
@@ -98,6 +116,27 b' class HistoryTrim(BaseIPythonApplication):'
98
116
99 os.rename(new_hist_file, hist_file)
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 class HistoryApp(Application):
141 class HistoryApp(Application):
103 name = u'ipython-history'
142 name = u'ipython-history'
@@ -105,6 +144,7 b' class HistoryApp(Application):'
105
144
106 subcommands = Dict(dict(
145 subcommands = Dict(dict(
107 trim = (HistoryTrim, HistoryTrim.description.splitlines()[0]),
146 trim = (HistoryTrim, HistoryTrim.description.splitlines()[0]),
147 clear = (HistoryClear, HistoryClear.description.splitlines()[0]),
108 ))
148 ))
109
149
110 def start(self):
150 def start(self):
@@ -155,11 +155,13 b' class Tee(object):'
155 self.close()
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 """Asks a question and returns a boolean (y/n) answer.
159 """Asks a question and returns a boolean (y/n) answer.
160
160
161 If default is given (one of 'y','n'), it is used if the user input is
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 An EOF is treated as the default answer. If there is no default, an
166 An EOF is treated as the default answer. If there is no default, an
165 exception is raised to prevent infinite loops.
167 exception is raised to prevent infinite loops.
@@ -174,7 +176,8 b' def ask_yes_no(prompt,default=None):'
174 if not ans: # response was an empty string
176 if not ans: # response was an empty string
175 ans = default
177 ans = default
176 except KeyboardInterrupt:
178 except KeyboardInterrupt:
177 pass
179 if interrupt:
180 ans = interrupt
178 except EOFError:
181 except EOFError:
179 if default in answers.keys():
182 if default in answers.keys():
180 ans = default
183 ans = default
@@ -24,7 +24,7 b' _ipython()'
24 {
24 {
25 local cur=${COMP_WORDS[COMP_CWORD]}
25 local cur=${COMP_WORDS[COMP_CWORD]}
26 local prev=${COMP_WORDS[COMP_CWORD - 1]}
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 local opts=""
28 local opts=""
29 if [ -z "$__ipython_complete_baseopts" ]; then
29 if [ -z "$__ipython_complete_baseopts" ]; then
30 _ipython_get_flags baseopts
30 _ipython_get_flags baseopts
@@ -46,12 +46,21 b' _ipython()'
46
46
47 if [[ ${cur} == -* ]]; then
47 if [[ ${cur} == -* ]]; then
48 case $mode in
48 case $mode in
49 "notebook" | "qtconsole" | "console" | "kernel")
49 "notebook" | "qtconsole" | "console" | "kernel" | "nbconvert")
50 _ipython_get_flags $mode
50 _ipython_get_flags $mode
51 opts=$"${opts} ${baseopts}"
51 opts=$"${opts} ${baseopts}"
52 ;;
52 ;;
53 "locate" | "history" | "profile")
53 "locate" | "profile")
54 _ipython_get_flags $mode
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 opts=$"${opts}"
64 opts=$"${opts}"
56 ;;
65 ;;
57 *)
66 *)
@@ -62,10 +71,17 b' _ipython()'
62 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
71 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
63 return 0
72 return 0
64 elif [[ $mode == "profile" ]]; then
73 elif [[ $mode == "profile" ]]; then
65 opts="list create locate"
74 opts="list create locate "
75 local IFS=$'\t\n'
66 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
76 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
67 elif [[ $mode == "history" ]]; then
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 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
85 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
70 elif [[ $mode == "locate" ]]; then
86 elif [[ $mode == "locate" ]]; then
71 opts="profile"
87 opts="profile"
General Comments 0
You need to be logged in to leave comments. Login now