-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodic.py
126 lines (95 loc) · 3.67 KB
/
codic.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
import sublime
import sublime_plugin
import urllib.request
import urllib.parse
import json
class codicTranslateStringCommand(sublime_plugin.TextCommand):
def run(self, edit, casing=None, project_id=None, input_word=None):
input_flag = False
if input_word is None:
sels = self.view.sel()
for sel in sels:
selection_word = self.view.substr(sel)
if selection_word is None or selection_word == '':
sublime.active_window().run_command('codic_input_string')
else:
selection_word = input_word
input_flag = True
if selection_word == '':
sublime.status_message('No word was selected.')
return
casing = setCasingSetting(casing)
acronym_style = getAcronymStyleSetting()
url = getApiUrl()+'/engine/translate.json'
values = {
'text': selection_word,
'casing': casing,
'acronym_style': acronym_style
}
if project_id is not None:
values['project_id'] = project_id
headers = getAutorizationHeader()
req = requestApi(url, values, headers)
with urllib.request.urlopen(req) as response:
the_page = response.read()
data = json.loads(the_page.decode('utf-8'))
if data[0]['successful']:
result = data[0]['translated_text']
if input_flag:
self.view.insert(edit, self.view.sel()[0].a, result)
else:
self.view.replace(edit, sel, result)
class codicGetProjectIdsCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.items = []
url = getApiUrl()+'/user_projects.json'
values = {
}
headers = getAutorizationHeader()
req = requestApi(url, values, headers)
with urllib.request.urlopen(req) as response:
the_page = response.read()
data = json.loads(the_page.decode('utf-8'))
for res_data in data:
proj_id = str(res_data['id'])
name = res_data['name']
desc = res_data['description']
self.items.append([proj_id, name, desc])
self.window = sublime.active_window()
self.window.show_quick_panel(self.items, self.on_done)
def on_done(self, index):
proj_id = self.items[index][0]
self.window.run_command("codic_replace_word", {
"casing": "camel", "project_id": proj_id
})
class codicInputStringCommand(sublime_plugin.TextCommand):
"""docstring for inputTranslationWord"""
def run(self, edit):
self.window = sublime.active_window()
self.window.show_input_panel("codic input string", "", self.on_done, self.on_change, self.on_cancel)
def on_done(self, word):
self.window.run_command("codic_translate_string", {
"input_word": word
})
def on_cancel(self):
pass
def on_change(self):
pass
def requestApi(url, values, headers):
data = urllib.parse.urlencode(values)
full_url = url+'?'+data
req = urllib.request.Request(full_url, None, headers, None, False, 'GET')
return req
def getSettings():
return sublime.load_settings("SublimeCodic.sublime-settings")
def getApiUrl():
return getSettings().get('api_url')
def getAutorizationHeader():
access_token = getSettings().get('access_token')
return {'Authorization': 'Bearer '+access_token}
def setCasingSetting(casing):
if casing is None:
return getSettings().get('casing')
return casing
def getAcronymStyleSetting():
return getSettings().get('acronym_style')