sublime-rana/packages/wakatime/main.py

97 lines
2.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
2015-09-29 10:11:25 +00:00
wakatime.main
2015-03-09 22:23:29 +00:00
~~~~~~~~~~~~~
2017-02-21 00:18:38 +00:00
Module entry point.
:copyright: (c) 2013 Alan Hamlett.
:license: BSD, see LICENSE for more details.
"""
from __future__ import print_function
import logging
import os
import sys
2018-10-05 07:06:02 +00:00
import time
import traceback
2016-03-06 20:48:42 +00:00
pwd = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(pwd))
sys.path.insert(0, os.path.join(pwd, 'packages'))
2014-06-09 20:18:30 +00:00
2015-03-09 22:23:29 +00:00
from .__about__ import __version__
2019-05-08 01:42:31 +00:00
from .api import send_heartbeats, get_time_today
2017-11-09 07:12:05 +00:00
from .arguments import parse_arguments
from .compat import u, json
2018-09-21 05:29:34 +00:00
from .constants import SUCCESS, UNKNOWN_ERROR, HEARTBEATS_PER_REQUEST
2015-04-04 18:03:55 +00:00
from .logger import setup_logging
2017-05-25 06:53:28 +00:00
log = logging.getLogger('WakaTime')
2017-11-09 07:12:05 +00:00
from .heartbeat import Heartbeat
2015-05-12 22:03:23 +00:00
from .offlinequeue import Queue
2016-04-28 22:04:46 +00:00
2015-09-29 10:11:25 +00:00
def execute(argv=None):
if argv:
sys.argv = ['wakatime'] + argv
2017-11-09 07:12:05 +00:00
args, configs = parse_arguments()
setup_logging(args, __version__)
2019-05-08 01:42:31 +00:00
if args.today:
text, retval = get_time_today(args)
if text:
print(text)
return retval
2016-01-06 21:57:03 +00:00
try:
2017-11-09 07:12:05 +00:00
heartbeats = []
2016-01-06 21:57:03 +00:00
2017-11-09 07:12:05 +00:00
hb = Heartbeat(vars(args), args, configs)
if hb:
heartbeats.append(hb)
else:
log.debug(hb.skip)
2016-04-28 22:04:46 +00:00
if args.extra_heartbeats:
try:
2017-11-09 07:12:05 +00:00
for extra_data in json.loads(sys.stdin.readline()):
hb = Heartbeat(extra_data, args, configs)
if hb:
heartbeats.append(hb)
else:
log.debug(hb.skip)
except json.JSONDecodeError as ex:
log.warning(u('Malformed extra heartbeats json: {msg}').format(
msg=u(ex),
))
2016-04-28 22:04:46 +00:00
2018-09-21 05:29:34 +00:00
retval = SUCCESS
while heartbeats:
retval = send_heartbeats(heartbeats[:HEARTBEATS_PER_REQUEST], args, configs)
heartbeats = heartbeats[HEARTBEATS_PER_REQUEST:]
if retval != SUCCESS:
break
if heartbeats:
Queue(args, configs).push_many(heartbeats)
2016-04-28 22:04:46 +00:00
if retval == SUCCESS:
2017-11-09 07:12:05 +00:00
queue = Queue(args, configs)
2018-09-21 05:29:34 +00:00
for offline_heartbeats in queue.pop_many(args.sync_offline_activity):
2018-10-05 07:06:02 +00:00
time.sleep(1)
2017-11-09 07:12:05 +00:00
retval = send_heartbeats(offline_heartbeats, args, configs)
2018-09-21 05:29:34 +00:00
if retval != SUCCESS:
break
2016-04-28 22:04:46 +00:00
return retval
2016-01-06 21:57:03 +00:00
except:
2016-09-02 08:50:54 +00:00
log.traceback(logging.ERROR)
2016-04-28 22:04:46 +00:00
print(traceback.format_exc())
2016-03-06 22:13:27 +00:00
return UNKNOWN_ERROR