This commit is contained in:
Carlos Henrique Gandarez 2015-05-15 11:21:15 -03:00
parent 1576cfc1f0
commit 7f35f96286

View file

@ -29,14 +29,14 @@ sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pac
from .__about__ import __version__ from .__about__ import __version__
from .compat import u, open, is_py3 from .compat import u, open, is_py3
from .offlinequeue import Queue
from .logger import setup_logging from .logger import setup_logging
from .project import find_project from .offlinequeue import Queue
from .stats import get_file_stats
from .packages import argparse from .packages import argparse
from .packages import simplejson as json from .packages import simplejson as json
from .packages import requests
from .packages.requests.exceptions import RequestException from .packages.requests.exceptions import RequestException
from .project import find_project
from .session_cache import SessionCache
from .stats import get_file_stats
try: try:
from .packages import tzlocal from .packages import tzlocal
except: except:
@ -147,6 +147,10 @@ def parseArguments(argv):
type=float, type=float,
help='optional floating-point unix epoch timestamp; '+ help='optional floating-point unix epoch timestamp; '+
'uses current time by default') 'uses current time by default')
parser.add_argument('--lineno', dest='lineno',
help='optional line number; current line being edited')
parser.add_argument('--cursorpos', dest='cursorpos',
help='optional cursor position in the current file')
parser.add_argument('--notfile', dest='notfile', action='store_true', parser.add_argument('--notfile', dest='notfile', action='store_true',
help='when set, will accept any value for the file. for example, '+ help='when set, will accept any value for the file. for example, '+
'a domain name or other item you want to log time towards.') 'a domain name or other item you want to log time towards.')
@ -322,6 +326,10 @@ def send_heartbeat(project=None, branch=None, stats={}, key=None, targetFile=Non
data['language'] = stats['language'] data['language'] = stats['language']
if stats.get('dependencies'): if stats.get('dependencies'):
data['dependencies'] = stats['dependencies'] data['dependencies'] = stats['dependencies']
if stats.get('lineno'):
data['lineno'] = stats['lineno']
if stats.get('cursorpos'):
data['cursorpos'] = stats['cursorpos']
if isWrite: if isWrite:
data['is_write'] = isWrite data['is_write'] = isWrite
if project: if project:
@ -352,10 +360,13 @@ def send_heartbeat(project=None, branch=None, stats={}, key=None, targetFile=Non
if tz: if tz:
headers['TimeZone'] = u(tz.zone) headers['TimeZone'] = u(tz.zone)
session_cache = SessionCache()
session = session_cache.get()
# log time to api # log time to api
response = None response = None
try: try:
response = requests.post(api_url, data=request_body, headers=headers, response = session.post(api_url, data=request_body, headers=headers,
proxies=proxies) proxies=proxies)
except RequestException: except RequestException:
exception_data = { exception_data = {
@ -377,6 +388,7 @@ def send_heartbeat(project=None, branch=None, stats={}, key=None, targetFile=Non
log.debug({ log.debug({
'response_code': response_code, 'response_code': response_code,
}) })
session_cache.save(session)
return True return True
if offline: if offline:
if response_code != 400: if response_code != 400:
@ -402,6 +414,7 @@ def send_heartbeat(project=None, branch=None, stats={}, key=None, targetFile=Non
'response_code': response_code, 'response_code': response_code,
'response_content': response_content, 'response_content': response_content,
}) })
session_cache.delete()
return False return False
@ -424,7 +437,8 @@ def main(argv=None):
if os.path.isfile(args.targetFile) or args.notfile: if os.path.isfile(args.targetFile) or args.notfile:
stats = get_file_stats(args.targetFile, notfile=args.notfile) stats = get_file_stats(args.targetFile, notfile=args.notfile,
lineno=args.lineno, cursorpos=args.cursorpos)
project = None project = None
if not args.notfile: if not args.notfile:
@ -435,7 +449,7 @@ def main(argv=None):
branch = project.branch() branch = project.branch()
if not project_name: if not project_name:
project_name = project.name() project_name = project.name()
if send_heartbeat( if send_heartbeat(
project=project_name, project=project_name,
branch=branch, branch=branch,