2013-07-08 05:38:01 +04:00
|
|
|
""" ==========================================================
|
2016-10-24 13:41:46 +03:00
|
|
|
File: WakaTime.py
|
2013-07-08 05:38:01 +04:00
|
|
|
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.
|
2014-03-15 00:36:20 +04:00
|
|
|
Website: https://wakatime.com/
|
2013-07-08 05:38:01 +04:00
|
|
|
==========================================================="""
|
2013-07-02 13:21:13 +04:00
|
|
|
|
2015-04-01 02:19:30 +03:00
|
|
|
|
2017-11-16 05:36:43 +03:00
|
|
|
__version__ = '8.0.2'
|
2013-07-02 13:21:13 +04:00
|
|
|
|
2015-04-01 02:19:30 +03:00
|
|
|
|
2013-07-23 23:09:58 +04:00
|
|
|
import sublime
|
|
|
|
import sublime_plugin
|
|
|
|
|
2016-11-11 13:29:51 +03:00
|
|
|
import contextlib
|
2016-04-29 01:18:38 +03:00
|
|
|
import json
|
2013-08-05 09:11:22 +04:00
|
|
|
import os
|
2013-07-23 23:09:58 +04:00
|
|
|
import platform
|
2015-10-02 01:07:55 +03:00
|
|
|
import re
|
2013-08-08 13:17:47 +04:00
|
|
|
import sys
|
2013-07-02 13:21:13 +04:00
|
|
|
import time
|
2013-08-08 13:17:47 +04:00
|
|
|
import threading
|
2016-11-11 13:29:51 +03:00
|
|
|
import traceback
|
2015-08-01 01:19:56 +03:00
|
|
|
import urllib
|
2015-02-24 09:20:27 +03:00
|
|
|
import webbrowser
|
2015-03-20 11:14:53 +03:00
|
|
|
from datetime import datetime
|
2016-01-02 09:20:02 +03:00
|
|
|
from subprocess import Popen, STDOUT, PIPE
|
2016-11-11 13:29:51 +03:00
|
|
|
from zipfile import ZipFile
|
2015-10-02 01:07:55 +03:00
|
|
|
try:
|
|
|
|
import _winreg as winreg # py2
|
|
|
|
except ImportError:
|
2015-10-02 01:14:36 +03:00
|
|
|
try:
|
|
|
|
import winreg # py3
|
|
|
|
except ImportError:
|
|
|
|
winreg = None
|
2016-04-29 01:18:38 +03:00
|
|
|
try:
|
|
|
|
import Queue as queue # py2
|
|
|
|
except ImportError:
|
|
|
|
import queue # py3
|
2015-04-01 02:19:30 +03:00
|
|
|
|
2013-07-02 13:21:13 +04:00
|
|
|
|
2016-01-02 09:20:02 +03:00
|
|
|
is_py2 = (sys.version_info[0] == 2)
|
|
|
|
is_py3 = (sys.version_info[0] == 3)
|
|
|
|
|
|
|
|
if is_py2:
|
|
|
|
def u(text):
|
|
|
|
if text is None:
|
|
|
|
return None
|
2017-02-09 06:24:26 +03:00
|
|
|
if isinstance(text, unicode):
|
|
|
|
return text
|
2016-01-02 09:20:02 +03:00
|
|
|
try:
|
|
|
|
return text.decode('utf-8')
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
return text.decode(sys.getdefaultencoding())
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
return unicode(text)
|
|
|
|
except:
|
2017-01-10 21:05:24 +03:00
|
|
|
try:
|
|
|
|
return text.decode('utf-8', 'replace')
|
2017-02-09 06:24:26 +03:00
|
|
|
except:
|
|
|
|
try:
|
|
|
|
return unicode(str(text))
|
|
|
|
except:
|
|
|
|
return unicode('')
|
2016-01-02 09:20:02 +03:00
|
|
|
|
|
|
|
elif is_py3:
|
|
|
|
def u(text):
|
|
|
|
if text is None:
|
|
|
|
return None
|
|
|
|
if isinstance(text, bytes):
|
|
|
|
try:
|
|
|
|
return text.decode('utf-8')
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
return text.decode(sys.getdefaultencoding())
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
return str(text)
|
|
|
|
except:
|
2016-09-22 11:24:20 +03:00
|
|
|
return text.decode('utf-8', 'replace')
|
2016-01-02 09:20:02 +03:00
|
|
|
|
|
|
|
else:
|
|
|
|
raise Exception('Unsupported Python version: {0}.{1}.{2}'.format(
|
|
|
|
sys.version_info[0],
|
|
|
|
sys.version_info[1],
|
|
|
|
sys.version_info[2],
|
|
|
|
))
|
|
|
|
|
|
|
|
|
2013-07-08 05:38:01 +04:00
|
|
|
# globals
|
2015-05-06 22:25:52 +03:00
|
|
|
HEARTBEAT_FREQUENCY = 2
|
2013-08-08 13:17:47 +04:00
|
|
|
ST_VERSION = int(sublime.version())
|
2015-04-01 02:19:30 +03:00
|
|
|
PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
API_CLIENT = os.path.join(PLUGIN_DIR, 'packages', 'wakatime', 'cli.py')
|
2013-08-08 13:17:47 +04:00
|
|
|
SETTINGS_FILE = 'WakaTime.sublime-settings'
|
|
|
|
SETTINGS = {}
|
2015-05-06 22:25:52 +03:00
|
|
|
LAST_HEARTBEAT = {
|
2013-11-28 15:16:43 +04:00
|
|
|
'time': 0,
|
|
|
|
'file': None,
|
|
|
|
'is_write': False,
|
|
|
|
}
|
2015-03-10 00:32:27 +03:00
|
|
|
PYTHON_LOCATION = None
|
2016-04-29 01:18:38 +03:00
|
|
|
HEARTBEATS = queue.Queue()
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2014-12-24 21:01:56 +03:00
|
|
|
|
2015-10-02 22:09:37 +03:00
|
|
|
# Log Levels
|
|
|
|
DEBUG = 'DEBUG'
|
|
|
|
INFO = 'INFO'
|
|
|
|
WARNING = 'WARNING'
|
|
|
|
ERROR = 'ERROR'
|
|
|
|
|
|
|
|
|
2015-04-01 02:19:30 +03:00
|
|
|
# add wakatime package to path
|
|
|
|
sys.path.insert(0, os.path.join(PLUGIN_DIR, 'packages'))
|
2013-08-08 13:41:29 +04:00
|
|
|
try:
|
2016-11-11 13:16:17 +03:00
|
|
|
from wakatime.main import parseConfigFile
|
2015-04-01 02:19:30 +03:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2013-08-13 07:30:06 +04:00
|
|
|
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2016-04-29 01:18:38 +03:00
|
|
|
def set_timeout(callback, seconds):
|
|
|
|
"""Runs the callback after the given seconds delay.
|
|
|
|
|
|
|
|
If this is Sublime Text 3, runs the callback on an alternate thread. If this
|
|
|
|
is Sublime Text 2, runs the callback in the main thread.
|
|
|
|
"""
|
|
|
|
|
|
|
|
milliseconds = int(seconds * 1000)
|
|
|
|
try:
|
|
|
|
sublime.set_timeout_async(callback, milliseconds)
|
|
|
|
except AttributeError:
|
|
|
|
sublime.set_timeout(callback, milliseconds)
|
|
|
|
|
|
|
|
|
2015-10-02 22:09:37 +03:00
|
|
|
def log(lvl, message, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
if lvl == DEBUG and not SETTINGS.get('debug'):
|
|
|
|
return
|
|
|
|
msg = message
|
|
|
|
if len(args) > 0:
|
|
|
|
msg = message.format(*args)
|
|
|
|
elif len(kwargs) > 0:
|
|
|
|
msg = message.format(**kwargs)
|
2016-09-23 09:36:23 +03:00
|
|
|
try:
|
|
|
|
print('[WakaTime] [{lvl}] {msg}'.format(lvl=lvl, msg=msg))
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
print(u('[WakaTime] [{lvl}] {msg}').format(lvl=lvl, msg=u(msg)))
|
2015-10-02 22:09:37 +03:00
|
|
|
except RuntimeError:
|
2016-04-29 01:18:38 +03:00
|
|
|
set_timeout(lambda: log(lvl, message, *args, **kwargs), 0)
|
|
|
|
|
|
|
|
|
2016-05-12 13:07:24 +03:00
|
|
|
def resources_folder():
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
return os.path.join(os.getenv('APPDATA'), 'WakaTime')
|
|
|
|
else:
|
|
|
|
return os.path.join(os.path.expanduser('~'), '.wakatime')
|
|
|
|
|
|
|
|
|
2016-04-29 01:18:38 +03:00
|
|
|
def update_status_bar(status):
|
|
|
|
"""Updates the status bar."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
if SETTINGS.get('status_bar_message'):
|
|
|
|
msg = datetime.now().strftime(SETTINGS.get('status_bar_message_fmt'))
|
|
|
|
if '{status}' in msg:
|
|
|
|
msg = msg.format(status=status)
|
|
|
|
|
|
|
|
active_window = sublime.active_window()
|
|
|
|
if active_window:
|
|
|
|
for view in active_window.views():
|
|
|
|
view.set_status('wakatime', msg)
|
|
|
|
except RuntimeError:
|
|
|
|
set_timeout(lambda: update_status_bar(status), 0)
|
2015-10-02 22:09:37 +03:00
|
|
|
|
|
|
|
|
2013-08-14 13:36:17 +04:00
|
|
|
def prompt_api_key():
|
2013-08-08 13:17:47 +04:00
|
|
|
global SETTINGS
|
2014-12-24 21:01:56 +03:00
|
|
|
|
2017-03-16 18:36:17 +03:00
|
|
|
if SETTINGS.get('api_key'):
|
|
|
|
return True
|
2014-12-24 21:01:56 +03:00
|
|
|
|
|
|
|
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
|
2014-12-24 21:01:56 +03:00
|
|
|
|
2017-03-16 18:36:17 +03:00
|
|
|
window = sublime.active_window()
|
|
|
|
if window:
|
2013-08-05 09:11:22 +04:00
|
|
|
def got_key(text):
|
|
|
|
if text:
|
2013-08-14 13:36:17 +04:00
|
|
|
SETTINGS.set('api_key', str(text))
|
2013-08-08 13:17:47 +04:00
|
|
|
sublime.save_settings(SETTINGS_FILE)
|
2017-03-16 18:36:17 +03:00
|
|
|
window.show_input_panel('[WakaTime] Enter your wakatime.com api key:', default_key, got_key, None, None)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
log(ERROR, 'Could not prompt for api key because no window found.')
|
|
|
|
return False
|
2013-07-10 11:14:44 +04:00
|
|
|
|
2013-08-09 05:52:45 +04:00
|
|
|
|
2013-08-08 13:41:29 +04:00
|
|
|
def python_binary():
|
2015-03-10 00:32:27 +03:00
|
|
|
if PYTHON_LOCATION is not None:
|
|
|
|
return PYTHON_LOCATION
|
2015-10-02 01:07:55 +03:00
|
|
|
|
|
|
|
# look for python in PATH and common install locations
|
2015-03-10 00:32:27 +03:00
|
|
|
paths = [
|
2016-05-12 13:07:24 +03:00
|
|
|
os.path.join(resources_folder(), 'python'),
|
2015-10-06 13:59:04 +03:00
|
|
|
None,
|
2015-10-02 01:19:58 +03:00
|
|
|
'/',
|
|
|
|
'/usr/local/bin/',
|
|
|
|
'/usr/bin/',
|
2015-03-10 00:32:27 +03:00
|
|
|
]
|
|
|
|
for path in paths:
|
2015-10-02 01:07:55 +03:00
|
|
|
path = find_python_in_folder(path)
|
|
|
|
if path is not None:
|
2015-10-02 22:09:37 +03:00
|
|
|
set_python_binary_location(path)
|
2015-03-10 00:32:27 +03:00
|
|
|
return path
|
2015-10-02 01:07:55 +03:00
|
|
|
|
|
|
|
# look for python in windows registry
|
|
|
|
path = find_python_from_registry(r'SOFTWARE\Python\PythonCore')
|
|
|
|
if path is not None:
|
2015-10-02 22:09:37 +03:00
|
|
|
set_python_binary_location(path)
|
2015-10-02 01:07:55 +03:00
|
|
|
return path
|
|
|
|
path = find_python_from_registry(r'SOFTWARE\Wow6432Node\Python\PythonCore')
|
|
|
|
if path is not None:
|
2015-10-02 22:09:37 +03:00
|
|
|
set_python_binary_location(path)
|
2015-10-02 01:07:55 +03:00
|
|
|
return path
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-10-02 22:09:37 +03:00
|
|
|
def set_python_binary_location(path):
|
|
|
|
global PYTHON_LOCATION
|
|
|
|
PYTHON_LOCATION = path
|
2016-01-02 09:20:02 +03:00
|
|
|
log(DEBUG, 'Found Python at: {0}'.format(path))
|
2015-10-02 22:09:37 +03:00
|
|
|
|
|
|
|
|
2015-10-02 01:07:55 +03:00
|
|
|
def find_python_from_registry(location, reg=None):
|
2015-10-02 01:14:36 +03:00
|
|
|
if platform.system() != 'Windows' or winreg is None:
|
2015-10-02 01:07:55 +03:00
|
|
|
return None
|
|
|
|
|
|
|
|
if reg is None:
|
|
|
|
path = find_python_from_registry(location, reg=winreg.HKEY_CURRENT_USER)
|
|
|
|
if path is None:
|
|
|
|
path = find_python_from_registry(location, reg=winreg.HKEY_LOCAL_MACHINE)
|
|
|
|
return path
|
|
|
|
|
|
|
|
val = None
|
|
|
|
sub_key = 'InstallPath'
|
|
|
|
compiled = re.compile(r'^\d+\.\d+$')
|
|
|
|
|
|
|
|
try:
|
|
|
|
with winreg.OpenKey(reg, location) as handle:
|
|
|
|
versions = []
|
|
|
|
try:
|
|
|
|
for index in range(1024):
|
|
|
|
version = winreg.EnumKey(handle, index)
|
|
|
|
try:
|
|
|
|
if compiled.search(version):
|
|
|
|
versions.append(version)
|
|
|
|
except re.error:
|
|
|
|
pass
|
|
|
|
except EnvironmentError:
|
|
|
|
pass
|
|
|
|
versions.sort(reverse=True)
|
|
|
|
for version in versions:
|
|
|
|
try:
|
|
|
|
path = winreg.QueryValue(handle, version + '\\' + sub_key)
|
|
|
|
if path is not None:
|
|
|
|
path = find_python_in_folder(path)
|
|
|
|
if path is not None:
|
2015-10-02 22:09:37 +03:00
|
|
|
log(DEBUG, 'Found python from {reg}\\{key}\\{version}\\{sub_key}.'.format(
|
|
|
|
reg=reg,
|
|
|
|
key=location,
|
|
|
|
version=version,
|
|
|
|
sub_key=sub_key,
|
|
|
|
))
|
2015-10-02 01:07:55 +03:00
|
|
|
return path
|
|
|
|
except WindowsError:
|
2015-10-02 22:09:37 +03:00
|
|
|
log(DEBUG, 'Could not read registry value "{reg}\\{key}\\{version}\\{sub_key}".'.format(
|
|
|
|
reg=reg,
|
2015-10-02 01:07:55 +03:00
|
|
|
key=location,
|
|
|
|
version=version,
|
|
|
|
sub_key=sub_key,
|
|
|
|
))
|
|
|
|
except WindowsError:
|
2016-03-11 20:23:08 +03:00
|
|
|
log(DEBUG, 'Could not read registry value "{reg}\\{key}".'.format(
|
|
|
|
reg=reg,
|
|
|
|
key=location,
|
|
|
|
))
|
2016-11-11 13:29:51 +03:00
|
|
|
except:
|
|
|
|
log(ERROR, 'Could not read registry value "{reg}\\{key}":\n{exc}'.format(
|
|
|
|
reg=reg,
|
|
|
|
key=location,
|
|
|
|
exc=traceback.format_exc(),
|
|
|
|
))
|
2015-10-02 01:07:55 +03:00
|
|
|
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
2016-01-02 09:20:02 +03:00
|
|
|
def find_python_in_folder(folder, headless=True):
|
|
|
|
pattern = re.compile(r'\d+\.\d+')
|
|
|
|
|
2015-10-06 13:59:42 +03:00
|
|
|
path = 'python'
|
2015-10-06 13:59:04 +03:00
|
|
|
if folder is not None:
|
|
|
|
path = os.path.realpath(os.path.join(folder, 'python'))
|
2016-01-02 09:20:02 +03:00
|
|
|
if headless:
|
2016-01-02 10:06:03 +03:00
|
|
|
path = u(path) + u('w')
|
2016-09-22 11:24:20 +03:00
|
|
|
log(DEBUG, u('Looking for Python at: {0}').format(u(path)))
|
2015-10-02 01:07:55 +03:00
|
|
|
try:
|
2016-01-02 09:20:02 +03:00
|
|
|
process = Popen([path, '--version'], stdout=PIPE, stderr=STDOUT)
|
|
|
|
output, err = process.communicate()
|
|
|
|
output = u(output).strip()
|
|
|
|
retcode = process.poll()
|
2016-01-02 10:06:03 +03:00
|
|
|
log(DEBUG, u('Python Version Output: {0}').format(output))
|
2016-01-02 09:20:02 +03:00
|
|
|
if not retcode and pattern.search(output):
|
|
|
|
return path
|
2015-10-02 01:07:55 +03:00
|
|
|
except:
|
2016-04-29 17:56:48 +03:00
|
|
|
log(DEBUG, u(sys.exc_info()[1]))
|
2016-01-02 09:20:02 +03:00
|
|
|
|
|
|
|
if headless:
|
|
|
|
path = find_python_in_folder(folder, headless=False)
|
|
|
|
if path is not None:
|
|
|
|
return path
|
|
|
|
|
2015-03-10 00:32:27 +03:00
|
|
|
return None
|
2013-07-10 11:14:44 +04:00
|
|
|
|
2013-08-09 05:52:45 +04:00
|
|
|
|
2015-04-03 01:04:05 +03:00
|
|
|
def obfuscate_apikey(command_list):
|
|
|
|
cmd = list(command_list)
|
2015-04-01 23:01:10 +03:00
|
|
|
apikey_index = None
|
|
|
|
for num in range(len(cmd)):
|
|
|
|
if cmd[num] == '--key':
|
|
|
|
apikey_index = num + 1
|
|
|
|
break
|
|
|
|
if apikey_index is not None and apikey_index < len(cmd):
|
2015-10-01 21:24:13 +03:00
|
|
|
cmd[apikey_index] = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX' + cmd[apikey_index][-4:]
|
2015-04-01 23:01:10 +03:00
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2016-04-29 01:18:38 +03:00
|
|
|
def enough_time_passed(now, is_write):
|
|
|
|
if now - LAST_HEARTBEAT['time'] > HEARTBEAT_FREQUENCY * 60:
|
2013-07-10 11:14:44 +04:00
|
|
|
return True
|
2016-04-29 01:18:38 +03:00
|
|
|
if is_write and now - LAST_HEARTBEAT['time'] > 2:
|
2015-05-06 22:22:42 +03:00
|
|
|
return True
|
2013-07-10 11:14:44 +04:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-04-03 02:41:55 +03:00
|
|
|
def find_folder_containing_file(folders, current_file):
|
|
|
|
"""Returns absolute path to folder containing the file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
parent_folder = None
|
|
|
|
|
|
|
|
current_folder = current_file
|
|
|
|
while True:
|
2015-02-06 04:40:45 +03:00
|
|
|
for folder in folders:
|
2015-04-03 02:41:55 +03:00
|
|
|
if os.path.realpath(os.path.dirname(current_folder)) == os.path.realpath(folder):
|
|
|
|
parent_folder = folder
|
|
|
|
break
|
|
|
|
if parent_folder is not None:
|
|
|
|
break
|
|
|
|
if not current_folder or os.path.dirname(current_folder) == current_folder:
|
|
|
|
break
|
|
|
|
current_folder = os.path.dirname(current_folder)
|
|
|
|
|
|
|
|
return parent_folder
|
|
|
|
|
|
|
|
|
|
|
|
def find_project_from_folders(folders, current_file):
|
|
|
|
"""Find project name from open folders.
|
|
|
|
"""
|
|
|
|
|
|
|
|
folder = find_folder_containing_file(folders, current_file)
|
2015-04-08 00:20:21 +03:00
|
|
|
return os.path.basename(folder) if folder else None
|
2014-06-18 21:35:59 +04:00
|
|
|
|
|
|
|
|
2015-05-07 00:06:06 +03:00
|
|
|
def is_view_active(view):
|
|
|
|
if view:
|
|
|
|
active_window = sublime.active_window()
|
|
|
|
if active_window:
|
|
|
|
active_view = active_window.active_view()
|
|
|
|
if active_view:
|
|
|
|
return active_view.buffer_id() == view.buffer_id()
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2016-04-30 01:01:25 +03:00
|
|
|
def handle_activity(view, is_write=False):
|
2015-03-25 21:08:27 +03:00
|
|
|
window = view.window()
|
|
|
|
if window is not None:
|
2016-04-29 01:18:38 +03:00
|
|
|
entity = view.file_name()
|
|
|
|
if entity:
|
|
|
|
timestamp = time.time()
|
|
|
|
last_file = LAST_HEARTBEAT['file']
|
|
|
|
if entity != last_file or enough_time_passed(timestamp, is_write):
|
|
|
|
project = window.project_data() if hasattr(window, 'project_data') else None
|
|
|
|
folders = window.folders()
|
|
|
|
append_heartbeat(entity, timestamp, is_write, view, project, folders)
|
|
|
|
|
|
|
|
|
|
|
|
def append_heartbeat(entity, timestamp, is_write, view, project, folders):
|
|
|
|
global LAST_HEARTBEAT
|
|
|
|
|
|
|
|
# add this heartbeat to queue
|
|
|
|
heartbeat = {
|
|
|
|
'entity': entity,
|
|
|
|
'timestamp': timestamp,
|
|
|
|
'is_write': is_write,
|
|
|
|
'cursorpos': view.sel()[0].begin() if view.sel() else None,
|
|
|
|
'project': project,
|
|
|
|
'folders': folders,
|
|
|
|
}
|
|
|
|
HEARTBEATS.put_nowait(heartbeat)
|
|
|
|
|
|
|
|
# make this heartbeat the LAST_HEARTBEAT
|
|
|
|
LAST_HEARTBEAT = {
|
|
|
|
'file': entity,
|
|
|
|
'time': timestamp,
|
|
|
|
'is_write': is_write,
|
|
|
|
}
|
|
|
|
|
|
|
|
# process the queue of heartbeats in the future
|
|
|
|
seconds = 4
|
|
|
|
set_timeout(process_queue, seconds)
|
|
|
|
|
|
|
|
|
|
|
|
def process_queue():
|
|
|
|
try:
|
|
|
|
heartbeat = HEARTBEATS.get_nowait()
|
|
|
|
except queue.Empty:
|
|
|
|
return
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2016-04-29 01:18:38 +03:00
|
|
|
has_extra_heartbeats = False
|
|
|
|
extra_heartbeats = []
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
extra_heartbeats.append(HEARTBEATS.get_nowait())
|
|
|
|
has_extra_heartbeats = True
|
|
|
|
except queue.Empty:
|
|
|
|
pass
|
|
|
|
|
|
|
|
thread = SendHeartbeatsThread(heartbeat)
|
|
|
|
if has_extra_heartbeats:
|
|
|
|
thread.add_extra_heartbeats(extra_heartbeats)
|
|
|
|
thread.start()
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2016-04-29 01:18:38 +03:00
|
|
|
|
|
|
|
class SendHeartbeatsThread(threading.Thread):
|
2015-10-01 22:39:28 +03:00
|
|
|
"""Non-blocking thread for sending heartbeats to api.
|
|
|
|
"""
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2016-04-29 01:18:38 +03:00
|
|
|
def __init__(self, heartbeat):
|
2013-08-08 13:17:47 +04:00
|
|
|
threading.Thread.__init__(self)
|
2016-04-29 01:18:38 +03:00
|
|
|
|
2013-08-14 12:49:08 +04:00
|
|
|
self.debug = SETTINGS.get('debug')
|
2013-08-14 13:36:17 +04:00
|
|
|
self.api_key = SETTINGS.get('api_key', '')
|
2013-10-28 08:30:10 +04:00
|
|
|
self.ignore = SETTINGS.get('ignore', [])
|
2017-09-14 17:39:28 +03:00
|
|
|
self.include = SETTINGS.get('include', [])
|
2017-03-04 19:21:21 +03:00
|
|
|
self.hidefilenames = SETTINGS.get('hidefilenames')
|
2017-03-10 19:11:40 +03:00
|
|
|
self.proxy = SETTINGS.get('proxy')
|
2017-11-06 07:04:48 +03:00
|
|
|
self.python_binary = SETTINGS.get('python_binary')
|
2016-04-29 01:18:38 +03:00
|
|
|
|
|
|
|
self.heartbeat = heartbeat
|
|
|
|
self.has_extra_heartbeats = False
|
|
|
|
|
|
|
|
def add_extra_heartbeats(self, extra_heartbeats):
|
|
|
|
self.has_extra_heartbeats = True
|
|
|
|
self.extra_heartbeats = extra_heartbeats
|
2013-08-08 13:17:47 +04:00
|
|
|
|
|
|
|
def run(self):
|
2016-04-29 01:18:38 +03:00
|
|
|
"""Running in background thread."""
|
|
|
|
|
|
|
|
self.send_heartbeats()
|
|
|
|
|
|
|
|
def build_heartbeat(self, entity=None, timestamp=None, is_write=None,
|
|
|
|
cursorpos=None, project=None, folders=None):
|
|
|
|
"""Returns a dict for passing to wakatime-cli as arguments."""
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'entity': entity,
|
|
|
|
'timestamp': timestamp,
|
|
|
|
'is_write': is_write,
|
|
|
|
}
|
|
|
|
|
|
|
|
if project and project.get('name'):
|
|
|
|
heartbeat['alternate_project'] = project.get('name')
|
|
|
|
elif folders:
|
|
|
|
project_name = find_project_from_folders(folders, entity)
|
2014-06-18 21:35:59 +04:00
|
|
|
if project_name:
|
2016-04-29 01:18:38 +03:00
|
|
|
heartbeat['alternate_project'] = project_name
|
|
|
|
|
|
|
|
if cursorpos is not None:
|
|
|
|
heartbeat['cursorpos'] = '{0}'.format(cursorpos)
|
|
|
|
|
|
|
|
return heartbeat
|
|
|
|
|
|
|
|
def send_heartbeats(self):
|
2017-11-06 07:04:48 +03:00
|
|
|
python = self.python_binary
|
|
|
|
if not python or not python.strip():
|
|
|
|
python = python_binary()
|
|
|
|
if python:
|
2016-04-29 01:18:38 +03:00
|
|
|
heartbeat = self.build_heartbeat(**self.heartbeat)
|
|
|
|
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
|
|
|
|
cmd = [
|
2017-11-06 07:04:48 +03:00
|
|
|
python,
|
2016-04-29 01:18:38 +03:00
|
|
|
API_CLIENT,
|
|
|
|
'--entity', heartbeat['entity'],
|
|
|
|
'--time', str('%f' % heartbeat['timestamp']),
|
|
|
|
'--plugin', ua,
|
|
|
|
]
|
|
|
|
if self.api_key:
|
|
|
|
cmd.extend(['--key', str(bytes.decode(self.api_key.encode('utf8')))])
|
|
|
|
if heartbeat['is_write']:
|
|
|
|
cmd.append('--write')
|
|
|
|
if heartbeat.get('alternate_project'):
|
|
|
|
cmd.extend(['--alternate-project', heartbeat['alternate_project']])
|
|
|
|
if heartbeat.get('cursorpos') is not None:
|
|
|
|
cmd.extend(['--cursorpos', heartbeat['cursorpos']])
|
|
|
|
for pattern in self.ignore:
|
2017-03-04 19:02:04 +03:00
|
|
|
cmd.extend(['--exclude', pattern])
|
2017-09-14 17:39:28 +03:00
|
|
|
for pattern in self.include:
|
|
|
|
cmd.extend(['--include', pattern])
|
2016-04-29 01:18:38 +03:00
|
|
|
if self.debug:
|
|
|
|
cmd.append('--verbose')
|
2017-03-04 19:21:21 +03:00
|
|
|
if self.hidefilenames:
|
|
|
|
cmd.append('--hidefilenames')
|
2017-03-10 19:11:40 +03:00
|
|
|
if self.proxy:
|
|
|
|
cmd.extend(['--proxy', self.proxy])
|
2016-04-29 01:18:38 +03:00
|
|
|
if self.has_extra_heartbeats:
|
|
|
|
cmd.append('--extra-heartbeats')
|
|
|
|
stdin = PIPE
|
|
|
|
extra_heartbeats = [self.build_heartbeat(**x) for x in self.extra_heartbeats]
|
|
|
|
extra_heartbeats = json.dumps(extra_heartbeats)
|
|
|
|
else:
|
|
|
|
extra_heartbeats = None
|
|
|
|
stdin = None
|
|
|
|
|
2015-10-02 22:09:37 +03:00
|
|
|
log(DEBUG, ' '.join(obfuscate_apikey(cmd)))
|
2016-01-02 10:06:03 +03:00
|
|
|
try:
|
2016-04-29 01:18:38 +03:00
|
|
|
process = Popen(cmd, stdin=stdin, stdout=PIPE, stderr=STDOUT)
|
|
|
|
inp = None
|
|
|
|
if self.has_extra_heartbeats:
|
|
|
|
inp = "{0}\n".format(extra_heartbeats)
|
|
|
|
inp = inp.encode('utf-8')
|
|
|
|
output, err = process.communicate(input=inp)
|
|
|
|
output = u(output)
|
|
|
|
retcode = process.poll()
|
|
|
|
if (not retcode or retcode == 102) and not output:
|
2016-01-02 10:06:03 +03:00
|
|
|
self.sent()
|
|
|
|
else:
|
2016-04-29 01:18:38 +03:00
|
|
|
update_status_bar('Error')
|
|
|
|
if retcode:
|
|
|
|
log(DEBUG if retcode == 102 else ERROR, 'wakatime-core exited with status: {0}'.format(retcode))
|
|
|
|
if output:
|
|
|
|
log(ERROR, u('wakatime-core output: {0}').format(output))
|
2016-01-02 10:06:03 +03:00
|
|
|
except:
|
|
|
|
log(ERROR, u(sys.exc_info()[1]))
|
2016-04-29 01:18:38 +03:00
|
|
|
update_status_bar('Error')
|
|
|
|
|
2013-08-13 07:30:06 +04:00
|
|
|
else:
|
2015-10-02 22:09:37 +03:00
|
|
|
log(ERROR, 'Unable to find python binary.')
|
2016-04-29 01:18:38 +03:00
|
|
|
update_status_bar('Error')
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2015-03-20 11:14:53 +03:00
|
|
|
def sent(self):
|
2016-04-29 01:18:38 +03:00
|
|
|
update_status_bar('OK')
|
2015-03-20 11:14:53 +03:00
|
|
|
|
2016-04-29 01:18:38 +03:00
|
|
|
|
|
|
|
def download_python():
|
|
|
|
thread = DownloadPython()
|
|
|
|
thread.start()
|
2015-03-20 11:14:53 +03:00
|
|
|
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2015-12-01 12:04:41 +03:00
|
|
|
class DownloadPython(threading.Thread):
|
|
|
|
"""Non-blocking thread for extracting embeddable Python on Windows machines.
|
2015-10-01 22:39:28 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
def run(self):
|
2015-12-01 12:04:41 +03:00
|
|
|
log(INFO, 'Downloading embeddable Python...')
|
2015-12-01 11:51:09 +03:00
|
|
|
|
2016-10-15 17:16:39 +03:00
|
|
|
ver = '3.5.2'
|
2015-12-01 11:51:09 +03:00
|
|
|
arch = 'amd64' if platform.architecture()[0] == '64bit' else 'win32'
|
|
|
|
url = 'https://www.python.org/ftp/python/{ver}/python-{ver}-embed-{arch}.zip'.format(
|
|
|
|
ver=ver,
|
|
|
|
arch=arch,
|
|
|
|
)
|
|
|
|
|
2016-05-12 13:07:24 +03:00
|
|
|
if not os.path.exists(resources_folder()):
|
|
|
|
os.makedirs(resources_folder())
|
2015-12-01 11:51:09 +03:00
|
|
|
|
2016-05-12 13:07:24 +03:00
|
|
|
zip_file = os.path.join(resources_folder(), 'python.zip')
|
2015-10-01 22:39:28 +03:00
|
|
|
try:
|
2015-12-01 11:51:09 +03:00
|
|
|
urllib.urlretrieve(url, zip_file)
|
2015-10-01 22:39:28 +03:00
|
|
|
except AttributeError:
|
2015-12-01 11:51:09 +03:00
|
|
|
urllib.request.urlretrieve(url, zip_file)
|
|
|
|
|
2015-12-01 12:04:41 +03:00
|
|
|
log(INFO, 'Extracting Python...')
|
2016-11-11 13:29:51 +03:00
|
|
|
with contextlib.closing(ZipFile(zip_file)) as zf:
|
2016-05-12 13:07:24 +03:00
|
|
|
path = os.path.join(resources_folder(), 'python')
|
2015-12-01 11:51:09 +03:00
|
|
|
zf.extractall(path)
|
2015-10-01 22:39:28 +03:00
|
|
|
|
2015-12-01 12:00:48 +03:00
|
|
|
try:
|
|
|
|
os.remove(zip_file)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2015-12-01 12:04:41 +03:00
|
|
|
log(INFO, 'Finished extracting Python.')
|
|
|
|
|
2015-10-01 22:39:28 +03:00
|
|
|
|
2013-08-08 13:17:47 +04:00
|
|
|
def plugin_loaded():
|
2013-10-28 08:30:10 +04:00
|
|
|
global SETTINGS
|
2015-10-02 22:09:37 +03:00
|
|
|
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
2014-10-14 22:02:52 +04:00
|
|
|
|
2016-04-29 01:18:38 +03:00
|
|
|
log(INFO, 'Initializing WakaTime plugin v%s' % __version__)
|
|
|
|
update_status_bar('Initializing')
|
|
|
|
|
2015-04-01 02:19:30 +03:00
|
|
|
if not python_binary():
|
2015-10-02 22:09:37 +03:00
|
|
|
log(WARNING, 'Python binary not found.')
|
2015-08-01 01:19:56 +03:00
|
|
|
if platform.system() == 'Windows':
|
2016-04-29 01:18:38 +03:00
|
|
|
set_timeout(download_python, 0)
|
2015-08-01 01:19:56 +03:00
|
|
|
else:
|
|
|
|
sublime.error_message("Unable to find Python binary!\nWakaTime needs Python to work correctly.\n\nGo to https://www.python.org/downloads")
|
|
|
|
return
|
2014-10-14 22:02:52 +04:00
|
|
|
|
2013-08-14 13:36:17 +04:00
|
|
|
after_loaded()
|
|
|
|
|
|
|
|
|
|
|
|
def after_loaded():
|
|
|
|
if not prompt_api_key():
|
2016-04-29 01:18:38 +03:00
|
|
|
set_timeout(after_loaded, 0.5)
|
2013-08-08 13:17:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
# 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):
|
2016-04-30 01:01:25 +03:00
|
|
|
handle_activity(view, is_write=True)
|
2013-07-02 13:21:13 +04:00
|
|
|
|
2015-04-13 02:45:16 +03:00
|
|
|
def on_selection_modified(self, view):
|
2015-05-07 00:06:06 +03:00
|
|
|
if is_view_active(view):
|
2016-04-30 01:01:25 +03:00
|
|
|
handle_activity(view)
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-07-16 15:02:30 +04:00
|
|
|
def on_modified(self, view):
|
2015-05-07 00:06:06 +03:00
|
|
|
if is_view_active(view):
|
2016-04-30 01:01:25 +03:00
|
|
|
handle_activity(view)
|
2015-02-24 01:59:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
class WakatimeDashboardCommand(sublime_plugin.ApplicationCommand):
|
2015-02-24 09:20:27 +03:00
|
|
|
|
2015-02-24 01:59:10 +03:00
|
|
|
def run(self):
|
|
|
|
webbrowser.open_new_tab('https://wakatime.com/dashboard')
|