-
Notifications
You must be signed in to change notification settings - Fork 0
/
colt_rpc.py
280 lines (215 loc) · 9.26 KB
/
colt_rpc.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import sublime
import urllib, urllib.request
import json
import COLT.colt
import calendar, time
import os
import threading
from COLT.colt import ColtPreferences
runAfterAuthorization = None
statusToSet = "Disconnected from COLT"
class ColtConnection(object):
port = -1
messageId = 1
activeSessions = 0
def setStatus(status):
sublime.set_timeout(lambda: setStatus_(status), 0)
def setStatus_(status):
view = sublime.active_window().active_view()
if not view is None and not statusToSet is None :
view.erase_status("colt")
view.set_status("colt", status)
def coltStateUpdate():
if isConnected() :
ColtConnection.activeSessions = getActiveSessionsCount()
if ColtConnection.activeSessions > 0 :
# setStatus("COLT: " + str(ColtConnection.activeSessions) + " connections")
setStatus("[~] Connected to COLT")
else :
setStatus("Connected to COLT")
else :
setStatus("Disconnected from COLT")
def isConnected():
return ColtConnection.port != -1
def hasActiveSessions():
return ColtConnection.activeSessions > 0
def disconnect():
ColtConnection.port = -1
ColtConnection.messageId = 1
ColtConnection.activeSessions = 0
sublime.status_message("Disconnected from COLT")
def runAfterAuthorization():
if not runAfterAuthorization is None :
runAfterAuthorization()
runAfterAuthorization = None
return
def authorize(window):
if getSecurityToken() is None :
makeNewSecurityToken(True, window)
elif not runAfterAuthorization is None:
runAfterAuthorization()
def getSecurityToken():
settings = sublime.load_settings(ColtPreferences.NAME)
if not settings.has("securityToken") :
return None
return settings.get("securityToken")
def makeNewSecurityToken(newRequest, window):
onShortKeyInput('42')
# if newRequest :
# try :
# requestShortCode()
# except Exception :
# sublime.error_message("Authorization request failed. Make sure COLT is active and running.")
# return
#
# window.show_input_panel("Enter authorization code displayed in top of COLT window:", "", onShortKeyInput, None, None)
def onShortKeyInput(shortCode):
if shortCode :
try :
token = obtainAuthToken(shortCode)
if token is None :
sublime.error_message("Invalid short code entered")
authorize()
return
settings = sublime.load_settings(ColtPreferences.NAME)
settings.set("securityToken", token)
sublime.save_settings(ColtPreferences.NAME)
sublime.status_message("Successfully authorized with COLT")
runAfterAuthorization()
except Exception:
sublime.error_message("Unable to authorize with COLT. Make sure COLT is active and running")
return
else :
sublime.error_message("Short authorization key can't be empty")
authorize(sublime.active_window())
def obtainAuthToken(shortCode):
response = runRPC(ColtConnection.port, "obtainAuthToken", [ shortCode ])
if "error" in response :
return None
return response["result"]
def requestShortCode():
runRPC(ColtConnection.port, "requestShortCode", [ "Sublime Plugin" ])
def runRPC(port, methodName, params):
jsonRequest = None
messageId = ColtConnection.messageId
ColtConnection.messageId += 1
if (params is None) :
jsonRequest = { "jsonrpc" : "2.0", "method" : methodName, "id": messageId }
else :
jsonRequest = { "jsonrpc" : "2.0", "method" : methodName, "params": params, "id": messageId }
jsonRequestStr = json.dumps(jsonRequest)
url = "http://localhost:" + str(port) + "/rpc/coltService"
req = urllib.request.Request(url)
response = None
try :
response = urllib.request.urlopen(req, jsonRequestStr.encode('UTF-8'))
except Exception :
disconnect()
raise
return json.loads(response.read().decode("utf-8"))
def reload():
return runRPC(ColtConnection.port, "reload", [ getSecurityToken() ])
def clearLog():
return runRPC(ColtConnection.port, "clearLog", [ getSecurityToken() ])
def getMethodCounts():
return runRPC(ColtConnection.port, "getMethodCounts", [ getSecurityToken() ])
def getLastRuntimeError():
return runRPC(ColtConnection.port, "getLastRuntimeError", [ getSecurityToken() ])
def startLive():
securityToken = getSecurityToken()
if not getSecurityToken() is None :
try :
error = runRPC(ColtConnection.port, "startLive", [ securityToken ])["error"]["data"]["exceptionTypeName"]
if error == "codeOrchestra.colt.core.rpc.security.InvalidAuthTokenException" :
# auth code expired - we need new one
makeNewSecurityToken(True, sublime.active_window())
except KeyError :
# there was no error - good :)
return
def getState():
return runRPC(ColtConnection.port, "getState", None)
def getActiveSessionsCount():
try :
jsonState = getState()
return len(jsonState["result"]["activeConnections"])
except Exception :
return 0
def reloadScriptAt(filePath, position, currentContent):
return runRPC(ColtConnection.port, "reloadScriptAt", [ getSecurityToken(), filePath, position, currentContent ])
def getDeclarationPosition(filePath, position, currentContent):
return runRPC(ColtConnection.port, "getDeclarationPosition", [ getSecurityToken(), filePath, position, currentContent ])
def getContextForPosition(filePath, position, currentContent, contextType):
return runRPC(ColtConnection.port, "getContextForPosition", [ getSecurityToken(), filePath, position, currentContent, contextType ])
def evaluateExpression(filePath, expression, position, currentContent):
return runRPC(ColtConnection.port, "evaluateExpression", [ getSecurityToken(), filePath, expression, position, currentContent ])
def getCallCount(filePath, position, currentContent):
return runRPC(ColtConnection.port, "getCallCount", [ getSecurityToken(), filePath, position, currentContent ])
def resetCallCounts():
return runRPC(ColtConnection.port, "resetCallCounts", [ getSecurityToken() ])
def getEnclosingTagId(filePath, position, currentContent):
return runRPC(ColtConnection.port, "getEnclosingTagId", [ getSecurityToken(), filePath, position, currentContent ])
def findAndShowJavaDocs(filePath, position, currentContent):
return runRPC(ColtConnection.port, "findAndShowJavaDocs", [ getSecurityToken(), filePath, position, currentContent ])
def angularExpressionCompletion(tagId, leftExpression):
return runRPC(ColtConnection.port, "angularExpressionCompletion", [ getSecurityToken(), tagId, leftExpression ])
def angularDirectiveDeclaration(filePath, position, currentContent):
return runRPC(ColtConnection.port, "angularDirectiveDeclaration", [ getSecurityToken(), filePath, position, currentContent ])
def getLastLogMessages():
return runRPC(ColtConnection.port, "getLastLogMessages", [ getSecurityToken() ])
def getMethodId(filePath, position, currentContent):
resultJSON = runRPC(ColtConnection.port, "getMethodId", [ getSecurityToken(), filePath, position, currentContent ])
if "error" in resultJSON :
return None
return resultJSON["result"]
def runMethod(methodId):
runRPC(ColtConnection.port, "runMethod", [ getSecurityToken(), methodId ])
def establishConnection(port):
ColtConnection.port = port
sublime.status_message("Established connection with COLT on port " + port)
#time.sleep(2)
def initAndConnect(settings, projectPath):
sublime.status_message("Trying to establish connection with COLT...")
port = locateCOLTServicePort(projectPath)
if not port is None :
establishConnection(port)
return port
COLT.colt.runCOLT(settings, projectPath)
timeout = 20
while timeout > 0 :
time.sleep(0.3)
timeout -= 0.3
port = locateCOLTServicePort(projectPath)
if not port is None :
establishConnection(port)
return port
sublime.error_message("Can't establish connection with COLT")
return None
def locateCOLTServicePort(projectPath):
port = getRPCPortForProject(projectPath)
if port is None :
return None
try :
runRPC(port, "ping", None)
except Exception:
return None
return port
def getRPCPortForProject(projectPath):
storageDir = COLT.colt.getProjectWorkingDir(projectPath)
if storageDir is None :
return None
rpcInfoFilePath = storageDir + os.sep + "rpc.info"
if not os.path.exists(rpcInfoFilePath) :
return None
timePassedSinceModification = int(calendar.timegm(time.gmtime())) - int(os.path.getmtime(rpcInfoFilePath))
if (timePassedSinceModification > 2) :
return None
with open(rpcInfoFilePath, "r") as rpcInfoFile :
return rpcInfoFile.read().split(":")[1]
def set_interval(func, sec):
def func_wrapper():
set_interval(func, sec)
func()
t = threading.Timer(sec, func_wrapper)
t.start()
return t
set_interval(coltStateUpdate, 0.8)