-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgtm-plugin.py
More file actions
125 lines (101 loc) · 4.03 KB
/
gtm-plugin.py
File metadata and controls
125 lines (101 loc) · 4.03 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
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
import sublime
import sublime_plugin
import sys
import time
import os
import subprocess
import re
gtm_settings = {}
def find_gtm_path():
if sys.platform == 'win32':
exe = 'gtm.exe'
path_sep = ";"
pf = os.path.join(os.environ.get("ProgramFiles", ""), "gtm")
pfx86 = os.path.join(os.environ.get("ProgramFiles(x86)", ""), "gtm")
default_path = pf + path_sep + pfx86
else:
exe = 'gtm'
path_sep = ":"
default_path = "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/"
env_path = set(os.environ['PATH'].split(path_sep))
paths = env_path.union(set(default_path.split(path_sep)))
if os.path.isfile(exe):
return exe
else:
for p in paths:
f = os.path.join(p, exe)
if os.path.isfile(f):
return f
return None
def plugin_loaded():
global gtm_settings
gtm_settings = sublime.load_settings('gtm.sublime-settings')
gtm_settings.add_on_change('gtm_status_bar', set_status_bar)
set_status_bar()
def set_status_bar():
if gtm_settings.get('gtm_status_bar', True):
GTM.status_option = '--status'
print("Enabling reporting time in status bar")
else:
GTM.status_option = ''
print("Disabling reporting time in status bar")
class GTM(sublime_plugin.EventListener):
gtm_ver_req = '>= 1.2.5'
update_interval = 30
last_update = 0
last_path = None
status_option = ''
no_gtm_err = ("GTM executable not found.\n\n"
"Install GTM and/or update your system path. "
"Make sure to restart Sublime after install.\n\n"
"See https://github.com/git-time-metric/gtm/blob/master/README.md")
record_err = ("GTM error saving time.\n\n"
"Install GTM and/or update the system path. "
"Make sure to restart Sublime after install.\n\n"
"See https://github.com/git-time-metric/gtm/blob/master/README.md")
ver_warn = ("GTM executable is out of date.\n\n"
"The plug-in may not work properly. "
"Please install the latest GTM version and restart Sublime.\n\n"
"See https://github.com/git-time-metric/gtm/blob/master/README.md")
gtm_path = find_gtm_path()
if not gtm_path:
sublime.error_message(no_gtm_err)
else:
p = subprocess.Popen('"{0}" verify "{1}"'.format(gtm_path, gtm_ver_req),
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
output = p.stdout.read()
version_ok = 'true' in output.decode('utf-8')
if not version_ok:
sublime.error_message(ver_warn)
def on_post_save_async(self, view):
self.record(view, view.file_name())
def on_modified_async(self, view):
self.record(view, view.file_name())
def on_selection_modified_async(self, view):
self.record(view, view.file_name())
def on_activated_async(self, view):
self.record(view, view.file_name())
def record(self, view, path):
if GTM.gtm_path and path and (
path != GTM.last_path or
time.time() - GTM.last_update > GTM.update_interval ):
GTM.last_update = time.time()
GTM.last_path = path
cmd = '"{0}" record {1} "{2}"'.format(GTM.gtm_path,
GTM.status_option,
path)
try:
cmd_output = subprocess.check_output(cmd, shell=True)
if GTM.status_option:
view.set_status(
"gtm-statusbar",
GTM.format_status(cmd_output))
else:
view.erase_status("gtm-statusbar")
except subprocess.CalledProcessError as e:
sublime.error_message(GTM.record_err)
def format_status(t):
return t.decode('utf-8').strip()