sublime-wakatime/packages/wakatime/main.py

79 lines
2.0 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
2015-09-29 13:11:25 +03:00
wakatime.main
2015-03-10 01:23:29 +03:00
~~~~~~~~~~~~~
2017-02-21 03:18:38 +03: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
import traceback
2016-03-06 23:48:42 +03: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-10 00:18:30 +04:00
2015-03-10 01:23:29 +03:00
from .__about__ import __version__
2017-11-09 10:12:05 +03:00
from .api import send_heartbeats
from .arguments import parse_arguments
from .compat import u, json
2017-11-09 20:09:28 +03:00
from .constants import SUCCESS, UNKNOWN_ERROR
2015-04-04 21:03:55 +03:00
from .logger import setup_logging
2017-05-25 09:53:28 +03:00
log = logging.getLogger('WakaTime')
2017-11-09 10:12:05 +03:00
from .heartbeat import Heartbeat
2015-05-13 01:03:23 +03:00
from .offlinequeue import Queue
2016-04-29 01:04:46 +03:00
2015-09-29 13:11:25 +03:00
def execute(argv=None):
if argv:
sys.argv = ['wakatime'] + argv
2017-11-09 10:12:05 +03:00
args, configs = parse_arguments()
setup_logging(args, __version__)
2016-01-07 00:57:03 +03:00
try:
2017-11-09 10:12:05 +03:00
heartbeats = []
2016-01-07 00:57:03 +03:00
2017-11-09 10:12:05 +03:00
hb = Heartbeat(vars(args), args, configs)
if hb:
heartbeats.append(hb)
else:
log.debug(hb.skip)
2016-04-29 01:04:46 +03:00
if args.extra_heartbeats:
try:
2017-11-09 10:12:05 +03: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-29 01:04:46 +03:00
2017-11-09 10:12:05 +03:00
retval = send_heartbeats(heartbeats, args, configs)
2016-04-29 01:04:46 +03:00
if retval == SUCCESS:
2017-11-09 10:12:05 +03:00
queue = Queue(args, configs)
offline_heartbeats = queue.pop_many()
if len(offline_heartbeats) > 0:
retval = send_heartbeats(offline_heartbeats, args, configs)
2016-04-29 01:04:46 +03:00
return retval
2016-01-07 00:57:03 +03:00
except:
2016-09-02 11:50:54 +03:00
log.traceback(logging.ERROR)
2016-04-29 01:04:46 +03:00
print(traceback.format_exc())
2016-03-07 01:13:27 +03:00
return UNKNOWN_ERROR