-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFormatter.py
More file actions
82 lines (53 loc) · 2.46 KB
/
DataFormatter.py
File metadata and controls
82 lines (53 loc) · 2.46 KB
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
import sublime, sublime_plugin
import sys, os, subprocess, threading
# The command to call to start the data formatter
class DataFormatterCommand(sublime_plugin.TextCommand):
def run(self, edit):
if sys.platform.startswith("win"):
sublime.message_dialog("data_formatter doesn't work yet on Windows")
return
filepath = self.view.file_name()
#filedir = os.path.dirname(filepath)
# Read the settings
prefs = sublime.load_settings("Preferences.sublime-settings")
buildenv = prefs.get("build_env", None)
# Run runscript with the dataformatter script
args = [ "wh", "dev:rewrite", filepath, "-" ]
# Create an environment
current_env = os.environ.copy()
if buildenv:
current_env["PATH"] = buildenv.get("PATH", current_env["PATH"])
# Initialize and start the thread running the actual search
thread = DataFormatterThread(self.view, args, current_env)
thread.start()
# The thread running the dataformatter script and displaying the output
class DataFormatterThread(threading.Thread):
def __init__(self, buffer, args, env):
threading.Thread.__init__(self)
# Store attributes
self.buffer = buffer
self.cmd = args
self.env = env
def run(self):
# Start the Data Formatter process
proc = subprocess.Popen(self.cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=self.env)
# Read process output
data = proc.communicate()
output = data[1].decode("utf-8") if data[1] else data[0].decode("utf-8")
# Currently, the output is either AnyToString-formatted JSON/RECORD ARRAY with errors or XML
output_format = None
if output.startswith("JSON:") or output.startswith("+RECORD ARRAY"):
output_format = "Packages/WebHare/AnyToString.sublime-syntax"
elif output.startswith("<"):
output_format = "Packages/XML/XML.sublime-syntax"
self.set_buffer({ "text": output, "format": output_format })
def set_buffer(self, args):
self.buffer.run_command("set_data_formatter_result", args)
# This TextCommand is used to add text to the Data Formatter results view (necessary to obtain an edit token)
class SetDataFormatterResultCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
# If a format is specified, set it
if "format" in args and args["format"]:
self.view.set_syntax_file(args["format"])
# Replace all contents with the supplied text
self.view.replace(edit, sublime.Region(0, self.view.size()), args["text"])