Show More
@@ -1,161 +1,156 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 | |
|
8 | 8 | import sqlite3 |
|
9 | 9 | from pathlib import Path |
|
10 | 10 | |
|
11 | 11 | from traitlets.config.application import Application |
|
12 | 12 | from .application import BaseIPythonApplication |
|
13 | 13 | from traitlets import Bool, Int, Dict |
|
14 | 14 | from ..utils.io import ask_yes_no |
|
15 | 15 | |
|
16 | 16 | trim_hist_help = """Trim the IPython history database to the last 1000 entries. |
|
17 | 17 | |
|
18 | 18 | This actually copies the last 1000 entries to a new database, and then replaces |
|
19 | 19 | the old file with the new. Use the `--keep=` argument to specify a number |
|
20 | 20 | other than 1000. |
|
21 | 21 | """ |
|
22 | 22 | |
|
23 | 23 | clear_hist_help = """Clear the IPython history database, deleting all entries. |
|
24 | 24 | |
|
25 | 25 | Because this is a destructive operation, IPython will prompt the user if they |
|
26 | 26 | really want to do this. Passing a `-f` flag will force clearing without a |
|
27 | 27 | prompt. |
|
28 | 28 | |
|
29 | 29 | This is an handy alias to `ipython history trim --keep=0` |
|
30 | 30 | """ |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class HistoryTrim(BaseIPythonApplication): |
|
34 | 34 | description = trim_hist_help |
|
35 | 35 | |
|
36 | backup = Bool(False, | |
|
37 | help="Keep the old history file as history.sqlite.<N>" | |
|
38 | ).tag(config=True) | |
|
36 | backup = Bool(False, help="Keep the old history file as history.sqlite.<N>").tag( | |
|
37 | config=True | |
|
38 | ) | |
|
39 | 39 | |
|
40 | keep = Int(1000, | |
|
41 | help="Number of recent lines to keep in the database." | |
|
42 | ).tag(config=True) | |
|
40 | keep = Int(1000, help="Number of recent lines to keep in the database.").tag( | |
|
41 | config=True | |
|
42 | ) | |
|
43 | 43 | |
|
44 |
flags = Dict( |
|
|
45 |
backup |
|
|
46 | backup.help | |
|
44 | flags = Dict( # type: ignore | |
|
45 | dict(backup=({"HistoryTrim": {"backup": True}}, backup.help)) | |
|
47 | 46 |
|
|
48 | )) | |
|
49 | 47 | |
|
50 | aliases=Dict(dict( | |
|
51 | keep = 'HistoryTrim.keep' | |
|
52 | )) | |
|
48 | aliases = Dict(dict(keep="HistoryTrim.keep")) # type: ignore | |
|
53 | 49 | |
|
54 | 50 | def start(self): |
|
55 | 51 | profile_dir = Path(self.profile_dir.location) |
|
56 | 52 | hist_file = profile_dir / "history.sqlite" |
|
57 | 53 | con = sqlite3.connect(hist_file) |
|
58 | 54 | |
|
59 | 55 | # Grab the recent history from the current database. |
|
60 | 56 | inputs = list(con.execute('SELECT session, line, source, source_raw FROM ' |
|
61 | 57 | 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,))) |
|
62 | 58 | if len(inputs) <= self.keep: |
|
63 | 59 | print("There are already at most %d entries in the history database." % self.keep) |
|
64 | 60 | print("Not doing anything. Use --keep= argument to keep fewer entries") |
|
65 | 61 | return |
|
66 | 62 | |
|
67 | 63 | print("Trimming history to the most recent %d entries." % self.keep) |
|
68 | 64 | |
|
69 | 65 | inputs.pop() # Remove the extra element we got to check the length. |
|
70 | 66 | inputs.reverse() |
|
71 | 67 | if inputs: |
|
72 | 68 | first_session = inputs[0][0] |
|
73 | 69 | outputs = list(con.execute('SELECT session, line, output FROM ' |
|
74 | 70 | 'output_history WHERE session >= ?', (first_session,))) |
|
75 | 71 | sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM ' |
|
76 | 72 | 'sessions WHERE session >= ?', (first_session,))) |
|
77 | 73 | con.close() |
|
78 | 74 | |
|
79 | 75 | # Create the new history database. |
|
80 | 76 | new_hist_file = profile_dir / "history.sqlite.new" |
|
81 | 77 | i = 0 |
|
82 | 78 | while new_hist_file.exists(): |
|
83 | 79 | # Make sure we don't interfere with an existing file. |
|
84 | 80 | i += 1 |
|
85 | 81 | new_hist_file = profile_dir / ("history.sqlite.new" + str(i)) |
|
86 | 82 | new_db = sqlite3.connect(new_hist_file) |
|
87 | 83 | new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer |
|
88 | 84 | primary key autoincrement, start timestamp, |
|
89 | 85 | end timestamp, num_cmds integer, remark text)""") |
|
90 | 86 | new_db.execute("""CREATE TABLE IF NOT EXISTS history |
|
91 | 87 | (session integer, line integer, source text, source_raw text, |
|
92 | 88 | PRIMARY KEY (session, line))""") |
|
93 | 89 | new_db.execute("""CREATE TABLE IF NOT EXISTS output_history |
|
94 | 90 | (session integer, line integer, output text, |
|
95 | 91 | PRIMARY KEY (session, line))""") |
|
96 | 92 | new_db.commit() |
|
97 | 93 | |
|
98 | 94 | |
|
99 | 95 | if inputs: |
|
100 | 96 | with new_db: |
|
101 | 97 | # Add the recent history into the new database. |
|
102 | 98 | new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions) |
|
103 | 99 | new_db.executemany('insert into history values (?,?,?,?)', inputs) |
|
104 | 100 | new_db.executemany('insert into output_history values (?,?,?)', outputs) |
|
105 | 101 | new_db.close() |
|
106 | 102 | |
|
107 | 103 | if self.backup: |
|
108 | 104 | i = 1 |
|
109 | 105 | backup_hist_file = profile_dir / ("history.sqlite.old.%d" % i) |
|
110 | 106 | while backup_hist_file.exists(): |
|
111 | 107 | i += 1 |
|
112 | 108 | backup_hist_file = profile_dir / ("history.sqlite.old.%d" % i) |
|
113 | 109 | hist_file.rename(backup_hist_file) |
|
114 | 110 | print("Backed up longer history file to", backup_hist_file) |
|
115 | 111 | else: |
|
116 | 112 | hist_file.unlink() |
|
117 | 113 | |
|
118 | 114 | new_hist_file.rename(hist_file) |
|
119 | 115 | |
|
116 | ||
|
120 | 117 | class HistoryClear(HistoryTrim): |
|
121 | 118 | description = clear_hist_help |
|
122 | keep = Int(0, | |
|
123 | help="Number of recent lines to keep in the database.") | |
|
124 | ||
|
125 | force = Bool(False, | |
|
126 | help="Don't prompt user for confirmation" | |
|
127 | ).tag(config=True) | |
|
128 | ||
|
129 | flags = Dict(dict( | |
|
130 | force = ({'HistoryClear' : {'force' : True}}, | |
|
131 | force.help), | |
|
132 | f = ({'HistoryTrim' : {'force' : True}}, | |
|
133 | force.help | |
|
119 | keep = Int(0, help="Number of recent lines to keep in the database.") | |
|
120 | ||
|
121 | force = Bool(False, help="Don't prompt user for confirmation").tag(config=True) | |
|
122 | ||
|
123 | flags = Dict( # type: ignore | |
|
124 | dict( | |
|
125 | force=({"HistoryClear": {"force": True}}, force.help), | |
|
126 | f=({"HistoryTrim": {"force": True}}, force.help), | |
|
134 | 127 | ) |
|
135 |
) |
|
|
136 | aliases = Dict() | |
|
128 | ) | |
|
129 | aliases = Dict() # type: ignore | |
|
137 | 130 | |
|
138 | 131 | def start(self): |
|
139 |
if self.force or ask_yes_no( |
|
|
140 |
default="no", interrupt="no" |
|
|
132 | if self.force or ask_yes_no( | |
|
133 | "Really delete all ipython history? ", default="no", interrupt="no" | |
|
134 | ): | |
|
141 | 135 | HistoryTrim.start(self) |
|
142 | 136 | |
|
137 | ||
|
143 | 138 | class HistoryApp(Application): |
|
144 |
name = |
|
|
139 | name = "ipython-history" | |
|
145 | 140 | description = "Manage the IPython history database." |
|
146 | 141 | |
|
147 | 142 | subcommands = Dict(dict( |
|
148 | 143 | trim = (HistoryTrim, HistoryTrim.description.splitlines()[0]), |
|
149 | 144 | clear = (HistoryClear, HistoryClear.description.splitlines()[0]), |
|
150 | 145 | )) |
|
151 | 146 | |
|
152 | 147 | def start(self): |
|
153 | 148 | if self.subapp is None: |
|
154 | 149 | print("No subcommand specified. Must specify one of: %s" % \ |
|
155 | 150 | (self.subcommands.keys())) |
|
156 | 151 | print() |
|
157 | 152 | self.print_description() |
|
158 | 153 | self.print_subcommands() |
|
159 | 154 | self.exit(1) |
|
160 | 155 | else: |
|
161 | 156 | return self.subapp.start() |
@@ -1,87 +1,84 b'' | |||
|
1 | 1 | [build-system] |
|
2 | 2 | requires = ["setuptools >= 51.0.0"] |
|
3 | 3 | # We need access to the 'setupbase' module at build time. |
|
4 | 4 | # Hence we declare a custom build backend. |
|
5 | 5 | build-backend = "_build_meta" # just re-exports setuptools.build_meta definitions |
|
6 | 6 | backend-path = ["."] |
|
7 | 7 | |
|
8 | 8 | [tool.mypy] |
|
9 | 9 | python_version = "3.10" |
|
10 | 10 | ignore_missing_imports = true |
|
11 | 11 | follow_imports = 'silent' |
|
12 | 12 | exclude = [ |
|
13 | 13 | 'test_\.+\.py', |
|
14 | 14 | 'IPython.utils.tests.test_wildcard', |
|
15 | 15 | 'testing', |
|
16 | 16 | 'tests', |
|
17 | 17 | 'PyColorize.py', |
|
18 | 18 | '_process_win32_controller.py', |
|
19 | 19 | 'IPython/core/application.py', |
|
20 | 20 | 'IPython/core/completerlib.py', |
|
21 | 21 | 'IPython/core/displaypub.py', |
|
22 | 'IPython/core/historyapp.py', | |
|
23 | 22 | #'IPython/core/interactiveshell.py', |
|
24 | 23 | 'IPython/core/magic.py', |
|
25 | 24 | 'IPython/core/profileapp.py', |
|
26 | 25 | # 'IPython/core/ultratb.py', |
|
27 | 26 | 'IPython/lib/deepreload.py', |
|
28 | 27 | 'IPython/lib/pretty.py', |
|
29 | 28 | 'IPython/sphinxext/ipython_directive.py', |
|
30 | 29 | 'IPython/terminal/ipapp.py', |
|
31 | 30 | 'IPython/utils/_process_win32.py', |
|
32 | 31 | 'IPython/utils/path.py', |
|
33 | 'IPython/utils/timing.py', | |
|
34 | 'IPython/utils/text.py' | |
|
35 | 32 | ] |
|
36 | 33 | |
|
37 | 34 | [tool.pytest.ini_options] |
|
38 | 35 | addopts = [ |
|
39 | 36 | "--durations=10", |
|
40 | 37 | "-pIPython.testing.plugin.pytest_ipdoctest", |
|
41 | 38 | "--ipdoctest-modules", |
|
42 | 39 | "--ignore=docs", |
|
43 | 40 | "--ignore=examples", |
|
44 | 41 | "--ignore=htmlcov", |
|
45 | 42 | "--ignore=ipython_kernel", |
|
46 | 43 | "--ignore=ipython_parallel", |
|
47 | 44 | "--ignore=results", |
|
48 | 45 | "--ignore=tmp", |
|
49 | 46 | "--ignore=tools", |
|
50 | 47 | "--ignore=traitlets", |
|
51 | 48 | "--ignore=IPython/core/tests/daft_extension", |
|
52 | 49 | "--ignore=IPython/sphinxext", |
|
53 | 50 | "--ignore=IPython/terminal/pt_inputhooks", |
|
54 | 51 | "--ignore=IPython/__main__.py", |
|
55 | 52 | "--ignore=IPython/external/qt_for_kernel.py", |
|
56 | 53 | "--ignore=IPython/html/widgets/widget_link.py", |
|
57 | 54 | "--ignore=IPython/html/widgets/widget_output.py", |
|
58 | 55 | "--ignore=IPython/terminal/console.py", |
|
59 | 56 | "--ignore=IPython/utils/_process_cli.py", |
|
60 | 57 | "--ignore=IPython/utils/_process_posix.py", |
|
61 | 58 | "--ignore=IPython/utils/_process_win32.py", |
|
62 | 59 | "--ignore=IPython/utils/_process_win32_controller.py", |
|
63 | 60 | "--ignore=IPython/utils/daemonize.py", |
|
64 | 61 | "--ignore=IPython/utils/eventful.py", |
|
65 | 62 | "--ignore=IPython/kernel", |
|
66 | 63 | "--ignore=IPython/consoleapp.py", |
|
67 | 64 | "--ignore=IPython/core/inputsplitter.py", |
|
68 | 65 | "--ignore=IPython/lib/kernel.py", |
|
69 | 66 | "--ignore=IPython/utils/jsonutil.py", |
|
70 | 67 | "--ignore=IPython/utils/localinterfaces.py", |
|
71 | 68 | "--ignore=IPython/utils/log.py", |
|
72 | 69 | "--ignore=IPython/utils/signatures.py", |
|
73 | 70 | "--ignore=IPython/utils/traitlets.py", |
|
74 | 71 | "--ignore=IPython/utils/version.py" |
|
75 | 72 | ] |
|
76 | 73 | doctest_optionflags = [ |
|
77 | 74 | "NORMALIZE_WHITESPACE", |
|
78 | 75 | "ELLIPSIS" |
|
79 | 76 | ] |
|
80 | 77 | ipdoctest_optionflags = [ |
|
81 | 78 | "NORMALIZE_WHITESPACE", |
|
82 | 79 | "ELLIPSIS" |
|
83 | 80 | ] |
|
84 | 81 | asyncio_mode = "strict" |
|
85 | 82 | |
|
86 | 83 | [tool.pyright] |
|
87 | 84 | pythonPlatform="All" |
General Comments 0
You need to be logged in to leave comments.
Login now