forked from gtarawneh/languagetool-sublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LTServer.py
40 lines (35 loc) · 846 Bytes
/
LTServer.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
import sublime
import json
def _is_ST2():
return (int(sublime.version()) < 3000)
if _is_ST2():
from urllib import urlencode
from urllib import urlopen
else:
try:
from urlparse import urlencode
from urllib2 import urlopen
except ImportError:
from urllib.parse import urlencode
from urllib.request import urlopen
def getResponse(server, text, language, disabledRules):
payload = {
'language': language,
'text': text.encode('utf8'),
'User-Agent': 'sublime',
'disabledRules' : ','.join(disabledRules)
}
content = _post(server, payload)
if content:
j = json.loads(content.decode('utf-8'))
return j['matches']
else:
return None
# internal functions:
def _post(server, payload):
data = urlencode(payload).encode('utf8')
try:
content = urlopen(server, data).read()
return content
except IOError:
return None