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