##// END OF EJS Templates
Merge pull request #1419 from takluyver/install-ext...
Min RK -
r6186:dbf3bdc6 merge
parent child Browse files
Show More
@@ -0,0 +1,12 b''
1 # -*- coding: utf-8 -*-
2 """
3 Useless IPython extension to test installing and loading extensions.
4 """
5 some_vars = {'arq': 185}
6
7 def load_ipython_extension(ip):
8 # set up simplified quantity input
9 ip.push(some_vars)
10
11 def unload_ipython_extension(ip):
12 ip.drop_by_id(some_vars)
@@ -19,6 +19,8 b' Authors:'
19
19
20 import os
20 import os
21 import sys
21 import sys
22 from urllib import urlretrieve
23 from urlparse import urlparse
22
24
23 from IPython.config.configurable import Configurable
25 from IPython.config.configurable import Configurable
24 from IPython.utils.traitlets import Instance
26 from IPython.utils.traitlets import Instance
@@ -123,3 +125,22 b' class ExtensionManager(Configurable):'
123 def _call_unload_ipython_extension(self, mod):
125 def _call_unload_ipython_extension(self, mod):
124 if hasattr(mod, 'unload_ipython_extension'):
126 if hasattr(mod, 'unload_ipython_extension'):
125 return mod.unload_ipython_extension(self.shell)
127 return mod.unload_ipython_extension(self.shell)
128
129 def install_extension(self, url, filename=None):
130 """Download and install an IPython extension.
131
132 If filename is given, the file will be so named (inside the extension
133 directory). Otherwise, the name from the URL will be used. The file must
134 have a .py or .zip extension; otherwise, a ValueError will be raised.
135 """
136 # Ensure the extension directory exists
137 if not os.path.isdir(self.ipython_extension_dir):
138 os.makedirs(self.ipython_extension_dir, mode = 0777)
139
140 if filename is None:
141 filename = urlparse(url).path.split('/')[-1]
142 if os.path.splitext(filename)[1] not in ('.py', '.zip'):
143 raise ValueError("The file must have a .py or .zip extension", filename)
144
145 filename = os.path.join(self.ipython_extension_dir, filename)
146 return urlretrieve(url, filename)
@@ -3457,6 +3457,31 b' Defaulting color scheme to \'NoColor\'"""'
3457 # print simple error message, rather than traceback if we can't
3457 # print simple error message, rather than traceback if we can't
3458 # hook up the GUI
3458 # hook up the GUI
3459 error(str(e))
3459 error(str(e))
3460
3461 def magic_install_ext(self, parameter_s):
3462 """Download and install an extension from a URL, e.g.::
3463
3464 %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py
3465
3466 The URL should point to an importable Python module - either a .py file
3467 or a .zip file.
3468
3469 Parameters:
3470
3471 -n filename : Specify a name for the file, rather than taking it from
3472 the URL.
3473 """
3474 opts, args = self.parse_options(parameter_s, 'n:')
3475 try:
3476 filename, headers = self.extension_manager.install_extension(args, opts.get('n'))
3477 except ValueError as e:
3478 print e
3479 return
3480
3481 filename = os.path.basename(filename)
3482 print "Installed %s. To use it, type:" % filename
3483 print " %%load_ext %s" % os.path.splitext(filename)[0]
3484
3460
3485
3461 def magic_load_ext(self, module_str):
3486 def magic_load_ext(self, module_str):
3462 """Load an IPython extension by its module name."""
3487 """Load an IPython extension by its module name."""
@@ -15,6 +15,7 b' import nose.tools as nt'
15 from IPython.testing import decorators as dec
15 from IPython.testing import decorators as dec
16 from IPython.testing import tools as tt
16 from IPython.testing import tools as tt
17 from IPython.utils import py3compat
17 from IPython.utils import py3compat
18 from IPython.utils.tempdir import TemporaryDirectory
18
19
19 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
20 # Test functions begin
21 # Test functions begin
@@ -394,3 +395,20 b' def test_prun_quotes():'
394 "Test that prun does not clobber string escapes (GH #1302)"
395 "Test that prun does not clobber string escapes (GH #1302)"
395 _ip.magic("prun -q x = '\t'")
396 _ip.magic("prun -q x = '\t'")
396 nt.assert_equal(_ip.user_ns['x'], '\t')
397 nt.assert_equal(_ip.user_ns['x'], '\t')
398
399 def test_extension():
400 tmpdir = TemporaryDirectory()
401 orig_ipython_dir = _ip.ipython_dir
402 try:
403 _ip.ipython_dir = tmpdir.name
404 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
405 url = os.path.join(os.path.dirname(__file__), "daft_extension.py")
406 _ip.magic("install_ext %s" % url)
407 _ip.user_ns.pop('arq', None)
408 _ip.magic("load_ext daft_extension")
409 tt.assert_equal(_ip.user_ns['arq'], 185)
410 _ip.magic("unload_ext daft_extension")
411 assert 'arq' not in _ip.user_ns
412 finally:
413 _ip.ipython_dir = orig_ipython_dir
414
@@ -4,3 +4,15 b''
4
4
5 This document describes in-flight development work.
5 This document describes in-flight development work.
6
6
7 Redesigned IPython notebook user interface
8 ------------------------------------------
9
10 .. add details
11
12 Other new features
13 ------------------
14
15 * **%install_ext**: A new magic function to install an IPython extension from
16 a URL. E.g. ``%install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py``.
17
18
General Comments 0
You need to be logged in to leave comments. Login now