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/ Website: https://www.wakati.me/
===========================================================""" ==========================================================="""
__version__ = '0.4.2' __version__ = '1.2.1'
import sublime import sublime
import sublime_plugin import sublime_plugin
@ -29,21 +29,21 @@ 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:
import ssl import ssl
HAS_SSL = True HAS_SSL = True
except ImportError:
from subprocess import Popen
# import wakatime package # import wakatime package
if HAS_SSL:
sys.path.insert(0, join(PLUGIN_DIR, 'packages', 'wakatime')) sys.path.insert(0, join(PLUGIN_DIR, 'packages', 'wakatime'))
import wakatime import wakatime
except ImportError:
from subprocess import Popen
def setup_settings_file(): def setup_settings_file():
""" Convert ~/.wakatime.conf to WakaTime.sublime-settings """ Convert ~/.wakatime.conf to WakaTime.sublime-settings
@ -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,13 +107,23 @@ def enough_time_passed(now):
def handle_write_action(view): def handle_write_action(view):
thread = SendActionThread(view.file_name(), isWrite=True) global LOCK, LAST_FILE, LAST_ACTION
thread.start() 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): def handle_normal_action(view):
thread = SendActionThread(view.file_name()) global LOCK, LAST_FILE, LAST_ACTION
thread.start() with LOCK:
targetFile = view.file_name()
thread = SendActionThread(targetFile)
thread.start()
LAST_FILE = targetFile
LAST_ACTION = time.time()
class SendActionThread(threading.Thread): class SendActionThread(threading.Thread):
@ -127,33 +133,29 @@ class SendActionThread(threading.Thread):
self.targetFile = targetFile self.targetFile = targetFile
self.isWrite = isWrite self.isWrite = isWrite
self.force = force self.force = force
self.debug = SETTINGS.get('debug')
self.api_key = SETTINGS.get('api_key', '')
def run(self): def run(self):
sublime.set_timeout(self.check, 0)
def check(self):
global LAST_ACTION, LAST_FILE
if self.targetFile: if self.targetFile:
self.timestamp = time.time() self.timestamp = time.time()
if self.force or self.isWrite or self.targetFile != LAST_FILE or enough_time_passed(self.timestamp): 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() self.send()
def send(self): def send(self):
api_key = get_api_key() if not self.api_key:
if not api_key: print('missing api key')
return return
cmd = [ cmd = [
API_CLIENT, API_CLIENT,
'--file', self.targetFile, '--file', self.targetFile,
'--time', str('%f' % self.timestamp), '--time', str('%f' % self.timestamp),
'--plugin', 'sublime-wakatime/%s' % __version__, '--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: if self.isWrite:
cmd.append('--write') cmd.append('--write')
if SETTINGS.get('debug'): if self.debug:
cmd.append('--verbose') cmd.append('--verbose')
print(cmd) print(cmd)
if HAS_SSL: if HAS_SSL:
@ -169,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
@ -179,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 handle_write_action(view)
if not BUSY:
BUSY = True
handle_write_action(view)
BUSY = False
def on_activated(self, view): def on_activated(self, view):
global BUSY handle_normal_action(view)
if not BUSY:
BUSY = True
handle_normal_action(view)
BUSY = False
def on_modified(self, view): def on_modified(self, view):
global BUSY handle_normal_action(view)
if not BUSY:
BUSY = True
handle_normal_action(view)
BUSY = False

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"
}
]
}
}
]
}