1
1
""" Pymode utils. """
2
- import json
3
2
import os .path
4
3
import sys
5
4
import threading
18
17
PY2 = sys .version_info [0 ]== 2
19
18
20
19
21
- def pymode_message (content ):
22
- """ Show message. """
23
-
24
- vim .command ('call pymode#wide_message("%s")' % str (content ))
25
-
26
-
27
- def pymode_confirm (yes = True ,msg = 'Do the changes:' ):
28
- """ Confirmation.
29
-
30
- :return bool:
31
-
32
- """
33
- default = 'yes' if yes else 'no'
34
- action = pymode_input (msg ,default )
35
- return action and 'yes' .startswith (action )
36
-
37
-
38
- def pymode_inputlist (msg ,opts ):
39
- """ Get user choice.
40
-
41
- :return str: A choosen option
42
-
43
- """
44
- choices = ['[Pymode] %s' % msg ]
45
- choices += ["%s. %s" % (num ,opt )for num ,opt in enumerate (opts ,1 )]
46
- try :
47
- input_str = int (vim .eval ('inputlist(%s)' % json .dumps (choices )))
48
- except (KeyboardInterrupt ,ValueError ):
49
- input_str = 0
50
-
51
- if not input_str :
52
- pymode_message ('Cancelled!' )
53
- return False
54
-
55
- try :
56
- return opts [input_str - 1 ]
57
- except (IndexError ,ValueError ):
58
- pymode_error ('Invalid option: %s' % input_str )
59
- return pymode_inputlist (msg ,opts )
60
-
61
-
62
- def pymode_input (umsg ,udefault = '' ,opts = None ):
63
- """ Get user input.
64
-
65
- :return str: A user input
66
-
67
- """
68
- msg = '[Pymode] %s ' % umsg
69
- default = udefault
70
-
71
- if default != '' :
72
- msg += '[%s] ' % default
73
-
74
- try :
75
- vim .command ('echohl Debug' )
76
- input_str = vim .eval ('input("%s> ")' % msg )
77
- vim .command ('echohl none' )
78
- except KeyboardInterrupt :
79
- input_str = ''
80
-
81
- return input_str or default
82
-
83
-
84
- def pymode_error (content ):
85
- """ Show error. """
86
-
87
- vim .command ('call pymode#error("%s")' % str (content ))
88
-
89
-
90
- def catch_and_print_exceptions (func ):
91
- """ Catch any exception.
92
-
93
- :return func:
94
-
95
- """
96
- def wrapper (* args ,** kwargs ):
97
- try :
98
- return func (* args ,** kwargs )
99
- except (Exception ,vim .error )as e :# noqa
100
- if DEBUG :
101
- raise
102
- pymode_error (e )
103
- return None
104
- return wrapper
105
-
106
-
107
20
@contextmanager
108
21
def silence_stderr ():
109
22
""" Redirect stderr. """
@@ -127,12 +40,3 @@ def patch_paths():
127
40
sys .path .insert (0 ,os .path .join (os .path .dirname (__file__ ),'libs2' ))
128
41
else :
129
42
sys .path .insert (0 ,os .path .join (os .path .dirname (__file__ ),'libs3' ))
130
-
131
-
132
- debug = lambda _ :None
133
-
134
- if DEBUG :
135
- def debug (msg ):# noqa
136
- """ Debug message. """
137
-
138
- print (msg )