##// END OF EJS Templates
check win32clipboard existence instead of platform==win32
vivainio -
Show More
@@ -1,45 +1,50 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2
2
3 import IPython.ipapi
3 import IPython.ipapi
4
4
5 ip = IPython.ipapi.get()
5 ip = IPython.ipapi.get()
6
6
7 from string import Template
7 from string import Template
8 import sys
8 import sys
9
9
10 def toclip_w32(s):
10 def toclip_w32(s):
11 """ places contents of s to clipboard """
11 """ places contents of s to clipboard
12
13 Needs pyvin32 to work:
14 http://sourceforge.net/projects/pywin32/
15 """
12 import win32clipboard as cl
16 import win32clipboard as cl
13 import win32con
17 import win32con
14 cl.OpenClipboard()
18 cl.OpenClipboard()
15 cl.EmptyClipboard()
19 cl.EmptyClipboard()
16 cl.SetClipboardText( s )
20 cl.SetClipboardText( s )
17 cl.CloseClipboard()
21 cl.CloseClipboard()
18
22
19 if sys.platform == 'win32':
23 try:
24 import win32clipboard
20 toclip = toclip_w32
25 toclip = toclip_w32
21 else:
26 except ImportError:
22 def toclip(s): pass
27 def toclip(s): pass
23
28
24
29
25 def render(tmpl):
30 def render(tmpl):
26 """ Render a template (as specified in string.Template docs) from ipython variables
31 """ Render a template (as specified in string.Template docs) from ipython variables
27
32
28 Example:
33 Example:
29
34
30 $ import ipy_render
35 $ import ipy_render
31 $ my_name = 'Bob' # %store this for convenience
36 $ my_name = 'Bob' # %store this for convenience
32 $ t_submission_form = "Submission report, author: $my_name" # %store also
37 $ t_submission_form = "Submission report, author: $my_name" # %store also
33 $ render t_submission_form
38 $ render t_submission_form
34
39
35 => returns "Submission report, author: Bob" and copies to clipboard on win32
40 => returns "Submission report, author: Bob" and copies to clipboard on win32
36
41
37 """
42 """
38
43
39 s = Template(tmpl)
44 s = Template(tmpl)
40 res = s.substitute(ip.user_ns)
45 res = s.substitute(ip.user_ns)
41 toclip(res)
46 toclip(res)
42 return res
47 return res
43
48
44 ip.user_ns['render'] = render
49 ip.to_user_ns('render')
45 No newline at end of file
50
General Comments 0
You need to be logged in to leave comments. Login now