Show More
@@ -26,6 +26,7 b' import threading' | |||
|
26 | 26 | |
|
27 | 27 | # Our own packages |
|
28 | 28 | from IPython.core.error import StdinNotImplementedError |
|
29 | from IPython.core.magic import Magics, register_magics, line_magic | |
|
29 | 30 | from IPython.config.configurable import Configurable |
|
30 | 31 | from IPython.external.decorator import decorator |
|
31 | 32 | from IPython.testing.skipdoctest import skip_doctest |
@@ -74,13 +75,13 b' class HistoryAccessor(Configurable):' | |||
|
74 | 75 | hist_file = Unicode(config=True, |
|
75 | 76 | help="""Path to file to use for SQLite history database. |
|
76 | 77 | |
|
77 |
By default, IPython will put the history database in the IPython |
|
|
78 |
directory. If you would rather share one history among |
|
|
79 | you ca set this value in each, so that they are consistent. | |
|
78 | By default, IPython will put the history database in the IPython | |
|
79 | profile directory. If you would rather share one history among | |
|
80 | profiles, you ca set this value in each, so that they are consistent. | |
|
80 | 81 | |
|
81 |
Due to an issue with fcntl, SQLite is known to misbehave on some NFS |
|
|
82 |
If you see IPython hanging, try setting this to something on a |
|
|
83 | e.g:: | |
|
82 | Due to an issue with fcntl, SQLite is known to misbehave on some NFS | |
|
83 | mounts. If you see IPython hanging, try setting this to something on a | |
|
84 | local disk, e.g:: | |
|
84 | 85 | |
|
85 | 86 | ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite |
|
86 | 87 | |
@@ -153,7 +154,8 b' class HistoryAccessor(Configurable):' | |||
|
153 | 154 | def init_db(self): |
|
154 | 155 | """Connect to the database, and create tables if necessary.""" |
|
155 | 156 | # use detect_types so that timestamps return datetime objects |
|
156 |
self.db = sqlite3.connect(self.hist_file, |
|
|
157 | self.db = sqlite3.connect(self.hist_file, | |
|
158 | detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) | |
|
157 | 159 | self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer |
|
158 | 160 | primary key autoincrement, start timestamp, |
|
159 | 161 | end timestamp, num_cmds integer, remark text)""") |
@@ -216,7 +218,8 b' class HistoryAccessor(Configurable):' | |||
|
216 | 218 | Returns |
|
217 | 219 | ------- |
|
218 | 220 | |
|
219 |
(session_id [int], start [datetime], end [datetime], num_cmds [int], |
|
|
221 | (session_id [int], start [datetime], end [datetime], num_cmds [int], | |
|
222 | remark [unicode]) | |
|
220 | 223 | |
|
221 | 224 | Sessions that are running or did not exit cleanly will have `end=None` |
|
222 | 225 | and `num_cmds=None`. |
@@ -512,7 +515,8 b' class HistoryManager(HistoryAccessor):' | |||
|
512 | 515 | session += self.session_number |
|
513 | 516 | if session==self.session_number: # Current session |
|
514 | 517 | return self._get_range_session(start, stop, raw, output) |
|
515 |
return super(HistoryManager, self).get_range(session, start, stop, raw, |
|
|
518 | return super(HistoryManager, self).get_range(session, start, stop, raw, | |
|
519 | output) | |
|
516 | 520 | |
|
517 | 521 | ## ---------------------------- |
|
518 | 522 | ## Methods for storing history: |
@@ -611,7 +615,9 b' class HistoryManager(HistoryAccessor):' | |||
|
611 | 615 | print("ERROR! Session/line number was not unique in", |
|
612 | 616 | "database. History logging moved to new session", |
|
613 | 617 | self.session_number) |
|
614 | try: # Try writing to the new session. If this fails, don't recurse | |
|
618 | try: | |
|
619 | # Try writing to the new session. If this fails, don't | |
|
620 | # recurse | |
|
615 | 621 | self._writeout_input_cache(conn) |
|
616 | 622 | except sqlite3.IntegrityError: |
|
617 | 623 | pass |
@@ -718,8 +724,12 b' def _format_lineno(session, line):' | |||
|
718 | 724 | return "%s#%s" % (session, line) |
|
719 | 725 | |
|
720 | 726 | |
|
727 | @register_magics | |
|
728 | class HistoryMagics(Magics): | |
|
729 | ||
|
721 | 730 | @skip_doctest |
|
722 | def magic_history(self, parameter_s = ''): | |
|
731 | @line_magic | |
|
732 | def history(self, parameter_s = ''): | |
|
723 | 733 | """Print input history (_i<n> variables), with most recent last. |
|
724 | 734 | |
|
725 | 735 | %history [-o -p -t -n] [-f filename] [range | -g pattern | -l number] |
@@ -746,31 +756,33 b" def magic_history(self, parameter_s = ''):" | |||
|
746 | 756 | |
|
747 | 757 | -o: also print outputs for each input. |
|
748 | 758 | |
|
749 |
-p: print classic '>>>' python prompts before each input. This is |
|
|
750 |
for making documentation, and in conjunction with -o, for |
|
|
751 | doctest-ready output. | |
|
759 | -p: print classic '>>>' python prompts before each input. This is | |
|
760 | useful for making documentation, and in conjunction with -o, for | |
|
761 | producing doctest-ready output. | |
|
752 | 762 | |
|
753 |
-r: (default) print the 'raw' history, i.e. the actual commands you |
|
|
763 | -r: (default) print the 'raw' history, i.e. the actual commands you | |
|
764 | typed. | |
|
754 | 765 | |
|
755 |
-t: print the 'translated' history, as IPython understands it. |
|
|
756 |
filters your input and converts it all into valid Python |
|
|
757 |
executing it (things like magics or aliases are turned |
|
|
758 |
calls, for example). With this option, you'll see the |
|
|
759 |
instead of the user-entered version: '%cd /' will be |
|
|
760 | 'get_ipython().magic("%cd /")' instead of '%cd /'. | |
|
766 | -t: print the 'translated' history, as IPython understands it. | |
|
767 | IPython filters your input and converts it all into valid Python | |
|
768 | source before executing it (things like magics or aliases are turned | |
|
769 | into function calls, for example). With this option, you'll see the | |
|
770 | native history instead of the user-entered version: '%cd /' will be | |
|
771 | seen as 'get_ipython().magic("%cd /")' instead of '%cd /'. | |
|
761 | 772 | |
|
762 | 773 | -g: treat the arg as a pattern to grep for in (full) history. |
|
763 | 774 | This includes the saved history (almost all commands ever written). |
|
764 | 775 | Use '%hist -g' to show full saved history (may be very long). |
|
765 | 776 | |
|
766 |
-l: get the last n lines from all sessions. Specify n as a single |
|
|
767 | the default is the last 10 lines. | |
|
777 | -l: get the last n lines from all sessions. Specify n as a single | |
|
778 | arg, or the default is the last 10 lines. | |
|
768 | 779 | |
|
769 |
-f FILENAME: instead of printing the output to the screen, redirect |
|
|
770 |
the given file. The file is always overwritten, though *when |
|
|
771 |
IPython asks for confirmation first. In particular, running |
|
|
772 |
|
|
|
773 |
FILENAME even if it already exists *without* |
|
|
780 | -f FILENAME: instead of printing the output to the screen, redirect | |
|
781 | it to the given file. The file is always overwritten, though *when | |
|
782 | it can*, IPython asks for confirmation first. In particular, running | |
|
783 | the command 'history -f FILENAME' from the IPython Notebook | |
|
784 | interface will replace FILENAME even if it already exists *without* | |
|
785 | confirmation. | |
|
774 | 786 | |
|
775 | 787 | Examples |
|
776 | 788 | -------- |
@@ -784,7 +796,8 b" def magic_history(self, parameter_s = ''):" | |||
|
784 | 796 | """ |
|
785 | 797 | |
|
786 | 798 | if not self.shell.displayhook.do_full_cache: |
|
787 |
print('This feature is only available if numbered prompts |
|
|
799 | print('This feature is only available if numbered prompts ' | |
|
800 | 'are in use.') | |
|
788 | 801 | return |
|
789 | 802 | opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string') |
|
790 | 803 | |
@@ -823,7 +836,6 b" def magic_history(self, parameter_s = ''):" | |||
|
823 | 836 | # Raw history is the default |
|
824 | 837 | raw = not('t' in opts) |
|
825 | 838 | |
|
826 | default_length = 40 | |
|
827 | 839 | pattern = None |
|
828 | 840 | |
|
829 | 841 | if 'g' in opts: # Glob search |
@@ -833,7 +845,7 b" def magic_history(self, parameter_s = ''):" | |||
|
833 | 845 | elif 'l' in opts: # Get 'tail' |
|
834 | 846 | try: |
|
835 | 847 | n = int(args) |
|
836 | except ValueError, IndexError: | |
|
848 | except (ValueError, IndexError): | |
|
837 | 849 | n = 10 |
|
838 | 850 | hist = history_manager.get_tail(n, raw=raw, output=get_output) |
|
839 | 851 | else: |
@@ -842,14 +854,16 b" def magic_history(self, parameter_s = ''):" | |||
|
842 | 854 | else: # Just get history for the current session |
|
843 | 855 | hist = history_manager.get_range(raw=raw, output=get_output) |
|
844 | 856 | |
|
845 |
# We could be displaying the entire history, so let's not try to pull |
|
|
846 |
# into a list in memory. Anything that needs more space will just |
|
|
857 | # We could be displaying the entire history, so let's not try to pull | |
|
858 | # it into a list in memory. Anything that needs more space will just | |
|
859 | # misalign. | |
|
847 | 860 | width = 4 |
|
848 | 861 | |
|
849 | 862 | for session, lineno, inline in hist: |
|
850 |
# Print user history with tabs expanded to 4 spaces. The GUI |
|
|
851 |
# use hard tabs for easier usability in auto-indented code, |
|
|
852 |
# to produce PEP-8 compliant history for safe pasting |
|
|
863 | # Print user history with tabs expanded to 4 spaces. The GUI | |
|
864 | # clients use hard tabs for easier usability in auto-indented code, | |
|
865 | # but we want to produce PEP-8 compliant history for safe pasting | |
|
866 | # into an editor. | |
|
853 | 867 | if get_output: |
|
854 | 868 | inline, output = inline |
|
855 | 869 | inline = inline.expandtabs(4).rstrip() |
@@ -870,17 +884,17 b" def magic_history(self, parameter_s = ''):" | |||
|
870 | 884 | if close_at_end: |
|
871 | 885 | outfile.close() |
|
872 | 886 | |
|
873 | ||
|
874 |
def |
|
|
887 | @line_magic | |
|
888 | def rep(self, arg): | |
|
875 | 889 | r"""Repeat a command, or get command to input line for editing. |
|
876 | 890 | |
|
877 | 891 | %recall and %rep are equivalent. |
|
878 | 892 | |
|
879 | 893 | - %recall (no arguments): |
|
880 | 894 | |
|
881 |
Place a string version of last computation result (stored in the |
|
|
882 |
variable) to the next input prompt. Allows you to create |
|
|
883 | lines without using copy-paste:: | |
|
895 | Place a string version of last computation result (stored in the | |
|
896 | special '_' variable) to the next input prompt. Allows you to create | |
|
897 | elaborate command lines without using copy-paste:: | |
|
884 | 898 | |
|
885 | 899 | In[1]: l = ["hei", "vaan"] |
|
886 | 900 | In[2]: "".join(l) |
@@ -928,8 +942,8 b' def magic_rep(self, arg):' | |||
|
928 | 942 | self.shell.set_next_input(cmd.rstrip()) |
|
929 | 943 | print("Couldn't evaluate or find in history:", arg) |
|
930 | 944 | |
|
931 | ||
|
932 |
def |
|
|
945 | @line_magic | |
|
946 | def rerun(self, parameter_s=''): | |
|
933 | 947 | """Re-run previous input |
|
934 | 948 | |
|
935 | 949 | By default, you can specify ranges of input history to be repeated |
@@ -971,11 +985,9 b" def magic_rerun(self, parameter_s=''):" | |||
|
971 | 985 | |
|
972 | 986 | |
|
973 | 987 | def init_ipython(ip): |
|
974 | ip.define_magic("rep", magic_rep) | |
|
975 |
ip.define_magic( |
|
|
976 |
ip.define_magic( |
|
|
977 | ip.define_magic("hist", magic_history) # Alternative name | |
|
978 | ip.define_magic("history", magic_history) | |
|
988 | ip.magics_manager.register(HistoryMagics) | |
|
989 | #ip.define_magic('hist', HistoryMagics.history) | |
|
990 | #ip.define_magic('recall', HistoryMagics.rep) | |
|
979 | 991 | |
|
980 | 992 | # XXX - ipy_completers are in quarantine, need to be updated to new apis |
|
981 | 993 | #import ipy_completers |
General Comments 0
You need to be logged in to leave comments.
Login now