v1.2.1 fixed #14

This commit is contained in:
Alan Hamlett 2013-08-14 02:36:17 -07:00
parent 09c9252b9b
commit dda5da361c

View File

@ -5,7 +5,7 @@ Maintainer: WakaTi.me <support@wakatime.com>
Website: https://www.wakati.me/ Website: https://www.wakati.me/
===========================================================""" ==========================================================="""
__version__ = '1.2.0' __version__ = '1.2.1'
import sublime import sublime
import sublime_plugin import sublime_plugin
@ -29,8 +29,8 @@ SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {} SETTINGS = {}
LAST_ACTION = 0 LAST_ACTION = 0
LAST_FILE = None LAST_FILE = None
BUSY = False
HAS_SSL = False HAS_SSL = False
LOCK = threading.RLock()
# check if we have SSL support # check if we have SSL support
try: try:
@ -70,24 +70,20 @@ def setup_settings_file():
sublime.save_settings(SETTINGS_FILE) sublime.save_settings(SETTINGS_FILE)
def get_api_key(): def prompt_api_key():
"""If api key not set, prompt user to enter one then save
to WakaTime.sublime-settings.
"""
global SETTINGS global SETTINGS
api_key = SETTINGS.get('api_key', '') if not SETTINGS.get('api_key'):
if not api_key:
def got_key(text): def got_key(text):
if text: if text:
api_key = str(text) SETTINGS.set('api_key', str(text))
SETTINGS.set('api_key', api_key)
sublime.save_settings(SETTINGS_FILE) sublime.save_settings(SETTINGS_FILE)
window = sublime.active_window() window = sublime.active_window()
if 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: else:
print('Error: Could not prompt for api key because no window found.') print('Error: Could not prompt for api key because no window found.')
return api_key return False
def python_binary(): def python_binary():
@ -111,7 +107,8 @@ def enough_time_passed(now):
def handle_write_action(view): def handle_write_action(view):
global LAST_FILE, LAST_ACTION global LOCK, LAST_FILE, LAST_ACTION
with LOCK:
targetFile = view.file_name() targetFile = view.file_name()
thread = SendActionThread(targetFile, isWrite=True) thread = SendActionThread(targetFile, isWrite=True)
thread.start() thread.start()
@ -120,7 +117,8 @@ def handle_write_action(view):
def handle_normal_action(view): def handle_normal_action(view):
global LAST_FILE, LAST_ACTION global LOCK, LAST_FILE, LAST_ACTION
with LOCK:
targetFile = view.file_name() targetFile = view.file_name()
thread = SendActionThread(targetFile) thread = SendActionThread(targetFile)
thread.start() thread.start()
@ -136,7 +134,7 @@ class SendActionThread(threading.Thread):
self.isWrite = isWrite self.isWrite = isWrite
self.force = force self.force = force
self.debug = SETTINGS.get('debug') self.debug = SETTINGS.get('debug')
self.api_key = get_api_key() self.api_key = SETTINGS.get('api_key', '')
def run(self): def run(self):
if self.targetFile: if self.targetFile:
@ -145,7 +143,6 @@ class SendActionThread(threading.Thread):
self.send() self.send()
def send(self): def send(self):
time.sleep(5)
if not self.api_key: if not self.api_key:
print('missing api key') print('missing api key')
return return
@ -174,6 +171,12 @@ class SendActionThread(threading.Thread):
def plugin_loaded(): def plugin_loaded():
setup_settings_file() 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 # need to call plugin_loaded because only ST3 will auto-call it
@ -184,22 +187,10 @@ if ST_VERSION < 3000:
class WakatimeListener(sublime_plugin.EventListener): class WakatimeListener(sublime_plugin.EventListener):
def on_post_save(self, view): def on_post_save(self, view):
global BUSY
if not BUSY:
BUSY = True
handle_write_action(view) handle_write_action(view)
BUSY = False
def on_activated(self, view): def on_activated(self, view):
global BUSY
if not BUSY:
BUSY = True
handle_normal_action(view) handle_normal_action(view)
BUSY = False
def on_modified(self, view): def on_modified(self, view):
global BUSY
if not BUSY:
BUSY = True
handle_normal_action(view) handle_normal_action(view)
BUSY = False