status message showing when WakaTime is enabled

This commit is contained in:
Alan Hamlett 2015-03-20 01:14:53 -07:00
parent d16d1ca747
commit 510eea0a8b

View File

@ -18,6 +18,7 @@ import sys
import time import time
import threading import threading
import webbrowser import webbrowser
from datetime import datetime
from os.path import expanduser, dirname, basename, realpath, join from os.path import expanduser, dirname, basename, realpath, join
# globals # globals
@ -149,26 +150,17 @@ def find_project_name_from_folders(folders):
def handle_action(view, is_write=False): def handle_action(view, is_write=False):
global LOCK, LAST_ACTION target_file = view.file_name()
with LOCK: project = view.window().project_file_name() if hasattr(view.window(), 'project_file_name') else None
target_file = view.file_name() thread = SendActionThread(target_file, view, is_write=is_write, project=project, folders=view.window().folders())
if target_file: thread.start()
project = view.window().project_file_name() if hasattr(view.window(), 'project_file_name') else None
if project:
project = basename(project).replace('.sublime-project', '', 1)
thread = SendActionThread(target_file, is_write=is_write, project=project, folders=view.window().folders())
thread.start()
LAST_ACTION = {
'file': target_file,
'time': time.time(),
'is_write': is_write,
}
class SendActionThread(threading.Thread): class SendActionThread(threading.Thread):
def __init__(self, target_file, is_write=False, project=None, folders=None, force=False): def __init__(self, target_file, view, is_write=False, project=None, folders=None, force=False):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.lock = LOCK
self.target_file = target_file self.target_file = target_file
self.is_write = is_write self.is_write = is_write
self.project = project self.project = project
@ -177,15 +169,17 @@ class SendActionThread(threading.Thread):
self.debug = SETTINGS.get('debug') self.debug = SETTINGS.get('debug')
self.api_key = SETTINGS.get('api_key', '') self.api_key = SETTINGS.get('api_key', '')
self.ignore = SETTINGS.get('ignore', []) self.ignore = SETTINGS.get('ignore', [])
self.last_action = LAST_ACTION self.last_action = LAST_ACTION.copy()
self.view = view
def run(self): def run(self):
if self.target_file: with self.lock:
self.timestamp = time.time() if self.target_file:
if self.force or (self.is_write and not self.last_action['is_write']) or self.target_file != self.last_action['file'] or enough_time_passed(self.timestamp, self.last_action['time']): self.timestamp = time.time()
self.send() if self.force or (self.is_write and not self.last_action['is_write']) or self.target_file != self.last_action['file'] or enough_time_passed(self.timestamp, self.last_action['time']):
self.send_heartbeat()
def send(self): def send_heartbeat(self):
if not self.api_key: if not self.api_key:
print('[WakaTime] Error: missing api key.') print('[WakaTime] Error: missing api key.')
return return
@ -199,6 +193,8 @@ class SendActionThread(threading.Thread):
] ]
if self.is_write: if self.is_write:
cmd.append('--write') cmd.append('--write')
if self.project:
self.project = basename(self.project).replace('.sublime-project', '', 1)
if self.project: if self.project:
cmd.extend(['--project', self.project]) cmd.extend(['--project', self.project])
elif self.folders: elif self.folders:
@ -215,6 +211,8 @@ class SendActionThread(threading.Thread):
code = wakatime.main(cmd) code = wakatime.main(cmd)
if code != 0: if code != 0:
print('[WakaTime] Error: Response code %d from wakatime package.' % code) print('[WakaTime] Error: Response code %d from wakatime package.' % code)
else:
self.sent()
else: else:
python = python_binary() python = python_binary()
if python: if python:
@ -226,9 +224,22 @@ class SendActionThread(threading.Thread):
else: else:
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr: with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
Popen(cmd, stderr=stderr) Popen(cmd, stderr=stderr)
self.sent()
else: else:
print('[WakaTime] Error: Unable to find python binary.') print('[WakaTime] Error: Unable to find python binary.')
def sent(self):
sublime.set_timeout(lambda: self.view.set_status('wakatime', 'WakaTime heartbeat sent {0}'.format(datetime.now().strftime('%A %b %d %Y at %I:%M %p %Z'))), 0)
sublime.set_timeout(self.set_last_action, 0)
def set_last_action(self):
global LAST_ACTION
LAST_ACTION = {
'file': self.target_file,
'time': self.timestamp,
'is_write': self.is_write,
}
def plugin_loaded(): def plugin_loaded():
global SETTINGS global SETTINGS