Skip to content

Commit

Permalink
Change the way the capturer is invoked
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfast committed May 17, 2018
1 parent 48e68b3 commit be891d6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 30 deletions.
10 changes: 6 additions & 4 deletions src/importnb/capture.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
try:
from .utils import __IPYTHON__, export
from .utils import export, __IPYTHON__
except:
from utils import __IPYTHON__, export
from utils import export, __IPYTHON__
__all__ = 'capture_output',

if False and __IPYTHON__:
if __IPYTHON__:
from IPython.utils.capture import capture_output
else:
from contextlib import redirect_stdout, ExitStack
Expand Down Expand Up @@ -60,4 +61,5 @@ def stderr(self): return self._stderr and self._stderr.getvalue() or ''

if __name__ == '__main__':
export('capture.ipynb', '../importnb/capture.py')
__import__('doctest').testmod()
__import__('doctest').testmod()

23 changes: 15 additions & 8 deletions src/importnb/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def lazy_loader_cls(loader):
return inspect.getclosurevars(loader).nonlocals.get('cls', loader)
return loader

e = ExitStack()

class Notebook(SourceFileLoader, ExitStack):
"""A SourceFileLoader for notebooks that provides line number debugginer in the JSON source."""
EXTENSION_SUFFIXES = '.ipynb',

def __init__(
self, fullname=None, path=None, *, stdout=False, stderr=False, display=True, lazy=False
self, fullname=None, path=None, *, stdout=False, stderr=False, display=False, lazy=False
):
SourceFileLoader.__init__(self, fullname, path)
ExitStack.__init__(self)
Expand All @@ -78,14 +80,18 @@ def __init__(

def __enter__(self, position=0):
add_path_hooks(type(self), self.EXTENSION_SUFFIXES, position=position, lazy=self._lazy)
stack = super().__enter__()
return stack.enter_context(capture_output(
stdout=self._stdout, stderr=self._stderr, display=self._display
))
if self._capture:
stack = super().__enter__()
return stack.enter_context(capture_output(
stdout=self._stdout, stderr=self._stderr, display=self._display
))

def __exit__(self, *excepts): remove_one_path_hook(type(self))
@property
def _capture(self): return any((self._stdout, self._stderr, self._display))
def __exit__(self, *excepts):
remove_one_path_hook(type(self))

def exec_module(self, module): super().exec_module(module)
if self._capture: super().__exit__(*excepts)

def source_to_code(Notebook, data, path):
with __import__('io').BytesIO(data) as stream:
Expand All @@ -111,4 +117,5 @@ def unload_ipython_extension(ip=None):

if __name__ == '__main__':
export('loader.ipynb', '../importnb/loader.py')
__import__('doctest').testmod()
__import__('doctest').testmod()

13 changes: 13 additions & 0 deletions src/importnb/tests/test_importnb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@
"import sys"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def test_capturer():\n",
" from importnb.capture import capture_output\n",
" if __IPYTHON__:\n",
" from IPython.utils.capture import capture_output as ipython_version\n",
" assert capture_output is ipython_version"
]
},
{
"cell_type": "code",
"execution_count": 10,
Expand Down
20 changes: 14 additions & 6 deletions src/notebooks/capture.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" from .utils import __IPYTHON__, export\n",
" from .utils import export, __IPYTHON__\n",
"except:\n",
" from utils import __IPYTHON__, export"
" from utils import export, __IPYTHON__\n",
"__all__ = 'capture_output',"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"if False and __IPYTHON__:\n",
"if __IPYTHON__:\n",
" from IPython.utils.capture import capture_output\n",
"else:\n",
" from contextlib import redirect_stdout, ExitStack\n",
Expand Down Expand Up @@ -75,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {
"scrolled": false
},
Expand All @@ -85,6 +86,13 @@
" export('capture.ipynb', '../importnb/capture.py')\n",
" __import__('doctest').testmod()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
36 changes: 24 additions & 12 deletions src/notebooks/loader.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -144,7 +144,7 @@
" EXTENSION_SUFFIXES = '.ipynb',\n",
" \n",
" def __init__(\n",
" self, fullname=None, path=None, *, stdout=False, stderr=False, display=True, lazy=False\n",
" self, fullname=None, path=None, *, stdout=False, stderr=False, display=False, lazy=False\n",
" ): \n",
" SourceFileLoader.__init__(self, fullname, path)\n",
" ExitStack.__init__(self)\n",
Expand All @@ -153,14 +153,19 @@
" \n",
" def __enter__(self, position=0): \n",
" add_path_hooks(type(self), self.EXTENSION_SUFFIXES, position=position, lazy=self._lazy)\n",
" stack = super().__enter__()\n",
" return stack.enter_context(capture_output(\n",
" stdout=self._stdout, stderr=self._stderr, display=self._display\n",
" ))\n",
" \n",
" def __exit__(self, *excepts): remove_one_path_hook(type(self))\n",
" if self._capture:\n",
" stack = super().__enter__()\n",
" return stack.enter_context(capture_output(\n",
" stdout=self._stdout, stderr=self._stderr, display=self._display\n",
" ))\n",
" \n",
" def exec_module(self, module): super().exec_module(module) \n",
" @property\n",
" def _capture(self): return any((self._stdout, self._stderr, self._display))\n",
" \n",
" def __exit__(self, *excepts): \n",
" remove_one_path_hook(type(self))\n",
" \n",
" if self._capture: super().__exit__(*excepts)\n",
" \n",
" def source_to_code(Notebook, data, path):\n",
" with __import__('io').BytesIO(data) as stream:\n",
Expand All @@ -176,7 +181,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -203,7 +208,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -222,7 +227,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {
"scrolled": false
},
Expand All @@ -232,6 +237,13 @@
" export('loader.ipynb', '../importnb/loader.py')\n",
" __import__('doctest').testmod()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit be891d6

Please sign in to comment.