Compare commits

..

8 Commits
0.4.2 ... 1.2.1

2 changed files with 41 additions and 65 deletions

View File

@ -5,7 +5,7 @@ Maintainer: WakaTi.me <support@wakatime.com>
Website: https://www.wakati.me/
==========================================================="""
__version__ = '0.4.2'
__version__ = '1.2.1'
import sublime
import sublime_plugin
@ -29,21 +29,21 @@ 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
# import wakatime package
if HAS_SSL:
# import wakatime package
sys.path.insert(0, join(PLUGIN_DIR, 'packages', 'wakatime'))
import wakatime
except ImportError:
from subprocess import Popen
def setup_settings_file():
""" Convert ~/.wakatime.conf to WakaTime.sublime-settings
@ -70,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():
@ -111,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):
@ -127,33 +133,29 @@ 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:
@ -169,6 +171,12 @@ class SendActionThread(threading.Thread):
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
@ -179,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)

View File

@ -1,20 +0,0 @@
{
"schema_version": "1.2",
"packages": [
{
"name": "WakaTime",
"description": "Automatic time tracking for Sublime Text 2 & 3",
"author": "wakati.me",
"homepage": "https://github.com/wakatime/sublime-wakatime",
"last_modified": "2013-08-09 02:22",
"platforms": {
"*": [
{
"version": "0.4.2",
"url": "https://codeload.github.com/wakatime/sublime-wakatime/zip/0.4.2"
}
]
}
}
]
}