##// END OF EJS Templates
Merge pull request #1703 from takluyver/i211...
Min RK -
r6741:cb19d0db merge
parent child Browse files
Show More
@@ -1,59 +1,66 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Global exception classes for IPython.core.
3 Global exception classes for IPython.core.
4
4
5 Authors:
5 Authors:
6
6
7 * Brian Granger
7 * Brian Granger
8 * Fernando Perez
8 * Fernando Perez
9 * Min Ragan-Kelley
9 * Min Ragan-Kelley
10
10
11 Notes
11 Notes
12 -----
12 -----
13 """
13 """
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Copyright (C) 2008-2011 The IPython Development Team
16 # Copyright (C) 2008-2011 The IPython Development Team
17 #
17 #
18 # Distributed under the terms of the BSD License. The full license is in
18 # Distributed under the terms of the BSD License. The full license is in
19 # the file COPYING, distributed as part of this software.
19 # the file COPYING, distributed as part of this software.
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Imports
23 # Imports
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Exception classes
27 # Exception classes
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 class IPythonCoreError(Exception):
30 class IPythonCoreError(Exception):
31 pass
31 pass
32
32
33
33
34 class TryNext(IPythonCoreError):
34 class TryNext(IPythonCoreError):
35 """Try next hook exception.
35 """Try next hook exception.
36
36
37 Raise this in your hook function to indicate that the next hook handler
37 Raise this in your hook function to indicate that the next hook handler
38 should be used to handle the operation. If you pass arguments to the
38 should be used to handle the operation. If you pass arguments to the
39 constructor those arguments will be used by the next hook instead of the
39 constructor those arguments will be used by the next hook instead of the
40 original ones.
40 original ones.
41
42 A _msg argument will not be passed on, so it can be used as a displayable
43 error message.
41 """
44 """
42
45
43 def __init__(self, *args, **kwargs):
46 def __init__(self, _msg="", *args, **kwargs):
44 self.args = args
47 self.args = args
45 self.kwargs = kwargs
48 self.kwargs = kwargs
49 self.msg = _msg
50
51 def __str__(self):
52 return str(self.msg)
46
53
47 class UsageError(IPythonCoreError):
54 class UsageError(IPythonCoreError):
48 """Error in magic function arguments, etc.
55 """Error in magic function arguments, etc.
49
56
50 Something that probably won't warrant a full traceback, but should
57 Something that probably won't warrant a full traceback, but should
51 nevertheless interrupt a macro / batch file.
58 nevertheless interrupt a macro / batch file.
52 """
59 """
53
60
54 class StdinNotImplementedError(IPythonCoreError, NotImplementedError):
61 class StdinNotImplementedError(IPythonCoreError, NotImplementedError):
55 """raw_input was requested in a context where it is not supported
62 """raw_input was requested in a context where it is not supported
56
63
57 For use in IPython kernels, where only some frontends may support
64 For use in IPython kernels, where only some frontends may support
58 stdin requests.
65 stdin requests.
59 """
66 """
@@ -1,56 +1,56 b''
1 """ Utilities for accessing the platform's clipboard.
1 """ Utilities for accessing the platform's clipboard.
2 """
2 """
3
3
4 import subprocess
4 import subprocess
5 import sys
5 import sys
6
6
7 from IPython.core.error import TryNext
7 from IPython.core.error import TryNext
8
8
9
9
10 def win32_clipboard_get():
10 def win32_clipboard_get():
11 """ Get the current clipboard's text on Windows.
11 """ Get the current clipboard's text on Windows.
12
12
13 Requires Mark Hammond's pywin32 extensions.
13 Requires Mark Hammond's pywin32 extensions.
14 """
14 """
15 try:
15 try:
16 import win32clipboard
16 import win32clipboard
17 except ImportError:
17 except ImportError:
18 message = ("Getting text from the clipboard requires the pywin32 "
18 message = ("Getting text from the clipboard requires the pywin32 "
19 "extensions: http://sourceforge.net/projects/pywin32/")
19 "extensions: http://sourceforge.net/projects/pywin32/")
20 raise TryNext(message)
20 raise TryNext(_msg=message)
21 win32clipboard.OpenClipboard()
21 win32clipboard.OpenClipboard()
22 text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
22 text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
23 # FIXME: convert \r\n to \n?
23 # FIXME: convert \r\n to \n?
24 win32clipboard.CloseClipboard()
24 win32clipboard.CloseClipboard()
25 return text
25 return text
26
26
27 def osx_clipboard_get():
27 def osx_clipboard_get():
28 """ Get the clipboard's text on OS X.
28 """ Get the clipboard's text on OS X.
29 """
29 """
30 p = subprocess.Popen(['pbpaste', '-Prefer', 'ascii'],
30 p = subprocess.Popen(['pbpaste', '-Prefer', 'ascii'],
31 stdout=subprocess.PIPE)
31 stdout=subprocess.PIPE)
32 text, stderr = p.communicate()
32 text, stderr = p.communicate()
33 # Text comes in with old Mac \r line endings. Change them to \n.
33 # Text comes in with old Mac \r line endings. Change them to \n.
34 text = text.replace('\r', '\n')
34 text = text.replace('\r', '\n')
35 return text
35 return text
36
36
37 def tkinter_clipboard_get():
37 def tkinter_clipboard_get():
38 """ Get the clipboard's text using Tkinter.
38 """ Get the clipboard's text using Tkinter.
39
39
40 This is the default on systems that are not Windows or OS X. It may
40 This is the default on systems that are not Windows or OS X. It may
41 interfere with other UI toolkits and should be replaced with an
41 interfere with other UI toolkits and should be replaced with an
42 implementation that uses that toolkit.
42 implementation that uses that toolkit.
43 """
43 """
44 try:
44 try:
45 import Tkinter
45 import Tkinter
46 except ImportError:
46 except ImportError:
47 message = ("Getting text from the clipboard on this platform "
47 message = ("Getting text from the clipboard on this platform "
48 "requires Tkinter.")
48 "requires Tkinter.")
49 raise TryNext(message)
49 raise TryNext(_msg=message)
50 root = Tkinter.Tk()
50 root = Tkinter.Tk()
51 root.withdraw()
51 root.withdraw()
52 text = root.clipboard_get()
52 text = root.clipboard_get()
53 root.destroy()
53 root.destroy()
54 return text
54 return text
55
55
56
56
General Comments 0
You need to be logged in to leave comments. Login now