Refactored pretty much everything.
I finally got around to making CLI calls happen async. This means things no longer lock up when the plugin is doing things.
This commit is contained in:
parent
d44e982585
commit
634cb564b0
148
beau.py
148
beau.py
|
|
@ -1,13 +1,15 @@
|
||||||
import json
|
import json
|
||||||
import platform
|
import platform
|
||||||
import sublime
|
|
||||||
import sublime_plugin
|
import sublime_plugin
|
||||||
from http.client import responses
|
from http.client import responses
|
||||||
from subprocess import Popen, PIPE
|
from sublime import load_settings, active_window
|
||||||
|
from subprocess import check_output
|
||||||
|
|
||||||
SETTINGS_FILE = 'Beau.sublime-settings'
|
SETTINGS_FILE = 'Beau.sublime-settings'
|
||||||
SYNTAX = 'Packages/JavaScript/JSON.sublime-syntax'
|
SYNTAX = 'Packages/JavaScript/JSON.sublime-syntax'
|
||||||
|
|
||||||
|
is_windows = (platform.system() == 'Windows')
|
||||||
|
|
||||||
class InsertTextCommand(sublime_plugin.TextCommand):
|
class InsertTextCommand(sublime_plugin.TextCommand):
|
||||||
def run(self, edit, text):
|
def run(self, edit, text):
|
||||||
self.view.insert(edit, 0, text)
|
self.view.insert(edit, 0, text)
|
||||||
|
|
@ -16,124 +18,86 @@ class BeauCommand(sublime_plugin.TextCommand):
|
||||||
requests = []
|
requests = []
|
||||||
path = ''
|
path = ''
|
||||||
|
|
||||||
|
def inThread(self, command, onComplete):
|
||||||
|
def thread(command, onComplete):
|
||||||
|
proc = check_output(command, shell=is_windows)
|
||||||
|
onComplete(proc)
|
||||||
|
return
|
||||||
|
|
||||||
|
thread = threading.Thread(target=thread, args=(command, onComplete))
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
return thread
|
||||||
|
|
||||||
def run(self, edit):
|
def run(self, edit):
|
||||||
settings = sublime.load_settings(SETTINGS_FILE)
|
settings = load_settings(SETTINGS_FILE)
|
||||||
self.path = settings.get('cli_path', '')
|
self.path = settings.get('cli_path', '')
|
||||||
active_window = sublime.active_window()
|
active_view = active_window().active_view()
|
||||||
active_view = active_window.active_view()
|
|
||||||
|
|
||||||
scope = active_view.scope_name(active_view.sel()[0].b)
|
scope = active_view.scope_name(active_view.sel()[0].b)
|
||||||
if not scope.startswith('source.yaml'):
|
if not scope.startswith('source.yaml'):
|
||||||
active_window.status_message('Beau can only be ran on yaml files.')
|
active_window().status_message('Beau can only be ran on yaml files.')
|
||||||
return
|
return
|
||||||
|
|
||||||
print('Using ' + self.path)
|
self.inThread(
|
||||||
print([
|
[self.path, '-c', active_view.file_name(), 'list', '--no-format'],
|
||||||
self.path,
|
self.listFetched
|
||||||
'-c',
|
)
|
||||||
active_view.file_name(),
|
|
||||||
'list',
|
|
||||||
'--no-format'
|
|
||||||
])
|
|
||||||
|
|
||||||
proc = Popen([
|
|
||||||
self.path,
|
|
||||||
'-c',
|
|
||||||
active_view.file_name(),
|
|
||||||
'list',
|
|
||||||
'--no-format'
|
|
||||||
], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=self.is_windows())
|
|
||||||
|
|
||||||
for line in iter(proc.stderr.readline, b''):
|
|
||||||
print(line)
|
|
||||||
active_window.status_message(line.decode("utf-8"))
|
|
||||||
|
|
||||||
|
def listFetched(self, list):
|
||||||
requests = []
|
requests = []
|
||||||
self.requests[:] = []
|
self.requests[:] = []
|
||||||
for line in iter(proc.stdout.readline, b''):
|
for line in list.splitlines():
|
||||||
req = line.decode('utf-8').rstrip().split('\t')
|
req = line.decode('utf-8').rstrip().split('\t')
|
||||||
|
method, alias, endpoint = req
|
||||||
|
requests.append([alias, endpoint])
|
||||||
|
self.requests.append(req)
|
||||||
|
|
||||||
if len(req) == 3:
|
active_window().show_quick_panel(requests, self.on_request_selected)
|
||||||
method, alias, endpoint = req
|
|
||||||
requests.append([alias, endpoint])
|
|
||||||
self.requests.append(req)
|
|
||||||
|
|
||||||
elif len(req) == 5:
|
def on_request_selected(self, index):
|
||||||
method, alias, endpoint, title, description = req
|
|
||||||
self.requests.append([method, alias, endpoint])
|
|
||||||
|
|
||||||
if description == 'undefined':
|
|
||||||
description = endpoint
|
|
||||||
|
|
||||||
if title == 'undefined':
|
|
||||||
title = alias
|
|
||||||
else:
|
|
||||||
title = title + ' (' + alias + ')'
|
|
||||||
|
|
||||||
requests.append([title, description])
|
|
||||||
|
|
||||||
proc.wait()
|
|
||||||
active_window.show_quick_panel(requests, self.on_done)
|
|
||||||
|
|
||||||
def on_done(self, index):
|
|
||||||
if index == -1:
|
if index == -1:
|
||||||
return
|
return
|
||||||
|
|
||||||
active_window = sublime.active_window()
|
active_view = active_window().active_view()
|
||||||
active_view = active_window.active_view()
|
|
||||||
|
|
||||||
method, alias, endpoint = self.requests[index]
|
method, alias, endpoint = self.requests[index]
|
||||||
|
|
||||||
active_window.status_message('Executing: ' + alias)
|
active_window().status_message('Running: ' + alias)
|
||||||
|
|
||||||
print([
|
def handleResult(result):
|
||||||
self.path,
|
response = []
|
||||||
'-c',
|
for line in result.splitlines():
|
||||||
active_view.file_name(),
|
response.append(line.rstrip())
|
||||||
'request',
|
|
||||||
alias,
|
|
||||||
'--no-format'
|
|
||||||
])
|
|
||||||
|
|
||||||
proc = Popen([
|
active_window().status_message('')
|
||||||
self.path,
|
|
||||||
'-c',
|
|
||||||
active_view.file_name(),
|
|
||||||
'request',
|
|
||||||
alias,
|
|
||||||
'--no-format'
|
|
||||||
], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=self.is_windows())
|
|
||||||
|
|
||||||
for line in iter(proc.stderr.readline, b''):
|
status, endpoint, headers, body = response
|
||||||
print(line)
|
|
||||||
active_window.status_message(line.decode("utf-8"))
|
|
||||||
|
|
||||||
response = []
|
status = status.decode('utf-8')
|
||||||
for line in iter(proc.stdout.readline, b''):
|
endpoint = endpoint.decode('utf-8')
|
||||||
response.append(line.rstrip())
|
headers = headers.decode('utf-8')
|
||||||
|
body = body.decode('utf-8')
|
||||||
|
|
||||||
active_window.status_message('')
|
results_view = active_window().new_file()
|
||||||
|
results_view.set_name('Results: ' + alias)
|
||||||
|
|
||||||
status, endpoint, headers, body = response
|
content = method + ' ' + endpoint + '\n'
|
||||||
|
content += status + ' ' + responses[int(status)] + '\n\n'
|
||||||
|
|
||||||
status = status.decode('utf-8')
|
content += 'Response Headers: \n'
|
||||||
endpoint = endpoint.decode('utf-8')
|
content += self.autoindent(headers)
|
||||||
headers = headers.decode('utf-8')
|
|
||||||
body = body.decode('utf-8')
|
|
||||||
|
|
||||||
results_view = active_window.new_file()
|
content += '\n\nResponse Body: \n'
|
||||||
results_view.set_name('Results: ' + alias)
|
content += self.autoindent(body)
|
||||||
|
|
||||||
content = method + ' ' + endpoint + '\n'
|
results_view.run_command('insert_text', {'text': content})
|
||||||
content += status + ' ' + responses[int(status)] + '\n\n'
|
results_view.set_scratch(True)
|
||||||
content += 'Response Headers: \n'
|
results_view.set_syntax_file(SYNTAX)
|
||||||
content += self.autoindent(headers)
|
|
||||||
content += '\n\nResponse Body: \n'
|
|
||||||
content += self.autoindent(body)
|
|
||||||
|
|
||||||
results_view.run_command('insert_text', {'text': content})
|
self.inThread(
|
||||||
results_view.set_scratch(True)
|
[self.path, '-c', active_view.file_name(), 'request', alias, '--no-format'],
|
||||||
results_view.set_syntax_file(SYNTAX)
|
onComplete=handleResult
|
||||||
|
)
|
||||||
|
|
||||||
def autoindent(self, obj):
|
def autoindent(self, obj):
|
||||||
parsed = json.loads(obj)
|
parsed = json.loads(obj)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue