##// END OF EJS Templates
Fix bug in iplib.py/safe_execfile that raises an IndexError when ipython is launched with a script as in ./ipython.py foo.py
Fix bug in iplib.py/safe_execfile that raises an IndexError when ipython is launched with a script as in ./ipython.py foo.py

File last commit:

r617:b971beab
r619:8630b292
Show More
ipy_render.py
67 lines | 1.7 KiB | text/x-python | PythonLexer
vivainio
Added ipy_render.py for template expansion
r607 #!/usr/bin/env python
vivainio
switch ipy_render.py to use Itpl notation
r614 """ IPython extension: Render templates from variables and paste to clipbard """
vivainio
Added ipy_render.py for template expansion
r607 import IPython.ipapi
ip = IPython.ipapi.get()
from string import Template
vivainio
win32 clipboard takes \r\n. render now treats arg as filename if it exists
r617 import sys,os
vivainio
Added ipy_render.py for template expansion
r607
vivainio
switch ipy_render.py to use Itpl notation
r614 from IPython.Itpl import itplns
vivainio
Added ipy_render.py for template expansion
r607 def toclip_w32(s):
vivainio
switch ipy_render.py to use Itpl notation
r614 """ Places contents of s to clipboard
vivainio
check win32clipboard existence instead of platform==win32
r613
Needs pyvin32 to work:
http://sourceforge.net/projects/pywin32/
"""
vivainio
Added ipy_render.py for template expansion
r607 import win32clipboard as cl
import win32con
cl.OpenClipboard()
cl.EmptyClipboard()
vivainio
win32 clipboard takes \r\n. render now treats arg as filename if it exists
r617 cl.SetClipboardText( s.replace('\n','\r\n' ))
vivainio
Added ipy_render.py for template expansion
r607 cl.CloseClipboard()
vivainio
check win32clipboard existence instead of platform==win32
r613 try:
import win32clipboard
vivainio
Added ipy_render.py for template expansion
r607 toclip = toclip_w32
vivainio
check win32clipboard existence instead of platform==win32
r613 except ImportError:
vivainio
Added ipy_render.py for template expansion
r607 def toclip(s): pass
def render(tmpl):
vivainio
switch ipy_render.py to use Itpl notation
r614 """ Render a template (Itpl format) from ipython variables
vivainio
Added ipy_render.py for template expansion
r607
Example:
$ import ipy_render
$ my_name = 'Bob' # %store this for convenience
$ t_submission_form = "Submission report, author: $my_name" # %store also
$ render t_submission_form
=> returns "Submission report, author: Bob" and copies to clipboard on win32
vivainio
switch ipy_render.py to use Itpl notation
r614
vivainio
win32 clipboard takes \r\n. render now treats arg as filename if it exists
r617 # if template exist as a file, read it. Note: ;f hei vaan => f("hei vaan")
$ ;render c:/templates/greeting.txt
vivainio
switch ipy_render.py to use Itpl notation
r614 Template examples (Ka-Ping Yee's Itpl library):
vivainio
Added ipy_render.py for template expansion
r607
vivainio
switch ipy_render.py to use Itpl notation
r614 Here is a $string.
Here is a $module.member.
Here is an $object.member.
Here is a $functioncall(with, arguments).
Here is an ${arbitrary + expression}.
Here is an $array[3] member.
Here is a $dictionary['member'].
vivainio
Added ipy_render.py for template expansion
r607 """
vivainio
win32 clipboard takes \r\n. render now treats arg as filename if it exists
r617 if os.path.isfile(tmpl):
tmpl = open(tmpl).read()
vivainio
switch ipy_render.py to use Itpl notation
r614 res = itplns(tmpl, ip.user_ns)
vivainio
Added ipy_render.py for template expansion
r607 toclip(res)
return res
vivainio
check win32clipboard existence instead of platform==win32
r613 ip.to_user_ns('render')
vivainio
Added ipy_render.py for template expansion
r607