Compare commits

..

4 Commits
1.1.0 ... 1.2.1

View File

@ -5,7 +5,7 @@ Maintainer: WakaTi.me <support@wakatime.com>
Website: https://www.wakati.me/
==========================================================="""
__version__ = '1.1.0'
__version__ = '1.2.1'
import sublime
import sublime_plugin
@ -29,19 +29,20 @@ SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {}
LAST_ACTION = 0
LAST_FILE = None
BUSY = False
HAS_SSL = False
LOCK = threading.RLock()
# check if we have SSL support
try:
import ssl
HAS_SSL = True
except ImportError:
#from subprocess import Popen
pass
# import wakatime package
sys.path.insert(0, join(PLUGIN_DIR, 'packages', 'wakatime'))
import wakatime
# import wakatime package
sys.path.insert(0, join(PLUGIN_DIR, 'packages', 'wakatime'))
import wakatime
except ImportError:
from subprocess import Popen
def setup_settings_file():
@ -69,24 +70,20 @@ def setup_settings_file():
sublime.save_settings(SETTINGS_FILE)
def get_api_key():
"""If api key not set, prompt user to enter one then save
to WakaTime.sublime-settings.
"""
def prompt_api_key():
global SETTINGS
api_key = SETTINGS.get('api_key', '')
if not api_key:
if not SETTINGS.get('api_key'):
def got_key(text):
if text:
api_key = str(text)
SETTINGS.set('api_key', api_key)
SETTINGS.set('api_key', str(text))
sublime.save_settings(SETTINGS_FILE)
window = sublime.active_window()
if window:
window.show_input_panel('Enter your WakaTi.me api key:', '', got_key, None, None)
window.show_input_panel('Enter your WakaTime api key:', '', got_key, None, None)
return True
else:
print('Error: Could not prompt for api key because no window found.')
return api_key
return False
def python_binary():
@ -110,13 +107,23 @@ def enough_time_passed(now):
def handle_write_action(view):
thread = SendActionThread(view.file_name(), isWrite=True)
thread.start()
global LOCK, LAST_FILE, LAST_ACTION
with LOCK:
targetFile = view.file_name()
thread = SendActionThread(targetFile, isWrite=True)
thread.start()
LAST_FILE = targetFile
LAST_ACTION = time.time()
def handle_normal_action(view):
thread = SendActionThread(view.file_name())
thread.start()
global LOCK, LAST_FILE, LAST_ACTION
with LOCK:
targetFile = view.file_name()
thread = SendActionThread(targetFile)
thread.start()
LAST_FILE = targetFile
LAST_ACTION = time.time()
class SendActionThread(threading.Thread):
@ -126,48 +133,50 @@ class SendActionThread(threading.Thread):
self.targetFile = targetFile
self.isWrite = isWrite
self.force = force
self.debug = SETTINGS.get('debug')
self.api_key = SETTINGS.get('api_key', '')
def run(self):
sublime.set_timeout(self.check, 0)
def check(self):
global LAST_ACTION, LAST_FILE
if self.targetFile:
self.timestamp = time.time()
if self.force or self.isWrite or self.targetFile != LAST_FILE or enough_time_passed(self.timestamp):
LAST_FILE = self.targetFile
LAST_ACTION = self.timestamp
self.send()
def send(self):
api_key = get_api_key()
if not api_key:
if not self.api_key:
print('missing api key')
return
cmd = [
API_CLIENT,
'--file', self.targetFile,
'--time', str('%f' % self.timestamp),
'--plugin', 'sublime-wakatime/%s' % __version__,
'--key', str(bytes.decode(api_key.encode('utf8'))),
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
]
if self.isWrite:
cmd.append('--write')
if SETTINGS.get('debug'):
if self.debug:
cmd.append('--verbose')
print(cmd)
#if HAS_SSL:
wakatime.main(cmd)
"""else:
if HAS_SSL:
wakatime.main(cmd)
else:
cmd.insert(0, python_binary())
if platform.system() == 'Windows':
Popen(cmd, shell=False)
else:
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
Popen(cmd, stderr=stderr)"""
Popen(cmd, stderr=stderr)
def plugin_loaded():
setup_settings_file()
after_loaded()
def after_loaded():
if not prompt_api_key():
sublime.set_timeout(after_loaded, 500)
# need to call plugin_loaded because only ST3 will auto-call it
@ -178,22 +187,10 @@ if ST_VERSION < 3000:
class WakatimeListener(sublime_plugin.EventListener):
def on_post_save(self, view):
global BUSY
if not BUSY:
BUSY = True
handle_write_action(view)
BUSY = False
handle_write_action(view)
def on_activated(self, view):
global BUSY
if not BUSY:
BUSY = True
handle_normal_action(view)
BUSY = False
handle_normal_action(view)
def on_modified(self, view):
global BUSY
if not BUSY:
BUSY = True
handle_normal_action(view)
BUSY = False
handle_normal_action(view)