Merge pull request #162 from rchl/fix/jq-check

Fix check for whether jq is installed in the system
This commit is contained in:
Terminal 2022-05-30 23:47:24 -05:00 committed by GitHub
commit f844dfd85e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 25 deletions

View File

@ -1,8 +1,10 @@
import decimal import decimal
import os import os
import functools
import re import re
import subprocess import subprocess
import shutil import shutil
import webbrowser
from xml.etree import ElementTree as et from xml.etree import ElementTree as et
import sublime import sublime
@ -18,26 +20,10 @@ PREVIOUS_QUERY_LEN = int()
xml_syntax = "Packages/XML/XML.sublime-syntax" xml_syntax = "Packages/XML/XML.sublime-syntax"
json_syntax = "Packages/JSON/JSON.sublime-syntax" json_syntax = "Packages/JSON/JSON.sublime-syntax"
jq_exists = bool()
jq_init = bool()
jq_path = str()
def get_jq_path():
def check_jq():
global jq_init, jq_exists, jq_path
settings = sublime.load_settings("Pretty JSON.sublime-settings") settings = sublime.load_settings("Pretty JSON.sublime-settings")
return shutil.which(settings.get("jq_binary", "jq"))
if jq_init:
return
jq_init = True
jq_test = settings.get("jq_binary", "jq")
try:
jq_path = shutil.which(jq_test)
jq_exists = True
except OSError as ex:
sublime.message_dialog(f"[Error]: {ex}")
jq_exists = False
class PrettyJsonBaseCommand: class PrettyJsonBaseCommand:
@ -467,21 +453,24 @@ class JqQueryPrettyJson(sublime_plugin.WindowCommand):
return self.is_enabled() return self.is_enabled()
def run(self): def run(self):
check_jq() jq_path = get_jq_path()
if jq_exists: if jq_path:
preview_view = self.window.active_view() preview_view = self.window.active_view()
preview_view.run_command("jq_pretty_json") preview_view.run_command("jq_pretty_json")
sublime.active_window().show_input_panel( sublime.active_window().show_input_panel(
"Enter ./jq filter expression", "Enter ./jq filter expression",
".", ".",
self.done, self.done,
self.send_query, functools.partial(self.send_query, jq_path),
None, None,
) )
else: else:
sublime.status_message( if sublime.ok_cancel_dialog(
"./jq tool is not available on your system. http://stedolan.github.io/jq" "./jq tool is not available on your system. Do you want to open the jq website?",
) "Open JQ Website"
):
webbrowser.open("http://stedolan.github.io/jq")
def get_content(self): def get_content(self):
"""returns content of active view or selected region""" """returns content of active view or selected region"""
@ -497,7 +486,7 @@ class JqQueryPrettyJson(sublime_plugin.WindowCommand):
selection = region selection = region
return view.substr(selection) return view.substr(selection)
def send_query(self, query: str): def send_query(self, jq_path: str, query: str):
global PREVIOUS_CONTENT, PREVIOUS_QUERY_LEN global PREVIOUS_CONTENT, PREVIOUS_QUERY_LEN
settings = sublime.load_settings("Pretty JSON.sublime-settings") settings = sublime.load_settings("Pretty JSON.sublime-settings")
try: try: