Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use load_ipython_extension to register doit magic functions #192

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions doc/tools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ First you need to register the new magic function into ipython shell.

.. code-block:: pycon

>>> from doit.tools import register_doit_as_IPython_magic
>>> register_doit_as_IPython_magic()
>>> %load_ext doit.tools


.. Tip::
Expand All @@ -89,8 +88,8 @@ First you need to register the new magic function into ipython shell.
(i.e. :file:`~/.ipython/profile_default/startup/doit_magic.ipy`)
with the following content::

from doit.tools import register_doit_as_IPython_magic
register_doit_as_IPython_magic()
from doit.tools import load_ipython_extension
load_ipython_extension()

Then you can define your `task_creator` functions and invoke them with `%doit`
magic-function, instead of invoking the cmd-line script with a :file:`dodo.py`
Expand Down
3 changes: 3 additions & 0 deletions doit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
from doit.loader import create_after
from doit.doit_cmd import get_var
from doit.api import run
from doit.tools import load_ipython_extension

__all__ = ['get_var', 'run', 'create_after']

def get_initial_workdir():
"""working-directory from where the doit command was invoked on shell"""
return loader.initial_workdir

assert load_ipython_extension # silence pyflakes
16 changes: 11 additions & 5 deletions doit/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def set_trace(): # pragma: no cover



def register_doit_as_IPython_magic(): # pragma: no cover
def load_ipython_extension(ip=None): # pragma: no cover
"""
Defines a ``%doit`` magic function[1] that discovers and execute tasks
from IPython's interactive variables (global namespace).
Expand All @@ -250,17 +250,21 @@ def register_doit_as_IPython_magic(): # pragma: no cover
(``~/.ipython/profile_default/startup/doit_magic.ipy``) with the
following content:

from doit.tools import register_doit_as_IPython_magic
register_doit_as_IPython_magic()
%load_ext doit.tools
%reload_ext doit.tools
%doit list

[1] http://ipython.org/ipython-doc/dev/interactive/tutorial.html#magic-functions
"""
from IPython.core.magic import register_line_magic
from IPython.core.getipython import get_ipython
from IPython.core.magic import register_line_magic

from doit.cmd_base import ModuleTaskLoader
from doit.doit_cmd import DoitMain

# Only (re)load_ext provides the ip context.
ip = ip or get_ipython()

@register_line_magic
def doit(line):
"""
Expand All @@ -283,11 +287,13 @@ def doit(line):
hi IPython

"""
ip = get_ipython()
# Override db-files location inside ipython-profile dir,
# which is certainly writable.
prof_dir = ip.profile_dir.location
opt_vals = {'dep_file': os.path.join(prof_dir, 'db', '.doit.db')}
commander = DoitMain(ModuleTaskLoader(ip.user_module),
extra_config={'GLOBAL': opt_vals})
commander.run(line.split())

# the name register_doit_as_IPython_magic is deprecated on **
register_doit_as_IPython_magic = load_ipython_extension