Show More
@@ -1,73 +1,73 | |||||
1 | import IPython.ipapi |
|
1 | import IPython.ipapi | |
2 | ip = IPython.ipapi.get() |
|
2 | ip = IPython.ipapi.get() | |
3 |
|
3 | |||
4 | import os,pprint |
|
4 | import os,pprint | |
5 |
|
5 | |||
6 | def export(filename = None): |
|
6 | def export(filename = None): | |
7 |
|
7 | |||
8 | lines = ['import IPython.ipapi', 'ip = IPython.ipapi.get()',''] |
|
8 | lines = ['import IPython.ipapi', 'ip = IPython.ipapi.get()',''] | |
9 |
|
9 | |||
10 | vars = ip.db.keys('autorestore/*') |
|
10 | vars = ip.db.keys('autorestore/*') | |
11 | vars.sort() |
|
11 | vars.sort() | |
12 | varstomove = [] |
|
12 | varstomove = [] | |
13 | get = ip.db.get |
|
13 | get = ip.db.get | |
14 |
|
14 | |||
15 | macros = [] |
|
15 | macros = [] | |
16 | variables = [] |
|
16 | variables = [] | |
17 |
|
17 | |||
18 | for var in vars: |
|
18 | for var in vars: | |
19 | k = os.path.basename(var) |
|
19 | k = os.path.basename(var) | |
20 | v = get(var) |
|
20 | v = get(var) | |
21 |
|
21 | |||
22 | if k.startswith('_'): |
|
22 | if k.startswith('_'): | |
23 | continue |
|
23 | continue | |
24 | if isinstance(v, IPython.macro.Macro): |
|
24 | if isinstance(v, IPython.macro.Macro): | |
25 | macros.append((k,v)) |
|
25 | macros.append((k,v)) | |
26 | if type(v) in [int, str, float]: |
|
26 | if type(v) in [int, str, float]: | |
27 | variables.append((k,v)) |
|
27 | variables.append((k,v)) | |
28 |
|
28 | |||
29 |
|
29 | |||
30 |
|
30 | |||
31 | if macros: |
|
31 | if macros: | |
32 | lines.extend(['# === Macros ===' ,'']) |
|
32 | lines.extend(['# === Macros ===' ,'']) | |
33 | for k,v in macros: |
|
33 | for k,v in macros: | |
34 | lines.append("ip.defmacro('%s'," % k) |
|
34 | lines.append("ip.defmacro('%s'," % k) | |
35 | for line in v.value.splitlines(): |
|
35 | for line in v.value.splitlines(): | |
36 | lines.append(' ' + repr(line+'\n')) |
|
36 | lines.append(' ' + repr(line+'\n')) | |
37 | lines.extend([')', '']) |
|
37 | lines.extend([')', '']) | |
38 |
|
38 | |||
39 | if variables: |
|
39 | if variables: | |
40 | lines.extend(['','# === Variables ===','']) |
|
40 | lines.extend(['','# === Variables ===','']) | |
41 | for k,v in variables: |
|
41 | for k,v in variables: | |
42 | varstomove.append(k) |
|
42 | varstomove.append(k) | |
43 | lines.append('%s = %s' % (k,repr(v))) |
|
43 | lines.append('%s = %s' % (k,repr(v))) | |
44 |
|
44 | |||
45 | lines.append('ip.to_user_ns("%s")' % (' '.join(varstomove))) |
|
45 | lines.append('ip.to_user_ns("%s")' % (' '.join(varstomove))) | |
46 |
|
46 | |||
47 | bkms = ip.db.get('bookmarks',{}) |
|
47 | bkms = ip.db.get('bookmarks',{}) | |
48 |
|
48 | |||
49 | if bkms: |
|
49 | if bkms: | |
50 | lines.extend(['','# === Bookmarks ===','']) |
|
50 | lines.extend(['','# === Bookmarks ===','']) | |
51 | lines.append("ip.db['bookmarks'] = %s " % pprint.pformat(bkms, indent = 2) ) |
|
51 | lines.append("ip.db['bookmarks'] = %s " % pprint.pformat(bkms, indent = 2) ) | |
52 |
|
52 | |||
53 | aliases = ip.db.get('stored_aliases', {} ) |
|
53 | aliases = ip.db.get('stored_aliases', {} ) | |
54 |
|
54 | |||
55 | if aliases: |
|
55 | if aliases: | |
56 | lines.extend(['','# === Alias definitions ===','']) |
|
56 | lines.extend(['','# === Alias definitions ===','']) | |
57 | for k,v in aliases.items(): |
|
57 | for k,v in aliases.items(): | |
58 | lines.append("ip.defalias('%s', %s)" % (k, repr(v[1]))) |
|
58 | lines.append("ip.defalias('%s', %s)" % (k, repr(v[1]))) | |
59 |
|
59 | |||
60 | env = ip.db.get('stored_env') |
|
60 | env = ip.db.get('stored_env') | |
61 | if env: |
|
61 | if env: | |
62 | lines.extend(['','# === Stored env vars ===','']) |
|
62 | lines.extend(['','# === Stored env vars ===','']) | |
63 | lines.append("ip.db['stored_env'] = %s " % pprint.pformat(env, indent = 2) ) |
|
63 | lines.append("ip.db['stored_env'] = %s " % pprint.pformat(env, indent = 2) ) | |
64 |
|
64 | |||
65 |
|
65 | |||
66 |
|
66 | |||
67 | out = '\n'.join(lines) |
|
67 | out = '\n'.join(lines) | |
68 |
|
68 | |||
69 | if filename: |
|
69 | if filename: | |
70 | open(filename,'w').write(out) |
|
70 | open(filename,'w').write(out) | |
71 | else: |
|
71 | else: | |
72 | print out |
|
72 | print out | |
73 |
|
73 |
@@ -1,43 +1,43 | |||||
1 | """ IPython extension management tools. |
|
1 | """ IPython extension management tools. | |
2 |
|
2 | |||
3 | After installation, you'll have the 'extutil' object in your namespace. |
|
3 | After installation, you'll have the 'extutil' object in your namespace. | |
4 | to. |
|
4 | to. | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | # for the purposes of this module, every module that has the name 'ip' globally |
|
7 | # for the purposes of this module, every module that has the name 'ip' globally | |
8 | # installed as below is an IPython extension |
|
8 | # installed as below is an IPython extension | |
9 |
|
9 | |||
10 | import IPython.ipapi |
|
10 | import IPython.ipapi | |
11 | ip = IPython.ipapi.get() |
|
11 | ip = IPython.ipapi.get() | |
12 |
|
12 | |||
13 | import sys,textwrap,inspect |
|
13 | import sys,textwrap,inspect | |
14 |
|
14 | |||
15 | def indent(s, ind= ' '): |
|
15 | def indent(s, ind= ' '): | |
16 | return '\n'.join([ind +l for l in s.splitlines()]) |
|
16 | return '\n'.join([ind +l for l in s.splitlines()]) | |
17 |
|
17 | |||
18 | class ExtUtil: |
|
18 | class ExtUtil: | |
19 | """ IPython extensios (ipy_* etc.) management utilities """ |
|
19 | """ IPython extensios (ipy_* etc.) management utilities """ | |
20 |
|
20 | |||
21 | def describe(self): |
|
21 | def describe(self): | |
22 | for n,mod in self._active(): |
|
22 | for n,mod in self._active(): | |
23 | doc = inspect.getdoc(mod) |
|
23 | doc = inspect.getdoc(mod) | |
24 | if doc: |
|
24 | if doc: | |
25 | print '== %s ==' % n |
|
25 | print '== %s ==' % n | |
26 | print indent(doc) |
|
26 | print indent(doc) | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | def ls(self): |
|
29 | def ls(self): | |
30 | """ Show list of installed extensions. """ |
|
30 | """ Show list of installed extensions. """ | |
31 | for n,m in self._active(): |
|
31 | for n,m in self._active(): | |
32 | print '%-20s %s' % (n,m.__file__.replace('\\','/')) |
|
32 | print '%-20s %s' % (n,m.__file__.replace('\\','/')) | |
33 | def _active(self): |
|
33 | def _active(self): | |
34 | act = [] |
|
34 | act = [] | |
35 | for mname,m in sys.modules.items(): |
|
35 | for mname,m in sys.modules.items(): | |
36 | o = getattr(m, 'ip', None) |
|
36 | o = getattr(m, 'ip', None) | |
37 | if isinstance(o, IPython.ipapi.IPApi): |
|
37 | if isinstance(o, IPython.ipapi.IPApi): | |
38 | act.append((mname,m)) |
|
38 | act.append((mname,m)) | |
39 | act.sort() |
|
39 | act.sort() | |
40 | return act |
|
40 | return act | |
41 |
|
41 | |||
42 | extutil = ExtUtil() |
|
42 | extutil = ExtUtil() | |
43 | ip.to_user_ns('extutil') |
|
43 | ip.to_user_ns('extutil') |
@@ -1,38 +1,38 | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 |
|
2 | |||
3 |
|
3 | |||
4 | """ |
|
4 | """ | |
5 | Add %global magic for GNU Global usage. |
|
5 | Add %global magic for GNU Global usage. | |
6 |
|
6 | |||
7 | http://www.gnu.org/software/global/ |
|
7 | http://www.gnu.org/software/global/ | |
8 |
|
8 | |||
9 | """ |
|
9 | """ | |
10 |
|
10 | |||
11 | import IPython.ipapi |
|
11 | import IPython.ipapi | |
12 | ip = IPython.ipapi.get() |
|
12 | ip = IPython.ipapi.get() | |
13 | import os |
|
13 | import os | |
14 |
|
14 | |||
15 | # alter to your liking |
|
15 | # alter to your liking | |
16 | global_bin = 'd:/opt/global/bin/global' |
|
16 | global_bin = 'd:/opt/global/bin/global' | |
17 |
|
17 | |||
18 | def global_f(self,cmdline): |
|
18 | def global_f(self,cmdline): | |
19 | simple = 0 |
|
19 | simple = 0 | |
20 | if '-' not in cmdline: |
|
20 | if '-' not in cmdline: | |
21 | cmdline = '-rx ' + cmdline |
|
21 | cmdline = '-rx ' + cmdline | |
22 | simple = 1 |
|
22 | simple = 1 | |
23 |
|
23 | |||
24 | lines = [l.rstrip() for l in os.popen( global_bin + ' ' + cmdline ).readlines()] |
|
24 | lines = [l.rstrip() for l in os.popen( global_bin + ' ' + cmdline ).readlines()] | |
25 |
|
25 | |||
26 | if simple: |
|
26 | if simple: | |
27 | parts = [l.split(None,3) for l in lines] |
|
27 | parts = [l.split(None,3) for l in lines] | |
28 | lines = ['%s [%s]\n%s' % (p[2].rjust(70),p[1],p[3].rstrip()) for p in parts] |
|
28 | lines = ['%s [%s]\n%s' % (p[2].rjust(70),p[1],p[3].rstrip()) for p in parts] | |
29 | print "\n".join(lines) |
|
29 | print "\n".join(lines) | |
30 |
|
30 | |||
31 | ip.expose_magic('global', global_f) |
|
31 | ip.expose_magic('global', global_f) | |
32 |
|
32 | |||
33 | def global_completer(self,event): |
|
33 | def global_completer(self,event): | |
34 | compl = [l.rstrip() for l in os.popen(global_bin + ' -c ' + event.symbol).readlines()] |
|
34 | compl = [l.rstrip() for l in os.popen(global_bin + ' -c ' + event.symbol).readlines()] | |
35 | return compl |
|
35 | return compl | |
36 |
|
36 | |||
37 | ip.set_hook('complete_command', global_completer, str_key = '%global') |
|
37 | ip.set_hook('complete_command', global_completer, str_key = '%global') | |
38 |
|
38 |
This diff has been collapsed as it changes many lines, (503 lines changed) Show them Hide them | |||||
@@ -1,251 +1,252 | |||||
1 | """ Legacy stuff |
|
1 | """ Legacy stuff | |
2 |
|
2 | |||
3 | Various stuff that are there for historical / familiarity reasons. |
|
3 | Various stuff that are there for historical / familiarity reasons. | |
4 |
|
4 | |||
5 | This is automatically imported by default profile, though not other profiles |
|
5 | This is automatically imported by default profile, though not other profiles | |
6 | (e.g. 'sh' profile). |
|
6 | (e.g. 'sh' profile). | |
7 |
|
7 | |||
8 | Stuff that is considered obsolete / redundant is gradually moved here. |
|
8 | Stuff that is considered obsolete / redundant is gradually moved here. | |
9 |
|
9 | |||
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | import IPython.ipapi |
|
12 | import IPython.ipapi | |
13 | ip = IPython.ipapi.get() |
|
13 | ip = IPython.ipapi.get() | |
14 |
|
14 | |||
15 | import os,sys |
|
15 | import os,sys | |
16 |
|
16 | |||
17 | from IPython.genutils import * |
|
17 | from IPython.genutils import * | |
18 |
|
18 | |||
19 | # use ? |
|
19 | # use ? | |
20 | def magic_pdef(self, parameter_s='', namespaces=None): |
|
20 | def magic_pdef(self, parameter_s='', namespaces=None): | |
21 | """Print the definition header for any callable object. |
|
21 | """Print the definition header for any callable object. | |
22 |
|
22 | |||
23 | If the object is a class, print the constructor information.""" |
|
23 | If the object is a class, print the constructor information.""" | |
24 | self._inspect('pdef',parameter_s, namespaces) |
|
24 | self._inspect('pdef',parameter_s, namespaces) | |
25 |
|
25 | |||
26 |
ip.expose_magic("pdef", magic_pdef) |
|
26 | ip.expose_magic("pdef", magic_pdef) | |
27 |
|
27 | |||
28 |
# use ? |
|
28 | # use ? | |
29 | def magic_pdoc(self, parameter_s='', namespaces=None): |
|
29 | def magic_pdoc(self, parameter_s='', namespaces=None): | |
30 | """Print the docstring for an object. |
|
30 | """Print the docstring for an object. | |
31 |
|
31 | |||
32 | If the given object is a class, it will print both the class and the |
|
32 | If the given object is a class, it will print both the class and the | |
33 | constructor docstrings.""" |
|
33 | constructor docstrings.""" | |
34 | self._inspect('pdoc',parameter_s, namespaces) |
|
34 | self._inspect('pdoc',parameter_s, namespaces) | |
35 |
|
35 | |||
36 |
ip.expose_magic("pdoc", magic_pdoc) |
|
36 | ip.expose_magic("pdoc", magic_pdoc) | |
37 |
|
37 | |||
38 | # use ?? |
|
38 | # use ?? | |
39 | def magic_psource(self, parameter_s='', namespaces=None): |
|
39 | def magic_psource(self, parameter_s='', namespaces=None): | |
40 | """Print (or run through pager) the source code for an object.""" |
|
40 | """Print (or run through pager) the source code for an object.""" | |
41 | self._inspect('psource',parameter_s, namespaces) |
|
41 | self._inspect('psource',parameter_s, namespaces) | |
42 |
|
42 | |||
43 | ip.expose_magic("pdoc", magic_psource) |
|
43 | ip.expose_magic("pdoc", magic_psource) | |
44 |
|
44 | |||
45 | # use ? |
|
45 | # use ? | |
46 | def magic_pfile(self, parameter_s=''): |
|
46 | def magic_pfile(self, parameter_s=''): | |
47 | """Print (or run through pager) the file where an object is defined. |
|
47 | """Print (or run through pager) the file where an object is defined. | |
48 |
|
48 | |||
49 | The file opens at the line where the object definition begins. IPython |
|
49 | The file opens at the line where the object definition begins. IPython | |
50 | will honor the environment variable PAGER if set, and otherwise will |
|
50 | will honor the environment variable PAGER if set, and otherwise will | |
51 | do its best to print the file in a convenient form. |
|
51 | do its best to print the file in a convenient form. | |
52 |
|
52 | |||
53 | If the given argument is not an object currently defined, IPython will |
|
53 | If the given argument is not an object currently defined, IPython will | |
54 | try to interpret it as a filename (automatically adding a .py extension |
|
54 | try to interpret it as a filename (automatically adding a .py extension | |
55 | if needed). You can thus use %pfile as a syntax highlighting code |
|
55 | if needed). You can thus use %pfile as a syntax highlighting code | |
56 | viewer.""" |
|
56 | viewer.""" | |
57 |
|
57 | |||
58 | # first interpret argument as an object name |
|
58 | # first interpret argument as an object name | |
59 | out = self._inspect('pfile',parameter_s) |
|
59 | out = self._inspect('pfile',parameter_s) | |
60 | # if not, try the input as a filename |
|
60 | # if not, try the input as a filename | |
61 | if out == 'not found': |
|
61 | if out == 'not found': | |
62 | try: |
|
62 | try: | |
63 | filename = get_py_filename(parameter_s) |
|
63 | filename = get_py_filename(parameter_s) | |
64 | except IOError,msg: |
|
64 | except IOError,msg: | |
65 | print msg |
|
65 | print msg | |
66 | return |
|
66 | return | |
67 | page(self.shell.inspector.format(file(filename).read())) |
|
67 | page(self.shell.inspector.format(file(filename).read())) | |
68 |
|
68 | |||
69 |
ip.expose_magic("pfile", magic_pfile) |
|
69 | ip.expose_magic("pfile", magic_pfile) | |
70 |
|
70 | |||
71 | # use rehashx |
|
71 | # use rehashx | |
72 |
|
72 | |||
73 | def magic_rehash(self, parameter_s = ''): |
|
73 | def magic_rehash(self, parameter_s = ''): | |
74 | """Update the alias table with all entries in $PATH. |
|
74 | """Update the alias table with all entries in $PATH. | |
75 |
|
75 | |||
76 | This version does no checks on execute permissions or whether the |
|
76 | This version does no checks on execute permissions or whether the | |
77 | contents of $PATH are truly files (instead of directories or something |
|
77 | contents of $PATH are truly files (instead of directories or something | |
78 | else). For such a safer (but slower) version, use %rehashx.""" |
|
78 | else). For such a safer (but slower) version, use %rehashx.""" | |
79 |
|
79 | |||
80 | # This function (and rehashx) manipulate the alias_table directly |
|
80 | # This function (and rehashx) manipulate the alias_table directly | |
81 | # rather than calling magic_alias, for speed reasons. A rehash on a |
|
81 | # rather than calling magic_alias, for speed reasons. A rehash on a | |
82 | # typical Linux box involves several thousand entries, so efficiency |
|
82 | # typical Linux box involves several thousand entries, so efficiency | |
83 | # here is a top concern. |
|
83 | # here is a top concern. | |
84 |
|
84 | |||
85 | path = filter(os.path.isdir,os.environ.get('PATH','').split(os.pathsep)) |
|
85 | path = filter(os.path.isdir,os.environ.get('PATH','').split(os.pathsep)) | |
86 | alias_table = self.shell.alias_table |
|
86 | alias_table = self.shell.alias_table | |
87 | for pdir in path: |
|
87 | for pdir in path: | |
88 | for ff in os.listdir(pdir): |
|
88 | for ff in os.listdir(pdir): | |
89 | # each entry in the alias table must be (N,name), where |
|
89 | # each entry in the alias table must be (N,name), where | |
90 | # N is the number of positional arguments of the alias. |
|
90 | # N is the number of positional arguments of the alias. | |
91 | alias_table[ff] = (0,ff) |
|
91 | alias_table[ff] = (0,ff) | |
92 | # Make sure the alias table doesn't contain keywords or builtins |
|
92 | # Make sure the alias table doesn't contain keywords or builtins | |
93 | self.shell.alias_table_validate() |
|
93 | self.shell.alias_table_validate() | |
94 | # Call again init_auto_alias() so we get 'rm -i' and other modified |
|
94 | # Call again init_auto_alias() so we get 'rm -i' and other modified | |
95 | # aliases since %rehash will probably clobber them |
|
95 | # aliases since %rehash will probably clobber them | |
96 | self.shell.init_auto_alias() |
|
96 | self.shell.init_auto_alias() | |
97 |
|
97 | |||
98 | ip.expose_magic("rehash", magic_rehash) |
|
98 | ip.expose_magic("rehash", magic_rehash) | |
99 |
|
99 | |||
100 | #use cd -<tab> |
|
100 | #use cd -<tab> | |
101 | def magic_dhist(self, parameter_s=''): |
|
101 | def magic_dhist(self, parameter_s=''): | |
102 | """Print your history of visited directories. |
|
102 | """Print your history of visited directories. | |
103 |
|
103 | |||
104 | %dhist -> print full history\\ |
|
104 | %dhist -> print full history\\ | |
105 | %dhist n -> print last n entries only\\ |
|
105 | %dhist n -> print last n entries only\\ | |
106 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ |
|
106 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ | |
107 |
|
107 | |||
108 | This history is automatically maintained by the %cd command, and |
|
108 | This history is automatically maintained by the %cd command, and | |
109 | always available as the global list variable _dh. You can use %cd -<n> |
|
109 | always available as the global list variable _dh. You can use %cd -<n> | |
110 | to go to directory number <n>.""" |
|
110 | to go to directory number <n>.""" | |
111 |
|
111 | |||
112 | dh = self.shell.user_ns['_dh'] |
|
112 | dh = self.shell.user_ns['_dh'] | |
113 | if parameter_s: |
|
113 | if parameter_s: | |
114 | try: |
|
114 | try: | |
115 | args = map(int,parameter_s.split()) |
|
115 | args = map(int,parameter_s.split()) | |
116 | except: |
|
116 | except: | |
117 | self.arg_err(Magic.magic_dhist) |
|
117 | self.arg_err(Magic.magic_dhist) | |
118 | return |
|
118 | return | |
119 | if len(args) == 1: |
|
119 | if len(args) == 1: | |
120 | ini,fin = max(len(dh)-(args[0]),0),len(dh) |
|
120 | ini,fin = max(len(dh)-(args[0]),0),len(dh) | |
121 | elif len(args) == 2: |
|
121 | elif len(args) == 2: | |
122 | ini,fin = args |
|
122 | ini,fin = args | |
123 | else: |
|
123 | else: | |
124 | self.arg_err(Magic.magic_dhist) |
|
124 | self.arg_err(Magic.magic_dhist) | |
125 | return |
|
125 | return | |
126 | else: |
|
126 | else: | |
127 | ini,fin = 0,len(dh) |
|
127 | ini,fin = 0,len(dh) | |
128 | nlprint(dh, |
|
128 | nlprint(dh, | |
129 | header = 'Directory history (kept in _dh)', |
|
129 | header = 'Directory history (kept in _dh)', | |
130 | start=ini,stop=fin) |
|
130 | start=ini,stop=fin) | |
131 |
|
131 | |||
132 | ip.expose_magic("dhist", magic_dhist) |
|
132 | ip.expose_magic("dhist", magic_dhist) | |
133 |
|
133 | |||
134 | # Exit |
|
134 | # Exit | |
135 | def magic_Quit(self, parameter_s=''): |
|
135 | def magic_Quit(self, parameter_s=''): | |
136 | """Exit IPython without confirmation (like %Exit).""" |
|
136 | """Exit IPython without confirmation (like %Exit).""" | |
137 |
|
137 | |||
138 | self.shell.exit_now = True |
|
138 | self.shell.exit_now = True | |
139 |
|
139 | |||
140 | ip.expose_magic("Quit", magic_Quit) |
|
140 | ip.expose_magic("Quit", magic_Quit) | |
141 |
|
141 | |||
142 |
|
142 | |||
143 | # make it autocallable fn if you really need it |
|
143 | # make it autocallable fn if you really need it | |
144 | def magic_p(self, parameter_s=''): |
|
144 | def magic_p(self, parameter_s=''): | |
145 | """Just a short alias for Python's 'print'.""" |
|
145 | """Just a short alias for Python's 'print'.""" | |
146 | exec 'print ' + parameter_s in self.shell.user_ns |
|
146 | exec 'print ' + parameter_s in self.shell.user_ns | |
147 |
|
147 | |||
148 | ip.expose_magic("p", magic_p) |
|
148 | ip.expose_magic("p", magic_p) | |
149 |
|
149 | |||
150 | # up + enter. One char magic. |
|
150 | # up + enter. One char magic. | |
151 | def magic_r(self, parameter_s=''): |
|
151 | def magic_r(self, parameter_s=''): | |
152 | """Repeat previous input. |
|
152 | """Repeat previous input. | |
153 |
|
153 | |||
154 | If given an argument, repeats the previous command which starts with |
|
154 | If given an argument, repeats the previous command which starts with | |
155 | the same string, otherwise it just repeats the previous input. |
|
155 | the same string, otherwise it just repeats the previous input. | |
156 |
|
156 | |||
157 | Shell escaped commands (with ! as first character) are not recognized |
|
157 | Shell escaped commands (with ! as first character) are not recognized | |
158 | by this system, only pure python code and magic commands. |
|
158 | by this system, only pure python code and magic commands. | |
159 | """ |
|
159 | """ | |
160 |
|
160 | |||
161 | start = parameter_s.strip() |
|
161 | start = parameter_s.strip() | |
162 | esc_magic = self.shell.ESC_MAGIC |
|
162 | esc_magic = self.shell.ESC_MAGIC | |
163 | # Identify magic commands even if automagic is on (which means |
|
163 | # Identify magic commands even if automagic is on (which means | |
164 | # the in-memory version is different from that typed by the user). |
|
164 | # the in-memory version is different from that typed by the user). | |
165 | if self.shell.rc.automagic: |
|
165 | if self.shell.rc.automagic: | |
166 | start_magic = esc_magic+start |
|
166 | start_magic = esc_magic+start | |
167 | else: |
|
167 | else: | |
168 | start_magic = start |
|
168 | start_magic = start | |
169 | # Look through the input history in reverse |
|
169 | # Look through the input history in reverse | |
170 | for n in range(len(self.shell.input_hist)-2,0,-1): |
|
170 | for n in range(len(self.shell.input_hist)-2,0,-1): | |
171 | input = self.shell.input_hist[n] |
|
171 | input = self.shell.input_hist[n] | |
172 | # skip plain 'r' lines so we don't recurse to infinity |
|
172 | # skip plain 'r' lines so we don't recurse to infinity | |
173 | if input != '_ip.magic("r")\n' and \ |
|
173 | if input != '_ip.magic("r")\n' and \ | |
174 | (input.startswith(start) or input.startswith(start_magic)): |
|
174 | (input.startswith(start) or input.startswith(start_magic)): | |
175 | #print 'match',`input` # dbg |
|
175 | #print 'match',`input` # dbg | |
176 | print 'Executing:',input, |
|
176 | print 'Executing:',input, | |
177 | self.shell.runlines(input) |
|
177 | self.shell.runlines(input) | |
178 | return |
|
178 | return | |
179 | print 'No previous input matching `%s` found.' % start |
|
179 | print 'No previous input matching `%s` found.' % start | |
180 |
|
180 | |||
181 | ip.expose_magic("r", magic_r) |
|
181 | ip.expose_magic("r", magic_r) | |
182 |
|
182 | |||
183 |
|
183 | |||
184 | # use _ip.option.automagic |
|
184 | # use _ip.option.automagic | |
185 |
|
185 | |||
186 | def magic_automagic(self, parameter_s = ''): |
|
186 | def magic_automagic(self, parameter_s = ''): | |
187 | """Make magic functions callable without having to type the initial %. |
|
187 | """Make magic functions callable without having to type the initial %. | |
188 |
|
188 | |||
189 | Without argumentsl toggles on/off (when off, you must call it as |
|
189 | Without argumentsl toggles on/off (when off, you must call it as | |
190 | %automagic, of course). With arguments it sets the value, and you can |
|
190 | %automagic, of course). With arguments it sets the value, and you can | |
191 | use any of (case insensitive): |
|
191 | use any of (case insensitive): | |
192 |
|
192 | |||
193 | - on,1,True: to activate |
|
193 | - on,1,True: to activate | |
194 |
|
194 | |||
195 | - off,0,False: to deactivate. |
|
195 | - off,0,False: to deactivate. | |
196 |
|
196 | |||
197 | Note that magic functions have lowest priority, so if there's a |
|
197 | Note that magic functions have lowest priority, so if there's a | |
198 | variable whose name collides with that of a magic fn, automagic won't |
|
198 | variable whose name collides with that of a magic fn, automagic won't | |
199 | work for that function (you get the variable instead). However, if you |
|
199 | work for that function (you get the variable instead). However, if you | |
200 | delete the variable (del var), the previously shadowed magic function |
|
200 | delete the variable (del var), the previously shadowed magic function | |
201 | becomes visible to automagic again.""" |
|
201 | becomes visible to automagic again.""" | |
202 |
|
202 | |||
203 | rc = self.shell.rc |
|
203 | rc = self.shell.rc | |
204 | arg = parameter_s.lower() |
|
204 | arg = parameter_s.lower() | |
205 | if parameter_s in ('on','1','true'): |
|
205 | if parameter_s in ('on','1','true'): | |
206 | rc.automagic = True |
|
206 | rc.automagic = True | |
207 | elif parameter_s in ('off','0','false'): |
|
207 | elif parameter_s in ('off','0','false'): | |
208 | rc.automagic = False |
|
208 | rc.automagic = False | |
209 | else: |
|
209 | else: | |
210 | rc.automagic = not rc.automagic |
|
210 | rc.automagic = not rc.automagic | |
211 | print '\n' + Magic.auto_status[rc.automagic] |
|
211 | print '\n' + Magic.auto_status[rc.automagic] | |
212 |
|
212 | |||
213 | ip.expose_magic("automagic", magic_automagic) |
|
213 | ip.expose_magic("automagic", magic_automagic) | |
214 |
|
214 | |||
215 | # use _ip.options.autocall |
|
215 | # use _ip.options.autocall | |
216 | def magic_autocall(self, parameter_s = ''): |
|
216 | def magic_autocall(self, parameter_s = ''): | |
217 | """Make functions callable without having to type parentheses. |
|
217 | """Make functions callable without having to type parentheses. | |
218 |
|
218 | |||
219 | Usage: |
|
219 | Usage: | |
220 |
|
220 | |||
221 | %autocall [mode] |
|
221 | %autocall [mode] | |
222 |
|
222 | |||
223 | The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the |
|
223 | The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the | |
224 | value is toggled on and off (remembering the previous state).""" |
|
224 | value is toggled on and off (remembering the previous state).""" | |
225 |
|
225 | |||
226 | rc = self.shell.rc |
|
226 | rc = self.shell.rc | |
227 |
|
227 | |||
228 | if parameter_s: |
|
228 | if parameter_s: | |
229 | arg = int(parameter_s) |
|
229 | arg = int(parameter_s) | |
230 | else: |
|
230 | else: | |
231 | arg = 'toggle' |
|
231 | arg = 'toggle' | |
232 |
|
232 | |||
233 | if not arg in (0,1,2,'toggle'): |
|
233 | if not arg in (0,1,2,'toggle'): | |
234 | error('Valid modes: (0->Off, 1->Smart, 2->Full') |
|
234 | error('Valid modes: (0->Off, 1->Smart, 2->Full') | |
235 | return |
|
235 | return | |
236 |
|
236 | |||
237 | if arg in (0,1,2): |
|
237 | if arg in (0,1,2): | |
238 | rc.autocall = arg |
|
238 | rc.autocall = arg | |
239 | else: # toggle |
|
239 | else: # toggle | |
240 | if rc.autocall: |
|
240 | if rc.autocall: | |
241 | self._magic_state.autocall_save = rc.autocall |
|
241 | self._magic_state.autocall_save = rc.autocall | |
242 | rc.autocall = 0 |
|
242 | rc.autocall = 0 | |
243 | else: |
|
243 | else: | |
244 | try: |
|
244 | try: | |
245 | rc.autocall = self._magic_state.autocall_save |
|
245 | rc.autocall = self._magic_state.autocall_save | |
246 | except AttributeError: |
|
246 | except AttributeError: | |
247 | rc.autocall = self._magic_state.autocall_save = 1 |
|
247 | rc.autocall = self._magic_state.autocall_save = 1 | |
248 |
|
248 | |||
249 | print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall] |
|
249 | print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall] | |
250 |
|
250 | |||
251 | ip.expose_magic("autocall", magic_autocall) No newline at end of file |
|
251 | ip.expose_magic("autocall", magic_autocall) | |
|
252 |
@@ -1,4 +1,4 | |||||
1 | """ Config file for 'default' profile """ |
|
1 | """ Config file for 'default' profile """ | |
2 |
|
2 | |||
3 | # get various stuff that are there for historical / familiarity reasons |
|
3 | # get various stuff that are there for historical / familiarity reasons | |
4 | import ipy_legacy No newline at end of file |
|
4 | import ipy_legacy |
@@ -1,25 +1,25 | |||||
1 | """ IPython 'sci' profile |
|
1 | """ IPython 'sci' profile | |
2 |
|
2 | |||
3 | Replaces the old scipy profile. |
|
3 | Replaces the old scipy profile. | |
4 |
|
4 | |||
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | import IPython.ipapi |
|
8 | import IPython.ipapi | |
9 | import ipy_defaults |
|
9 | import ipy_defaults | |
10 |
|
10 | |||
11 | def main(): |
|
11 | def main(): | |
12 | ip = IPython.ipapi.get() |
|
12 | ip = IPython.ipapi.get() | |
13 |
|
13 | |||
14 | try: |
|
14 | try: | |
15 | ip.ex("import scipy") |
|
15 | ip.ex("import scipy") | |
16 | ip.ex("import numpy") |
|
16 | ip.ex("import numpy") | |
17 |
|
17 | |||
18 | ip.ex("from scipy import *") |
|
18 | ip.ex("from scipy import *") | |
19 | ip.ex("from numpy import *") |
|
19 | ip.ex("from numpy import *") | |
20 | print "SciPy profile successfully loaded." |
|
20 | print "SciPy profile successfully loaded." | |
21 | except ImportError: |
|
21 | except ImportError: | |
22 | print "Unable to start scipy profile, are scipy and numpy installed?" |
|
22 | print "Unable to start scipy profile, are scipy and numpy installed?" | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | main() No newline at end of file |
|
25 | main() |
@@ -1,68 +1,68 | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 |
|
2 | |||
3 | """ IPython extension: Render templates from variables and paste to clipbard """ |
|
3 | """ IPython extension: Render templates from variables and paste to clipbard """ | |
4 |
|
4 | |||
5 | import IPython.ipapi |
|
5 | import IPython.ipapi | |
6 |
|
6 | |||
7 | ip = IPython.ipapi.get() |
|
7 | ip = IPython.ipapi.get() | |
8 |
|
8 | |||
9 | from string import Template |
|
9 | from string import Template | |
10 | import sys,os |
|
10 | import sys,os | |
11 |
|
11 | |||
12 | from IPython.Itpl import itplns |
|
12 | from IPython.Itpl import itplns | |
13 |
|
13 | |||
14 | def toclip_w32(s): |
|
14 | def toclip_w32(s): | |
15 | """ Places contents of s to clipboard |
|
15 | """ Places contents of s to clipboard | |
16 |
|
16 | |||
17 | Needs pyvin32 to work: |
|
17 | Needs pyvin32 to work: | |
18 | http://sourceforge.net/projects/pywin32/ |
|
18 | http://sourceforge.net/projects/pywin32/ | |
19 | """ |
|
19 | """ | |
20 | import win32clipboard as cl |
|
20 | import win32clipboard as cl | |
21 | import win32con |
|
21 | import win32con | |
22 | cl.OpenClipboard() |
|
22 | cl.OpenClipboard() | |
23 | cl.EmptyClipboard() |
|
23 | cl.EmptyClipboard() | |
24 | cl.SetClipboardText( s.replace('\n','\r\n' )) |
|
24 | cl.SetClipboardText( s.replace('\n','\r\n' )) | |
25 | cl.CloseClipboard() |
|
25 | cl.CloseClipboard() | |
26 |
|
26 | |||
27 | try: |
|
27 | try: | |
28 | import win32clipboard |
|
28 | import win32clipboard | |
29 | toclip = toclip_w32 |
|
29 | toclip = toclip_w32 | |
30 | except ImportError: |
|
30 | except ImportError: | |
31 | def toclip(s): pass |
|
31 | def toclip(s): pass | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | def render(tmpl): |
|
34 | def render(tmpl): | |
35 | """ Render a template (Itpl format) from ipython variables |
|
35 | """ Render a template (Itpl format) from ipython variables | |
36 |
|
36 | |||
37 | Example: |
|
37 | Example: | |
38 |
|
38 | |||
39 | $ import ipy_render |
|
39 | $ import ipy_render | |
40 | $ my_name = 'Bob' # %store this for convenience |
|
40 | $ my_name = 'Bob' # %store this for convenience | |
41 | $ t_submission_form = "Submission report, author: $my_name" # %store also |
|
41 | $ t_submission_form = "Submission report, author: $my_name" # %store also | |
42 | $ render t_submission_form |
|
42 | $ render t_submission_form | |
43 |
|
43 | |||
44 | => returns "Submission report, author: Bob" and copies to clipboard on win32 |
|
44 | => returns "Submission report, author: Bob" and copies to clipboard on win32 | |
45 |
|
45 | |||
46 | # if template exist as a file, read it. Note: ;f hei vaan => f("hei vaan") |
|
46 | # if template exist as a file, read it. Note: ;f hei vaan => f("hei vaan") | |
47 | $ ;render c:/templates/greeting.txt |
|
47 | $ ;render c:/templates/greeting.txt | |
48 |
|
48 | |||
49 | Template examples (Ka-Ping Yee's Itpl library): |
|
49 | Template examples (Ka-Ping Yee's Itpl library): | |
50 |
|
50 | |||
51 | Here is a $string. |
|
51 | Here is a $string. | |
52 | Here is a $module.member. |
|
52 | Here is a $module.member. | |
53 | Here is an $object.member. |
|
53 | Here is an $object.member. | |
54 | Here is a $functioncall(with, arguments). |
|
54 | Here is a $functioncall(with, arguments). | |
55 | Here is an ${arbitrary + expression}. |
|
55 | Here is an ${arbitrary + expression}. | |
56 | Here is an $array[3] member. |
|
56 | Here is an $array[3] member. | |
57 | Here is a $dictionary['member']. |
|
57 | Here is a $dictionary['member']. | |
58 | """ |
|
58 | """ | |
59 |
|
59 | |||
60 | if os.path.isfile(tmpl): |
|
60 | if os.path.isfile(tmpl): | |
61 | tmpl = open(tmpl).read() |
|
61 | tmpl = open(tmpl).read() | |
62 |
|
62 | |||
63 | res = itplns(tmpl, ip.user_ns) |
|
63 | res = itplns(tmpl, ip.user_ns) | |
64 | toclip(res) |
|
64 | toclip(res) | |
65 | return res |
|
65 | return res | |
66 |
|
66 | |||
67 | ip.to_user_ns('render') |
|
67 | ip.to_user_ns('render') | |
68 | No newline at end of file |
|
68 |
@@ -1,70 +1,70 | |||||
1 | r""" %which magic command |
|
1 | r""" %which magic command | |
2 |
|
2 | |||
3 | %which <cmd> => search PATH for files matching PATH. Also scans aliases |
|
3 | %which <cmd> => search PATH for files matching PATH. Also scans aliases | |
4 |
|
4 | |||
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | import IPython.ipapi |
|
7 | import IPython.ipapi | |
8 | ip = IPython.ipapi.get() |
|
8 | ip = IPython.ipapi.get() | |
9 |
|
9 | |||
10 | import os,sys |
|
10 | import os,sys | |
11 | from fnmatch import fnmatch |
|
11 | from fnmatch import fnmatch | |
12 | def which(fname): |
|
12 | def which(fname): | |
13 | fullpath = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) |
|
13 | fullpath = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) | |
14 |
|
14 | |||
15 | if '.' not in fullpath: |
|
15 | if '.' not in fullpath: | |
16 | fullpath = ['.'] + fullpath |
|
16 | fullpath = ['.'] + fullpath | |
17 | fn = fname |
|
17 | fn = fname | |
18 | for p in fullpath: |
|
18 | for p in fullpath: | |
19 | for f in os.listdir(p): |
|
19 | for f in os.listdir(p): | |
20 | head, ext = os.path.splitext(f) |
|
20 | head, ext = os.path.splitext(f) | |
21 | if f == fn or fnmatch(head, fn): |
|
21 | if f == fn or fnmatch(head, fn): | |
22 | yield os.path.join(p,f) |
|
22 | yield os.path.join(p,f) | |
23 | return |
|
23 | return | |
24 |
|
24 | |||
25 | def which_alias(fname): |
|
25 | def which_alias(fname): | |
26 | for al, tgt in ip.IP.alias_table.items(): |
|
26 | for al, tgt in ip.IP.alias_table.items(): | |
27 | if not (al == fname or fnmatch(al, fname)): |
|
27 | if not (al == fname or fnmatch(al, fname)): | |
28 | continue |
|
28 | continue | |
29 | trg = tgt[1] |
|
29 | trg = tgt[1] | |
30 |
|
30 | |||
31 | trans = ip.expand_alias(trg) |
|
31 | trans = ip.expand_alias(trg) | |
32 | cmd = trans.split(None,1)[0] |
|
32 | cmd = trans.split(None,1)[0] | |
33 | print al,"->",trans |
|
33 | print al,"->",trans | |
34 | for realcmd in which(cmd): |
|
34 | for realcmd in which(cmd): | |
35 | print " ==",realcmd |
|
35 | print " ==",realcmd | |
36 |
|
36 | |||
37 | def which_f(self, arg): |
|
37 | def which_f(self, arg): | |
38 | r""" %which <cmd> => search PATH for files matching cmd. Also scans aliases. |
|
38 | r""" %which <cmd> => search PATH for files matching cmd. Also scans aliases. | |
39 |
|
39 | |||
40 | Traverses PATH and prints all files (not just executables!) that match the |
|
40 | Traverses PATH and prints all files (not just executables!) that match the | |
41 | pattern on command line. Probably more useful in finding stuff |
|
41 | pattern on command line. Probably more useful in finding stuff | |
42 | interactively than 'which', which only prints the first matching item. |
|
42 | interactively than 'which', which only prints the first matching item. | |
43 |
|
43 | |||
44 | Also discovers and expands aliases, so you'll see what will be executed |
|
44 | Also discovers and expands aliases, so you'll see what will be executed | |
45 | when you call an alias. |
|
45 | when you call an alias. | |
46 |
|
46 | |||
47 | Example: |
|
47 | Example: | |
48 |
|
48 | |||
49 | [~]|62> %which d |
|
49 | [~]|62> %which d | |
50 | d -> ls -F --color=auto |
|
50 | d -> ls -F --color=auto | |
51 | == c:\cygwin\bin\ls.exe |
|
51 | == c:\cygwin\bin\ls.exe | |
52 | c:\cygwin\bin\d.exe |
|
52 | c:\cygwin\bin\d.exe | |
53 |
|
53 | |||
54 | [~]|64> %which diff* |
|
54 | [~]|64> %which diff* | |
55 | diff3 -> diff3 |
|
55 | diff3 -> diff3 | |
56 | == c:\cygwin\bin\diff3.exe |
|
56 | == c:\cygwin\bin\diff3.exe | |
57 | diff -> diff |
|
57 | diff -> diff | |
58 | == c:\cygwin\bin\diff.exe |
|
58 | == c:\cygwin\bin\diff.exe | |
59 | c:\cygwin\bin\diff.exe |
|
59 | c:\cygwin\bin\diff.exe | |
60 | c:\cygwin\bin\diff3.exe |
|
60 | c:\cygwin\bin\diff3.exe | |
61 |
|
61 | |||
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 | which_alias(arg) |
|
64 | which_alias(arg) | |
65 |
|
65 | |||
66 | for e in which(arg): |
|
66 | for e in which(arg): | |
67 | print e |
|
67 | print e | |
68 |
|
68 | |||
69 | ip.expose_magic("which",which_f) |
|
69 | ip.expose_magic("which",which_f) | |
70 |
|
70 |
General Comments 0
You need to be logged in to leave comments.
Login now