-
Notifications
You must be signed in to change notification settings - Fork 1
/
ITDCHelperTools.py
261 lines (180 loc) · 6.73 KB
/
ITDCHelperTools.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
# @package ITDCHelper
# @author Avtandil Kikabidze aka LONGMAN
# @copyright Copyright (c) 2008-2015, Avtandil Kikabidze ([email protected])
# @link http://long.ge
# @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
import sublime, sublime_plugin, os, re, sys
directory = os.path.dirname(os.path.realpath(__file__))
libs_path = os.path.join(directory, "itdchelper")
if libs_path not in sys.path:
sys.path.append(libs_path)
import threading
from random import sample
import time
from urllib.parse import quote
from functions import *
from panel import ItdchelperProjectPanel
from request import ITDCRequest
from loading import ItdchelperLoading
from commands import (EraseCommand)
### Chmod path
class ItdchelperToolsChmodCommand(sublime_plugin.TextCommand):
chmods = ['0777', '0755']
path = None
recursively = False
def run(self, edit, paths, recursively):
path = self.get_first(paths)
if (not path):
show_error('Path is empty')
self.path = path
self.recursively = recursively
items = []
item = []
item.append('Chmod 777 (rwx-rwx-rwx)')
item.append(self.path)
items.append(item)
item = []
item.append('Chmod 755 (rwxr-xr-x)')
item.append(self.path)
items.append(item)
self.view.window().show_quick_panel(items, self.on_done_final)
return
def on_done_final(self, choice):
if (choice == -1):
return
try:
chmod = self.chmods[choice]
except IndexError:
return
if (not self.path):
return
thread = ItdchelperToolsChmodProcess(self.path, chmod, self.recursively, self.view)
thread.start()
def get_first(self, iterable, default=None):
if iterable:
for item in iterable:
return item
return default
def is_enabled(self):
ret = True
return ret
class ItdchelperToolsChmodProcess(threading.Thread):
settings = None
localservice_url = ''
path = ''
recursively = False
chmod = False
view = None
window = None
start_time = None
def __init__(self, path, chmod, recursively, pview):
self.settings = sublime.load_settings('ITDCHelper.sublime-settings')
self.start_time = time.time()
self.path = path
if (recursively is True):
self.recursively = 1
else:
self.recursively = 0
self.chmod = chmod
self.view = pview
self.window = self.view.window()
self.localservice_url = 'http://tools.local.itdc.ge/project/chmod.php'
threading.Thread.__init__(self)
def run(self):
if not hasattr(self, 'panel'):
self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperToolsChmod', 'Chmod Path: "'+self.path+'"'+"\n")
self.panel.append('Chmodding path "'+self.path+'"')
self.panel.append('Loading')
thread2 = ItdchelperLoading(self.panel)
thread2.start()
service_url = self.localservice_url + '?mode=chmod&chmod='+self.chmod+'&recursively='+str(self.recursively)+'&path='+self.path
#print(service_url)
#self.panel.append('DEBUG: request to '+service_url)
request = ITDCRequest(service_url)
if (request.isError()):
thread2.stop()
stderr = "Error: "+request.getError()
elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
txt = stderr + elapsed
self.panel.replace(txt)
self.panel.finish()
return False
json_data = request.getJSON()
#self.panel.append(' Success', False)
thread2.stop()
elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
status = json_data['status']
msg = json_data['msg'] + elapsed
if (status == "ERROR"):
msg = "Error: "+msg
self.panel.replace(msg)
self.panel.finish()
def isRunning(self):
return self.running
### CMD
class ItdchelperToolsCmdCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel('CMD:', '', lambda s: self.on_done(s), None, None)
return
def on_done(self, cmd):
if (len(cmd) < 1):
show_error('Command is empty!')
return
thread = ItdchelperToolsCmdProcess(cmd, self.view)
thread.start()
return
def is_enabled(self):
settings = sublime.load_settings('ITDCHelper.sublime-settings')
cmd_enabled = settings.get('cmd_enabled', 0)
if (cmd_enabled):
return True
return False
class ItdchelperToolsCmdProcess(threading.Thread):
settings = None
localservice_url = ''
path = ''
recursively = False
chmod = False
view = None
window = None
start_time = None
def __init__(self, cmd, pview):
self.settings = sublime.load_settings('ITDCHelper.sublime-settings')
self.start_time = time.time()
self.cmd = cmd
self.view = pview
self.window = self.view.window()
self.localservice_url = 'http://tools.local.itdc.ge/project/cmd.php'
threading.Thread.__init__(self)
def run(self):
if not hasattr(self, 'panel'):
self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperToolsCmd', 'Command: "'+self.cmd+'"'+"\n")
self.panel.append('Executing command')
self.panel.append('Loading')
thread2 = ItdchelperLoading(self.panel)
thread2.start()
cmd = quote(self.cmd)
service_url = self.localservice_url + '?cmd='+cmd
#print(service_url)
#self.panel.append('DEBUG: request to '+service_url)
request = ITDCRequest(service_url)
if (request.isError()):
thread2.stop()
stderr = "Error: "+request.getError()
elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
txt = stderr + elapsed
self.panel.replace(txt)
self.panel.finish()
return False
json_data = request.getJSON()
#self.panel.append(' Success', False)
thread2.stop()
elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
status = json_data['status']
msg = json_data['msg'] + elapsed
if (status == "ERROR"):
msg = "Error: "+msg
self.panel.replace(msg)
self.panel.finish()
def isRunning(self):
return self.running