/
util.py
148 lines (122 loc) · 4.55 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import vdebug.opts
import vdebug.log
import vim
import sys
import re
class Keymapper:
"""Map and unmap key commands for the Vim user interface.
"""
exclude = ["run","set_breakpoint","eval_visual"]
def __init__(self):
self.keymaps = vim.eval("g:vdebug_keymap")
self.leader = vim.eval("g:vdebug_leader_key")
self.is_mapped = False
self.existing = []
def map(self):
if self.is_mapped:
return
for func in self.keymaps:
key = self.keymaps[func]
if func not in self.exclude:
vim.command("redir @z | silent map %s%s | redir END" %(self.leader,key))
self.__save_map_output( vim.eval("@z").strip() )
map_cmd = "map %s%s :python debugger.%s()<cr>" %\
(self.leader,key,func)
vim.command(map_cmd)
self.is_mapped = True
def __save_map_output(self,output):
if output.startswith('No mapping'):
return False
else:
vdebug.log.Log("Storing existing key mapping, '%s' " % output,\
vdebug.log.Logger.DEBUG)
self.existing.append(output)
return True
def unmap(self):
if self.is_mapped:
self.is_mapped = False
for func in self.keymaps:
key = self.keymaps[func]
if func not in self.exclude:
vim.command("unmap %s%s" %(self.leader,key))
for mapping in self.existing:
vdebug.log.Log("Remapping key with '%s' " % mapping,\
vdebug.log.Logger.DEBUG)
vim.command("map %s" % mapping)
class FilePath:
"""Normalizes a file name and allows for remote and local path mapping.
"""
def __init__(self,filename):
if filename is None or \
len(filename) == 0:
raise FilePathError, "Missing or invalid file name"
if filename.startswith('file:///'):
if sys.platform == "win32":
""" remove prefix till the drive letter """
filename = filename[8:]
else:
filename = filename[7:]
self.local = self._create_local(filename)
self.remote = self._create_remote(filename)
def _create_local(self,f):
"""Create the file name as a locally valid version.
Uses the "local_path" and "remote_path" options.
"""
ret = f
if vdebug.opts.Options.isset('path_maps'):
for remote, local in vdebug.opts.Options.get('path_maps', dict).items():
if ret.startswith(remote):
vdebug.log.Log("Replacing remote path (%s) " % remote +\
"with local path (%s)" % local ,\
vdebug.log.Logger.DEBUG)
ret = ret.replace(remote,local)
break
return ret
def _create_remote(self,f):
"""Create the file name valid for the remote server.
Uses the "local_path" and "remote_path" options.
"""
ret = f
if vdebug.opts.Options.isset('path_maps'):
for remote, local in vdebug.opts.Options.get('path_maps', dict).items():
if ret.startswith(local):
vdebug.log.Log("Replacing local path (%s) " % local +\
"with remote path (%s)" % remote ,\
vdebug.log.Logger.DEBUG)
ret = ret.replace(local,remote)
break
return ret
def as_local(self):
return self.local
def as_remote(self):
return self.remote
def __eq__(self,other):
if isinstance(other,FilePath):
if other.as_local() == self.as_local():
return True
return False
def __ne__(self,other):
if isinstance(other,FilePath):
if other.as_local() == self.as_local():
return False
return True
def __add__(self,other):
return self.as_local() + other
def __radd__(self,other):
return other + self.as_local()
def __str__(self):
return self.as_local()
def __repr__(self):
return str(self)
class FilePathError(Exception):
pass
class InputStream:
"""Get a character from Vim's input stream.
Used to check for keyboard interrupts."""
def probe(self):
try:
vim.eval("getchar(0)")
except vim.error:
raise UserInterrupt
class UserInterrupt(Exception):
"""Raised when a user interrupts connection wait."""