~ellisonbg/ipython/bugfixes0411409

« back to all changes in this revision

Viewing changes to IPython/Extensions/ipy_render.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
""" IPython extension: Render templates from variables and paste to clipbard """
 
4
 
 
5
import IPython.ipapi
 
6
 
 
7
ip = IPython.ipapi.get()
 
8
 
 
9
from string import Template
 
10
import sys,os
 
11
 
 
12
from IPython.Itpl import itplns
 
13
 
 
14
def toclip_w32(s):
 
15
    """ Places contents of s to clipboard
 
16
    
 
17
    Needs pyvin32 to work:
 
18
    http://sourceforge.net/projects/pywin32/
 
19
    """
 
20
    import win32clipboard as cl
 
21
    import win32con
 
22
    cl.OpenClipboard()
 
23
    cl.EmptyClipboard()
 
24
    cl.SetClipboardText( s.replace('\n','\r\n' ))
 
25
    cl.CloseClipboard()
 
26
 
 
27
try:
 
28
    import win32clipboard    
 
29
    toclip = toclip_w32
 
30
except ImportError:
 
31
    def toclip(s): pass
 
32
    
 
33
 
 
34
def render(tmpl):
 
35
    """ Render a template (Itpl format) from ipython variables
 
36
 
 
37
    Example:
 
38
    
 
39
    $ import ipy_render
 
40
    $ my_name = 'Bob'  # %store this for convenience
 
41
    $ t_submission_form = "Submission report, author: $my_name"  # %store also
 
42
    $ render t_submission_form
 
43
    
 
44
    => returns "Submission report, author: Bob" and copies to clipboard on win32
 
45
 
 
46
    # if template exist as a file, read it. Note: ;f hei vaan => f("hei vaan")
 
47
    $ ;render c:/templates/greeting.txt  
 
48
    
 
49
    Template examples (Ka-Ping Yee's Itpl library):
 
50
    
 
51
    Here is a $string.
 
52
    Here is a $module.member.
 
53
    Here is an $object.member.
 
54
    Here is a $functioncall(with, arguments).
 
55
    Here is an ${arbitrary + expression}.
 
56
    Here is an $array[3] member.
 
57
    Here is a $dictionary['member'].
 
58
    """
 
59
    
 
60
    if os.path.isfile(tmpl):
 
61
        tmpl = open(tmpl).read()
 
62
        
 
63
    res = itplns(tmpl, ip.user_ns)
 
64
    toclip(res)
 
65
    return res
 
66
 
 
67
ip.to_user_ns('render')
 
68
    
 
 
b'\\ No newline at end of file'