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 ``` diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1278a9d --- /dev/null +++ b/setup.py @@ -0,0 +1,51 @@ +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', + maintainer='Liam Deacon', + maintainer_email='liam.m.deacon@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', + + 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 0000000..0b1bada Binary files /dev/null and b/tempmagic/__init__.pyc differ 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 0000000..248d07e Binary files /dev/null and b/tempmagic/tempmagic.pyc differ