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