mirror of
https://github.com/wakatime/sublime-wakatime.git
synced 2023-08-10 21:13:02 +03:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
9a6be7ca4e | |||
1ea9b2a761 | |||
bd5e87e030 | |||
0256ff4a6a | |||
9d170b3276 | |||
c54e575210 | |||
07513d8f10 | |||
30902cc050 | |||
aa7962d49a | |||
d8c662f3db | |||
10d88ebf2d | |||
2f28c561b1 |
28
HISTORY.rst
28
HISTORY.rst
@ -3,6 +3,34 @@ History
|
||||
-------
|
||||
|
||||
|
||||
8.4.2 (2019-05-07)
|
||||
++++++++++++++++++
|
||||
|
||||
- Upgrade wakatime-cli to v11.0.0.
|
||||
- Rename largument --show-time-today to --today.
|
||||
- New argument --show-time-today for printing Today's coding time.
|
||||
|
||||
|
||||
8.4.1 (2019-05-01)
|
||||
++++++++++++++++++
|
||||
|
||||
- Use api subdomain for fetching today's coding activity.
|
||||
|
||||
|
||||
8.4.0 (2019-05-01)
|
||||
++++++++++++++++++
|
||||
|
||||
- Show today's coding time in status bar.
|
||||
|
||||
|
||||
8.3.6 (2019-04-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- Upgrade wakatime-cli to v10.8.4.
|
||||
- Use wakatime fork of certifi package.
|
||||
`sublime-wakatime#95 <https://github.com/wakatime/sublime-wakatime/issues/95>`_
|
||||
|
||||
|
||||
8.3.5 (2019-04-30)
|
||||
++++++++++++++++++
|
||||
|
||||
|
71
WakaTime.py
71
WakaTime.py
@ -1,3 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
""" ==========================================================
|
||||
File: WakaTime.py
|
||||
Description: Automatic time tracking for Sublime Text 2 and 3.
|
||||
@ -7,7 +8,7 @@ Website: https://wakatime.com/
|
||||
==========================================================="""
|
||||
|
||||
|
||||
__version__ = '8.3.5'
|
||||
__version__ = '8.4.2'
|
||||
|
||||
|
||||
import sublime
|
||||
@ -25,7 +26,7 @@ import threading
|
||||
import traceback
|
||||
import urllib
|
||||
import webbrowser
|
||||
from datetime import datetime
|
||||
from contextlib import closing
|
||||
from subprocess import STDOUT, PIPE
|
||||
from zipfile import ZipFile
|
||||
try:
|
||||
@ -40,6 +41,11 @@ try:
|
||||
except ImportError:
|
||||
import queue # py3
|
||||
|
||||
try:
|
||||
import urllib2
|
||||
except ImportError:
|
||||
import urllib.request as urllib2
|
||||
|
||||
|
||||
is_py2 = (sys.version_info[0] == 2)
|
||||
is_py3 = (sys.version_info[0] == 3)
|
||||
@ -47,6 +53,8 @@ is_win = platform.system() == 'Windows'
|
||||
|
||||
|
||||
if is_py2:
|
||||
STATUS_BAR_PREFIX = 'WakaTime'
|
||||
|
||||
def u(text):
|
||||
if text is None:
|
||||
return None
|
||||
@ -70,6 +78,8 @@ if is_py2:
|
||||
return unicode('')
|
||||
|
||||
elif is_py3:
|
||||
STATUS_BAR_PREFIX = '🕘'
|
||||
|
||||
def u(text):
|
||||
if text is None:
|
||||
return None
|
||||
@ -121,10 +131,14 @@ LAST_HEARTBEAT = {
|
||||
'is_write': False,
|
||||
}
|
||||
LAST_HEARTBEAT_SENT_AT = 0
|
||||
LAST_FETCH_TODAY_CODING_TIME = 0
|
||||
FETCH_TODAY_DEBOUNCE_COUNTER = 0
|
||||
FETCH_TODAY_DEBOUNCE_SECONDS = 60
|
||||
PYTHON_LOCATION = None
|
||||
HEARTBEATS = queue.Queue()
|
||||
HEARTBEAT_FREQUENCY = 2 # minutes between logging heartbeat when editing same file
|
||||
SEND_BUFFER_SECONDS = 30 # seconds between sending buffered heartbeats to API
|
||||
API_SUMMARIES_URL = 'https://api.wakatime.com/api/v1/users/current/summaries?start=today&end=today&api_key={}'
|
||||
|
||||
|
||||
# Log Levels
|
||||
@ -180,21 +194,59 @@ def resources_folder():
|
||||
return os.path.join(os.path.expanduser('~'), '.wakatime')
|
||||
|
||||
|
||||
def update_status_bar(status):
|
||||
def update_status_bar(status=None, debounced=False, msg=None):
|
||||
"""Updates the status bar."""
|
||||
global LAST_FETCH_TODAY_CODING_TIME, FETCH_TODAY_DEBOUNCE_COUNTER
|
||||
|
||||
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)
|
||||
if not msg and SETTINGS.get('status_bar_message') is not False and SETTINGS.get('status_bar_enabled'):
|
||||
if SETTINGS.get('status_bar_coding_activity') and status == 'OK':
|
||||
if debounced:
|
||||
FETCH_TODAY_DEBOUNCE_COUNTER -= 1
|
||||
if debounced or not LAST_FETCH_TODAY_CODING_TIME:
|
||||
now = int(time.time())
|
||||
if LAST_FETCH_TODAY_CODING_TIME and (FETCH_TODAY_DEBOUNCE_COUNTER > 0 or LAST_FETCH_TODAY_CODING_TIME > now - FETCH_TODAY_DEBOUNCE_SECONDS):
|
||||
return
|
||||
LAST_FETCH_TODAY_CODING_TIME = now
|
||||
FetchStatusBarCodingTime().start()
|
||||
return
|
||||
else:
|
||||
FETCH_TODAY_DEBOUNCE_COUNTER += 1
|
||||
set_timeout(lambda: update_status_bar(status, debounced=True), FETCH_TODAY_DEBOUNCE_SECONDS)
|
||||
return
|
||||
else:
|
||||
msg = '{prefix}: {status}'.format(prefix=STATUS_BAR_PREFIX, status=status)
|
||||
|
||||
if msg:
|
||||
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)
|
||||
set_timeout(lambda: update_status_bar(status=status, debounced=debounced, msg=msg), 0)
|
||||
|
||||
|
||||
class FetchStatusBarCodingTime(threading.Thread):
|
||||
|
||||
def __init__(self):
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
self.api_key = SETTINGS.get('api_key', '')
|
||||
|
||||
def run(self):
|
||||
if not self.api_key:
|
||||
log(DEBUG, 'Missing WakaTime API key.')
|
||||
return
|
||||
|
||||
url = API_SUMMARIES_URL.format(self.api_key)
|
||||
try:
|
||||
with closing(urllib2.urlopen(url)) as response:
|
||||
grand_total_text = json.loads(u(response.read()))['data'][0]['grand_total']['text']
|
||||
msg = '{} Today: {}'.format(STATUS_BAR_PREFIX, grand_total_text)
|
||||
update_status_bar(msg=msg)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def prompt_api_key():
|
||||
@ -648,7 +700,7 @@ def plugin_loaded():
|
||||
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
||||
|
||||
log(INFO, 'Initializing WakaTime plugin v%s' % __version__)
|
||||
update_status_bar('Initializing')
|
||||
update_status_bar('Initializing...')
|
||||
|
||||
if not python_binary():
|
||||
log(WARNING, 'Python binary not found.')
|
||||
@ -664,6 +716,7 @@ def plugin_loaded():
|
||||
def after_loaded():
|
||||
if not prompt_api_key():
|
||||
set_timeout(after_loaded, 0.5)
|
||||
update_status_bar('OK')
|
||||
|
||||
|
||||
# need to call plugin_loaded because only ST3 will auto-call it
|
||||
|
@ -21,12 +21,13 @@
|
||||
// POSIX regular expressions will bypass your ignore setting.
|
||||
"include": [".*"],
|
||||
|
||||
// Status bar message. Set to false to hide status bar message.
|
||||
// Defaults to true.
|
||||
"status_bar_message": true,
|
||||
// Status bar for surfacing errors and displaying today's coding time. Set
|
||||
// to false to hide. Defaults to true.
|
||||
"status_bar_enabled": true,
|
||||
|
||||
// Status bar message format.
|
||||
"status_bar_message_fmt": "WakaTime {status} %I:%M %p",
|
||||
// Show today's coding activity in WakaTime status bar item.
|
||||
// Defaults to true.
|
||||
"status_bar_coding_activity": true,
|
||||
|
||||
// Obfuscate file paths when sending to API. Your dashboard will no longer display coding activity per file.
|
||||
"hidefilenames": false,
|
||||
|
@ -1,7 +1,7 @@
|
||||
__title__ = 'wakatime'
|
||||
__description__ = 'Common interface to the WakaTime api.'
|
||||
__url__ = 'https://github.com/wakatime/wakatime'
|
||||
__version_info__ = ('10', '8', '3')
|
||||
__version_info__ = ('11', '0', '0')
|
||||
__version__ = '.'.join(__version_info__)
|
||||
__author__ = 'Alan Hamlett'
|
||||
__author_email__ = 'alan@wakatime.com'
|
||||
|
@ -163,6 +163,119 @@ def send_heartbeats(heartbeats, args, configs, use_ntlm_proxy=False):
|
||||
return AUTH_ERROR if code == 401 else API_ERROR
|
||||
|
||||
|
||||
def get_time_today(args, use_ntlm_proxy=False):
|
||||
"""Get coding time from WakaTime API for given time range.
|
||||
|
||||
Returns total time as string or `None` when unable to fetch summary from
|
||||
the API. When verbose output enabled, returns error message when unable to
|
||||
fetch summary.
|
||||
"""
|
||||
|
||||
url = 'https://api.wakatime.com/api/v1/users/current/summaries'
|
||||
timeout = args.timeout
|
||||
if not timeout:
|
||||
timeout = 60
|
||||
|
||||
api_key = u(base64.b64encode(str.encode(args.key) if is_py3 else args.key))
|
||||
auth = u('Basic {api_key}').format(api_key=api_key)
|
||||
headers = {
|
||||
'User-Agent': get_user_agent(args.plugin),
|
||||
'Accept': 'application/json',
|
||||
'Authorization': auth,
|
||||
}
|
||||
|
||||
session_cache = SessionCache()
|
||||
session = session_cache.get()
|
||||
|
||||
should_try_ntlm = False
|
||||
proxies = {}
|
||||
if args.proxy:
|
||||
if use_ntlm_proxy:
|
||||
from .packages.requests_ntlm import HttpNtlmAuth
|
||||
username = args.proxy.rsplit(':', 1)
|
||||
password = ''
|
||||
if len(username) == 2:
|
||||
password = username[1]
|
||||
username = username[0]
|
||||
session.auth = HttpNtlmAuth(username, password, session)
|
||||
else:
|
||||
should_try_ntlm = '\\' in args.proxy
|
||||
proxies['https'] = args.proxy
|
||||
|
||||
ssl_verify = not args.nosslverify
|
||||
if args.ssl_certs_file and ssl_verify:
|
||||
ssl_verify = args.ssl_certs_file
|
||||
|
||||
params = {
|
||||
'start': 'today',
|
||||
'end': 'today',
|
||||
}
|
||||
|
||||
# send request to api
|
||||
response, code = None, None
|
||||
try:
|
||||
response = session.get(url, params=params, headers=headers,
|
||||
proxies=proxies, timeout=timeout,
|
||||
verify=ssl_verify)
|
||||
except RequestException:
|
||||
if should_try_ntlm:
|
||||
return get_time_today(args, use_ntlm_proxy=True)
|
||||
|
||||
session_cache.delete()
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
exception_data = {
|
||||
sys.exc_info()[0].__name__: u(sys.exc_info()[1]),
|
||||
'traceback': traceback.format_exc(),
|
||||
}
|
||||
log.error(exception_data)
|
||||
return '{0}: {1}'.format(sys.exc_info()[0].__name__, u(sys.exc_info()[1])), API_ERROR
|
||||
return None, API_ERROR
|
||||
|
||||
except: # delete cached session when requests raises unknown exception
|
||||
if should_try_ntlm:
|
||||
return get_time_today(args, use_ntlm_proxy=True)
|
||||
|
||||
session_cache.delete()
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
exception_data = {
|
||||
sys.exc_info()[0].__name__: u(sys.exc_info()[1]),
|
||||
'traceback': traceback.format_exc(),
|
||||
}
|
||||
log.error(exception_data)
|
||||
return '{0}: {1}'.format(sys.exc_info()[0].__name__, u(sys.exc_info()[1])), API_ERROR
|
||||
return None, API_ERROR
|
||||
|
||||
code = response.status_code if response is not None else None
|
||||
content = response.text if response is not None else None
|
||||
|
||||
if code == requests.codes.ok:
|
||||
try:
|
||||
text = response.json()['data'][0]['grand_total']['text']
|
||||
session_cache.save(session)
|
||||
return text, SUCCESS
|
||||
except:
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
exception_data = {
|
||||
sys.exc_info()[0].__name__: u(sys.exc_info()[1]),
|
||||
'traceback': traceback.format_exc(),
|
||||
}
|
||||
log.error(exception_data)
|
||||
return '{0}: {1}'.format(sys.exc_info()[0].__name__, u(sys.exc_info()[1])), API_ERROR
|
||||
return None, API_ERROR
|
||||
else:
|
||||
if should_try_ntlm:
|
||||
return get_time_today(args, use_ntlm_proxy=True)
|
||||
|
||||
session_cache.delete()
|
||||
log.debug({
|
||||
'response_code': code,
|
||||
'response_text': content,
|
||||
})
|
||||
if log.isEnabledFor(logging.DEBUG):
|
||||
return 'Error: {0}'.format(code), API_ERROR
|
||||
return None, API_ERROR
|
||||
|
||||
|
||||
def _process_server_results(heartbeats, code, content, results, args, configs):
|
||||
log.debug({
|
||||
'response_code': code,
|
||||
|
@ -201,6 +201,9 @@ def parse_arguments():
|
||||
'online 5 offline heartbeats are synced. Can ' +
|
||||
'be used without --entity to only sync offline ' +
|
||||
'activity without generating new heartbeats.')
|
||||
parser.add_argument('--today', dest='today',
|
||||
action='store_true',
|
||||
help='Prints dashboard time for Today, then exits.')
|
||||
parser.add_argument('--config', dest='config', action=StoreWithoutQuotes,
|
||||
help='Defaults to ~/.wakatime.cfg.')
|
||||
parser.add_argument('--verbose', dest='verbose', action='store_true',
|
||||
@ -245,7 +248,7 @@ def parse_arguments():
|
||||
if not args.entity:
|
||||
if args.file:
|
||||
args.entity = args.file
|
||||
elif not args.sync_offline_activity or args.sync_offline_activity == 'none':
|
||||
elif (not args.sync_offline_activity or args.sync_offline_activity == 'none') and not args.today:
|
||||
parser.error('argument --entity is required')
|
||||
|
||||
if not args.sync_offline_activity:
|
||||
|
@ -22,7 +22,7 @@ sys.path.insert(0, os.path.dirname(pwd))
|
||||
sys.path.insert(0, os.path.join(pwd, 'packages'))
|
||||
|
||||
from .__about__ import __version__
|
||||
from .api import send_heartbeats
|
||||
from .api import send_heartbeats, get_time_today
|
||||
from .arguments import parse_arguments
|
||||
from .compat import u, json
|
||||
from .constants import SUCCESS, UNKNOWN_ERROR, HEARTBEATS_PER_REQUEST
|
||||
@ -42,6 +42,12 @@ def execute(argv=None):
|
||||
|
||||
setup_logging(args, __version__)
|
||||
|
||||
if args.today:
|
||||
text, retval = get_time_today(args)
|
||||
if text:
|
||||
print(text)
|
||||
return retval
|
||||
|
||||
try:
|
||||
heartbeats = []
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
from certifi import where
|
||||
from . import where
|
||||
print(where())
|
||||
|
Reference in New Issue
Block a user