-
Notifications
You must be signed in to change notification settings - Fork 2
/
StataLinux.py
67 lines (56 loc) · 2.44 KB
/
StataLinux.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
import sublime
import sublime_plugin
import subprocess
from os import remove
from os import path
from tempfile import mkstemp
settingsfile = "StataLinux (Linux).sublime-settings"
class StataLinuxCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Load settings
settings = sublime.load_settings(settingsfile)
always_extract_full_line = settings.get('always_extract_full_line')
remove_temp_file = settings.get('remove_temp_file')
# Collect selected range or full current line if no selection in a list
contents = []
for region in self.view.sel():
# select full line if region starts and ends at the same point
if always_extract_full_line or region.a == region.b:
region = self.view.full_line(region)
contents.append(self.view.substr(region))
# write contents into a temporary file
fd, filename = mkstemp(prefix="StataLinux_", suffix=".do")
with open(filename, "w") as file:
for content in contents:
file.write(content + "\n")
# Create and execute bash command:
sublime_stata_sh_path = path.join(sublime.packages_path(), "StataLinux", "sublime-stata.sh")
cmd = "sh " + sublime_stata_sh_path + " " + '"' + filename + '"'
ret = subprocess.call(cmd, shell = True)
if ret != 0:
if ret == 1:
sublime.error_message("Bash script returned error code %s.\nIt seems Stata is not running." % ret)
else:
sublime.error_message("Bash script returned error code %s." % ret)
# Remove temporary file:
if remove_temp_file: remove(filename)
# Print status message for debugging:
# sublime.status_message("Content:%s" % content)
sublime.status_message('StataLinuxCommand ran successfully')
class StataLinuxAllCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = sublime.load_settings(settingsfile)
# Switch focus to Stata or not after sending a command depending on a setting
if settings.get('save_before_run_all'):
self.view.run_command("save")
# Define current file as the one to be run, saving it first
filename = self.view.file_name()
# Create and execute bash command:
sublime_stata_sh_path = path.join(sublime.packages_path(), "StataLinux", "sublime-stata.sh")
cmd = "sh " + sublime_stata_sh_path + " " + '"' + filename + '"'
ret = subprocess.call(cmd, shell = True)
if ret != 0:
if ret == 1:
sublime.error_message("Bash script returned error code %s.\nIt seems Stata is not running." % ret)
else:
sublime.error_message("Bash script returned error code %s." % ret)