upgraded wakatime package to v0.3.1

This commit is contained in:
Alan Hamlett 2013-08-12 03:32:04 -07:00
parent 284d7247ee
commit e9aa35ec27
4 changed files with 48 additions and 37 deletions

View file

@ -2,6 +2,17 @@
History History
------- -------
0.3.1 (2013-08-08)
++++++++++++++++++
- Using requests module instead of urllib2 to verify SSL certs
0.3.0 (2013-08-08)
++++++++++++++++++
- Allow importing directly from Python plugins
0.1.1 (2013-07-07) 0.1.1 (2013-07-07)
++++++++++++++++++ ++++++++++++++++++

View file

@ -14,7 +14,7 @@ from __future__ import print_function
import os import os
import sys import sys
sys.path.insert(0, os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import wakatime import wakatime
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -12,7 +12,7 @@
from __future__ import print_function from __future__ import print_function
__title__ = 'wakatime' __title__ = 'wakatime'
__version__ = '0.2.0' __version__ = '0.3.1'
__author__ = 'Alan Hamlett' __author__ = 'Alan Hamlett'
__license__ = 'BSD' __license__ = 'BSD'
__copyright__ = 'Copyright 2013 Alan Hamlett' __copyright__ = 'Copyright 2013 Alan Hamlett'
@ -27,14 +27,11 @@ import re
import sys import sys
import time import time
import traceback import traceback
try:
from urllib2 import HTTPError, Request, urlopen
except ImportError:
from urllib.error import HTTPError
from urllib.request import Request, urlopen
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from .log import setup_logging from .log import setup_logging
from .project import find_project from .project import find_project
from .packages import requests
try: try:
import argparse import argparse
except ImportError: except ImportError:
@ -52,6 +49,10 @@ class FileAction(argparse.Action):
def parseArguments(argv): def parseArguments(argv):
try:
sys.argv
except AttributeError:
sys.argv = argv
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Wakati.Me event api appender') description='Wakati.Me event api appender')
parser.add_argument('--file', dest='targetFile', metavar='file', parser.add_argument('--file', dest='targetFile', metavar='file',
@ -136,41 +137,34 @@ def send_action(project=None, tags=None, key=None, targetFile=None,
if tags: if tags:
data['tags'] = list(set(tags)) data['tags'] = list(set(tags))
log.debug(data) log.debug(data)
request = Request(url=url, data=str.encode(json.dumps(data)))
user_agent = get_user_agent(plugin) # setup api request headers
request.add_header('User-Agent', user_agent)
request.add_header('Content-Type', 'application/json')
auth = 'Basic %s' % bytes.decode(base64.b64encode(str.encode(key))) auth = 'Basic %s' % bytes.decode(base64.b64encode(str.encode(key)))
request.add_header('Authorization', auth) headers = {
response = None 'User-Agent': get_user_agent(plugin),
'Content-Type': 'application/json',
'Authorization': auth,
}
# send action to api
try: try:
response = urlopen(request) response = requests.post(url, data=str.encode(json.dumps(data)), headers=headers)
except HTTPError as exc: except requests.exceptions.RequestException as exc:
data = { exception_data = {
'response_code': exc.getcode(),
'response_content': exc.read(),
sys.exc_info()[0].__name__: str(sys.exc_info()[1]), sys.exc_info()[0].__name__: str(sys.exc_info()[1]),
} }
if log.isEnabledFor(logging.DEBUG): if log.isEnabledFor(logging.DEBUG):
data['traceback'] = traceback.format_exc() exception_data['traceback'] = traceback.format_exc()
log.error(data) log.error(exception_data)
except:
data = {
sys.exc_info()[0].__name__: str(sys.exc_info()[1]),
}
if log.isEnabledFor(logging.DEBUG):
data['traceback'] = traceback.format_exc()
log.error(data)
else: else:
if response.getcode() >= 200 and response.getcode() < 300: if response.status_code == requests.codes.created:
log.debug({ log.debug({
'response_code': response.getcode(), 'response_code': response.status_code,
'response_content': response.read(),
}) })
return True return True
log.error({ log.error({
'response_code': response.getcode(), 'response_code': response.status_code,
'response_content': response.read(), 'response_content': response.text,
}) })
return False return False

View file

@ -62,7 +62,18 @@ class JsonFormatter(logging.Formatter):
return exec_info[2].format_exc() return exec_info[2].format_exc()
def set_log_level(logger, args):
level = logging.WARN
if args.verbose:
level = logging.DEBUG
logger.setLevel(level)
def setup_logging(args, version): def setup_logging(args, version):
logger = logging.getLogger()
set_log_level(logger, args)
if len(logger.handlers) > 0:
return logger
logfile = args.logfile logfile = args.logfile
if not logfile: if not logfile:
logfile = '~/.wakatime.log' logfile = '~/.wakatime.log'
@ -77,10 +88,5 @@ def setup_logging(args, version):
plugin=args.plugin, plugin=args.plugin,
) )
handler.setFormatter(formatter) handler.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(handler) logger.addHandler(handler)
level = logging.INFO
if args.verbose:
level = logging.DEBUG
logger.setLevel(level)
return logger return logger