sublime-wakatime/WakaTime.py

271 lines
7.8 KiB
Python
Raw Normal View History

""" ==========================================================
2013-08-05 09:30:24 +04:00
File: WakaTime.py
Description: Automatic time tracking for Sublime Text 2 and 3.
2014-03-06 01:55:08 +04:00
Maintainer: WakaTime <support@wakatime.com>
2014-03-13 04:03:14 +04:00
License: BSD, see LICENSE for more details.
Website: https://wakatime.com/
==========================================================="""
2013-07-02 13:21:13 +04:00
2015-04-01 02:21:05 +03:00
__version__ = '3.0.14'
2013-07-02 13:21:13 +04:00
import sublime
import sublime_plugin
2013-07-24 02:18:31 +04:00
import glob
import os
import platform
import sys
2013-07-02 13:21:13 +04:00
import time
import threading
import webbrowser
from datetime import datetime
from subprocess import Popen
2013-07-02 13:21:13 +04:00
# globals
ACTION_FREQUENCY = 2
ST_VERSION = int(sublime.version())
PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__))
API_CLIENT = os.path.join(PLUGIN_DIR, 'packages', 'wakatime', 'cli.py')
SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {}
LAST_ACTION = {
'time': 0,
'file': None,
'is_write': False,
}
2013-08-14 13:36:17 +04:00
LOCK = threading.RLock()
PYTHON_LOCATION = None
# add wakatime package to path
sys.path.insert(0, os.path.join(PLUGIN_DIR, 'packages'))
try:
from wakatime.base import parseConfigFile
except ImportError:
pass
def createConfigFile():
"""Creates the .wakatime.cfg INI file in $HOME directory, if it does
not already exist.
"""
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
try:
2014-12-27 03:18:52 +03:00
with open(configFile) as fh:
pass
except IOError:
try:
2014-12-27 03:18:52 +03:00
with open(configFile, 'w') as fh:
fh.write("[settings]\n")
fh.write("debug = false\n")
fh.write("hidefilenames = false\n")
except IOError:
pass
2013-08-14 13:36:17 +04:00
def prompt_api_key():
global SETTINGS
createConfigFile()
default_key = ''
2015-03-10 01:23:29 +03:00
try:
configs = parseConfigFile()
if configs is not None:
if configs.has_option('settings', 'api_key'):
default_key = configs.get('settings', 'api_key')
except:
pass
if SETTINGS.get('api_key'):
return True
else:
def got_key(text):
if text:
2013-08-14 13:36:17 +04:00
SETTINGS.set('api_key', str(text))
sublime.save_settings(SETTINGS_FILE)
window = sublime.active_window()
if window:
window.show_input_panel('[WakaTime] Enter your wakatime.com api key:', default_key, got_key, None, None)
2013-08-14 13:36:17 +04:00
return True
else:
print('[WakaTime] Error: Could not prompt for api key because no window found.')
2013-08-14 13:36:17 +04:00
return False
def python_binary():
global PYTHON_LOCATION
if PYTHON_LOCATION is not None:
return PYTHON_LOCATION
paths = [
"pythonw",
"python",
"/usr/local/bin/python",
"/usr/bin/python",
]
for path in paths:
try:
Popen([path, '--version'])
PYTHON_LOCATION = path
return path
except:
pass
for path in glob.iglob('/python*'):
path = os.path.realpath(os.path.join(path, 'pythonw'))
try:
Popen([path, '--version'])
PYTHON_LOCATION = path
return path
except:
pass
return None
def enough_time_passed(now, last_time):
if now - last_time > ACTION_FREQUENCY * 60:
return True
return False
2014-06-18 21:35:59 +04:00
def find_project_name_from_folders(folders):
try:
for folder in folders:
for file_name in os.listdir(folder):
if file_name.endswith('.sublime-project'):
return file_name.replace('.sublime-project', '', 1)
except:
pass
2014-06-18 21:35:59 +04:00
return None
def handle_action(view, is_write=False):
window = view.window()
if window is not None:
target_file = view.file_name()
project = window.project_file_name() if hasattr(window, 'project_file_name') else None
folders = window.folders()
thread = SendActionThread(target_file, view, is_write=is_write, project=project, folders=folders)
thread.start()
class SendActionThread(threading.Thread):
def __init__(self, target_file, view, is_write=False, project=None, folders=None, force=False):
threading.Thread.__init__(self)
self.lock = LOCK
self.target_file = target_file
self.is_write = is_write
self.project = project
2014-06-18 21:35:59 +04:00
self.folders = folders
self.force = force
self.debug = SETTINGS.get('debug')
2013-08-14 13:36:17 +04:00
self.api_key = SETTINGS.get('api_key', '')
self.ignore = SETTINGS.get('ignore', [])
self.last_action = LAST_ACTION.copy()
self.view = view
def run(self):
with self.lock:
if self.target_file:
self.timestamp = time.time()
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_heartbeat(self):
if not self.api_key:
print('[WakaTime] Error: missing api key.')
return
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
cmd = [
API_CLIENT,
'--file', self.target_file,
'--time', str('%f' % self.timestamp),
'--plugin', ua,
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
]
if self.is_write:
cmd.append('--write')
if self.project:
self.project = os.path.basename(self.project).replace('.sublime-project', '', 1)
if self.project:
cmd.extend(['--project', self.project])
2014-06-18 21:35:59 +04:00
elif self.folders:
project_name = find_project_name_from_folders(self.folders)
if project_name:
cmd.extend(['--project', project_name])
for pattern in self.ignore:
cmd.extend(['--ignore', pattern])
if self.debug:
cmd.append('--verbose')
if python_binary():
cmd.insert(0, python_binary())
2015-03-10 01:25:40 +03:00
if self.debug:
print('[WakaTime] %s' % ' '.join(cmd))
if platform.system() == 'Windows':
Popen(cmd, shell=False)
else:
with open(os.path.join(os.path.expanduser('~'), '.wakatime.log'), 'a') as stderr:
Popen(cmd, stderr=stderr)
self.sent()
else:
print('[WakaTime] Error: Unable to find python binary.')
def sent(self):
2015-03-24 20:05:23 +03:00
sublime.set_timeout(self.set_status_bar, 0)
sublime.set_timeout(self.set_last_action, 0)
2015-03-24 20:05:23 +03:00
def set_status_bar(self):
if SETTINGS.get('status_bar_message'):
self.view.set_status('wakatime', 'WakaTime active {0}'.format(datetime.now().strftime('%I:%M %p')))
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():
global SETTINGS
print('[WakaTime] Initializing WakaTime plugin v%s' % __version__)
if not python_binary():
sublime.error_message("Unable to find Python binary!\nWakaTime needs Python to work correctly.\n\nGo to https://www.python.org/downloads")
return
SETTINGS = sublime.load_settings(SETTINGS_FILE)
2013-08-14 13:36:17 +04:00
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
if ST_VERSION < 3000:
plugin_loaded()
2013-07-02 13:21:13 +04:00
class WakatimeListener(sublime_plugin.EventListener):
def on_post_save(self, view):
handle_action(view, is_write=True)
2013-07-02 13:21:13 +04:00
def on_activated(self, view):
handle_action(view)
def on_modified(self, view):
handle_action(view)
class WakatimeDashboardCommand(sublime_plugin.ApplicationCommand):
def run(self):
webbrowser.open_new_tab('https://wakatime.com/dashboard')