From 31410a6db0b969190eab926520e871eb51dc32e7 Mon Sep 17 00:00:00 2001 From: Liam Deacon Date: Mon, 16 Oct 2017 14:50:05 +0100 Subject: [PATCH 1/3] Added setup.py Enables installation via `pip` for Jupyter >= 5.0 --- setup.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 setup.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1103e3b --- /dev/null +++ b/setup.py @@ -0,0 +1,48 @@ +try: + from setuptools import setup +except ImportError: + from distutils.core import setup +import io +from os import path + +here = path.abspath(path.dirname(__file__)) + +# Get the long description from the relevant file +with io.open(path.join(here, 'README.md'), encoding='utf-8') as f: + long_description = f.read() + +install_requires = [ + 'jupyter>=1.0.0', +] + +setup( + name='ipython-tempmagic', + version='0.0.1', + description='Temporary file magic IPython', + long_description=long_description, + url='https://github.com/tkf/ipython-tempmagic', + + # Author details + author='Takafumi Arakaki', + author_email='aka.tkf@gmail.com', + + # Choose your license + license='', + + # See https://pypi.python.org/pypi?%3Aaction=list_classifiers + classifiers=[ + 'Topic :: Text Processing :: Markup :: HTML', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + ], + + # What does your project relate to? + keywords='ipython jupyter notebook tempfile tempdir', + + py_modules=['tempmagic.py'], + install_requires=install_requires, +) From 729eb5093ac4a848c21905adbf4792a44b0885b8 Mon Sep 17 00:00:00 2001 From: Liam Deacon Date: Mon, 16 Oct 2017 14:53:53 +0100 Subject: [PATCH 2/3] Updated for pip install Added notes on how to install using pip for Jupyter >= 5.0 --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index a7fc777..d8266fe 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,22 @@ - `%%with_temp_dir` -- Run Python code in a temporary directory and clean up it after the execution. +# Installation + +Install using pip: + +``` +pip install ipython-tempmagic # PyPI published version +``` + +or + +``` +pip install git+https://github.com/Lightslayer/ipython-tempmagic # development version +``` + +For older Jupyter versions (< 5.0): + ```text %install_ext https://raw.github.com/tkf/ipython-tempmagic/master/tempmagic.py ``` From f6f0566531663c543cef675fb6d5d5f526629575 Mon Sep 17 00:00:00 2001 From: Liam Deacon Date: Thu, 8 Feb 2018 19:49:35 +0000 Subject: [PATCH 3/3] Fixed IPython extension --- setup.py | 3 ++ tempmagic/__init__.py | 1 + tempmagic/__init__.pyc | Bin 0 -> 148 bytes tempmagic/tempmagic.py | 86 ++++++++++++++++++++++++++++++++++++++++ tempmagic/tempmagic.pyc | Bin 0 -> 3264 bytes 5 files changed, 90 insertions(+) create mode 100644 tempmagic/__init__.py create mode 100644 tempmagic/__init__.pyc create mode 100644 tempmagic/tempmagic.py create mode 100644 tempmagic/tempmagic.pyc diff --git a/setup.py b/setup.py index 1103e3b..1278a9d 100644 --- a/setup.py +++ b/setup.py @@ -25,6 +25,8 @@ # Author details author='Takafumi Arakaki', author_email='aka.tkf@gmail.com', + maintainer='Liam Deacon', + maintainer_email='liam.m.deacon@gmail.com', # Choose your license license='', @@ -43,6 +45,7 @@ # What does your project relate to? keywords='ipython jupyter notebook tempfile tempdir', + packages={'tempmagic':''}, py_modules=['tempmagic.py'], install_requires=install_requires, ) diff --git a/tempmagic/__init__.py b/tempmagic/__init__.py new file mode 100644 index 0000000..691abf7 --- /dev/null +++ b/tempmagic/__init__.py @@ -0,0 +1 @@ +from tempmagic import * diff --git a/tempmagic/__init__.pyc b/tempmagic/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..0b1bada4ae71d54461b0173e4bd7ff68b49ecd55 GIT binary patch literal 148 zcmZSn%*$20vL-5-0ScIav;zXz z*qk7N)ZBvH#PrN$4IqaBK@^K3OX|nRXXa&=#K-FuRF-f6Wo&ZuQ%ZAE?LfvA12F>t D1BM-l literal 0 HcmV?d00001 diff --git a/tempmagic/tempmagic.py b/tempmagic/tempmagic.py new file mode 100644 index 0000000..090783f --- /dev/null +++ b/tempmagic/tempmagic.py @@ -0,0 +1,86 @@ +import os +import atexit +import contextlib + +from IPython.core.magic import Magics, magics_class, line_magic, cell_magic +from IPython.core.magic_arguments import (argument, magic_arguments, + parse_argstring) +from IPython.utils.tempdir import TemporaryDirectory + + +@contextlib.contextmanager +def chcwd(path): + cwd = os.getcwd() + try: + os.chdir(path) + yield + finally: + os.chdir(cwd) + + +@magics_class +class TempMagic(Magics): + + def __init__(self, *args, **kwds): + super(TempMagic, self).__init__(*args, **kwds) + self._temp_dirs = [] + atexit.register(self.cleanup_all) + + def cleanup_all(self): + for td in self._temp_dirs: + try: + td.cleanup() + except: + pass + self._temp_dirs[:] = [] + + @staticmethod + def _filter_none_values(*args, **kwds): + return dict( + (k, v) for (k, v) in dict(*args, **kwds).items() + if v is not None) + + @magic_arguments() + @argument('--suffix', '-s', default=None) + @argument('--prefix', '-p', default=None) + @argument('--directory', '-d', default=None) + @line_magic + def cdtemp(self, parameter_s=''): + """ + Make temporary directory and change the current to there. + + The temporary directory made will be removed when the current + IPython process terminates. + + """ + args = parse_argstring(self.cdtemp, parameter_s) + kwds = self._filter_none_values(vars(args)) + td = TemporaryDirectory(**kwds) + self._temp_dirs.append(td) + os.chdir(td.name) + return td.name + + @magic_arguments() + @argument('--suffix', '-s', default=None) + @argument('--prefix', '-p', default=None) + @argument('--directory', '-d', default=None) + @cell_magic + def with_temp_dir(self, line, cell): + """ + Execute code in a temporary directory. + """ + args = parse_argstring(self.with_temp_dir, line) + kwds = self._filter_none_values(vars(args)) + with TemporaryDirectory(**kwds) as path: + with chcwd(path): + self.shell.run_cell(cell) + + +def load_ipython_extension(ip): + """Load the extension in IPython.""" + global _loaded + if not _loaded: + ip.register_magics(TempMagic) + _loaded = True + +_loaded = False diff --git a/tempmagic/tempmagic.pyc b/tempmagic/tempmagic.pyc new file mode 100644 index 0000000000000000000000000000000000000000..248d07ebe950ccafe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