rana-cli/wakatime/main.py

89 lines
2.3 KiB
Python
Raw Normal View History

2015-03-09 21:57:41 +00:00
# -*- coding: utf-8 -*-
"""
wakatime.main
2015-03-09 21:57:41 +00:00
~~~~~~~~~~~~~
Module entry point.
2015-03-09 21:57:41 +00:00
:copyright: (c) 2013 Alan Hamlett.
:license: BSD, see LICENSE for more details.
"""
from __future__ import print_function
import logging
import os
import sys
import traceback
2016-03-06 20:42:54 +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'))
2015-03-09 21:57:41 +00:00
from .__about__ import __version__
2017-11-09 06:54:33 +00:00
from .api import send_heartbeats
from .arguments import parse_arguments
from .compat import u, json
from .constants import SUCCESS, UNKNOWN_ERROR, HEARTBEATS_PER_REQUEST
2015-04-01 19:48:42 +00:00
from .logger import setup_logging
log = logging.getLogger('WakaTime')
2017-11-09 06:54:33 +00:00
from .heartbeat import Heartbeat
from .offlinequeue import Queue
2016-04-20 22:30:44 +00:00
def execute(argv=None):
if argv:
sys.argv = ['wakatime'] + argv
2015-03-09 21:57:41 +00:00
2017-11-09 06:54:33 +00:00
args, configs = parse_arguments()
2015-03-09 21:57:41 +00:00
setup_logging(args, __version__)
try:
2017-11-09 06:54:33 +00:00
heartbeats = []
2017-11-09 06:54:33 +00:00
hb = Heartbeat(vars(args), args, configs)
if hb:
heartbeats.append(hb)
else:
log.debug(hb.skip)
2016-04-20 22:30:44 +00:00
if args.extra_heartbeats:
try:
2017-11-09 06:54:33 +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),
))
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)
if retval == SUCCESS:
2017-11-09 06:54:33 +00:00
queue = Queue(args, configs)
for offline_heartbeats in queue.pop_many(args.sync_offline_activity):
2017-11-09 06:54:33 +00:00
retval = send_heartbeats(offline_heartbeats, args, configs)
if retval != SUCCESS:
break
2016-04-20 22:30:44 +00:00
return retval
2015-03-09 21:57:41 +00:00
except:
log.traceback(logging.ERROR)
2016-04-20 22:30:44 +00:00
print(traceback.format_exc())
return UNKNOWN_ERROR