upgrade wakatime package to v2.1.0

This commit is contained in:
Alan Hamlett 2014-09-30 09:27:35 -07:00
parent 80556d0cbf
commit 8632c4ff08
10 changed files with 89 additions and 80 deletions

View File

@ -3,6 +3,12 @@ History
------- -------
2.1.0 (2014-09-30)
++++++++++++++++++
- python3 compatibility changes
2.0.8 (2014-08-29) 2.0.8 (2014-08-29)
++++++++++++++++++ ++++++++++++++++++

View File

@ -13,7 +13,7 @@
from __future__ import print_function from __future__ import print_function
__title__ = 'wakatime' __title__ = 'wakatime'
__version__ = '2.0.8' __version__ = '2.1.0'
__author__ = 'Alan Hamlett' __author__ = 'Alan Hamlett'
__license__ = 'BSD' __license__ = 'BSD'
__copyright__ = 'Copyright 2014 Alan Hamlett' __copyright__ = 'Copyright 2014 Alan Hamlett'
@ -40,6 +40,7 @@ except ImportError:
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages')) sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages'))
from .compat import u, open, is_py2, is_py3
from .queue import Queue from .queue import Queue
from .log import setup_logging from .log import setup_logging
from .project import find_project from .project import find_project
@ -54,11 +55,6 @@ except:
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
try:
unicode
except NameError:
unicode = str
class FileAction(argparse.Action): class FileAction(argparse.Action):
@ -82,7 +78,7 @@ def upgradeConfigFile(configFile):
'ignore': [], 'ignore': [],
} }
with open(oldConfig) as fh: with open(oldConfig, 'r', encoding='utf-8') as fh:
for line in fh.readlines(): for line in fh.readlines():
line = line.split('=', 1) line = line.split('=', 1)
if len(line) == 2 and line[0].strip() and line[1].strip(): if len(line) == 2 and line[0].strip() and line[1].strip():
@ -91,7 +87,7 @@ def upgradeConfigFile(configFile):
else: else:
configs[line[0].strip()] = line[1].strip() configs[line[0].strip()] = line[1].strip()
with open(configFile, 'w') as fh: with open(configFile, 'w', encoding='utf-8') as fh:
fh.write("[settings]\n") fh.write("[settings]\n")
for name, value in configs.items(): for name, value in configs.items():
if isinstance(value, list): if isinstance(value, list):
@ -119,7 +115,7 @@ def parseConfigFile(configFile):
configs = configparser.SafeConfigParser() configs = configparser.SafeConfigParser()
try: try:
with open(configFile) as fh: with open(configFile, 'r', encoding='utf-8') as fh:
try: try:
configs.readfp(fh) configs.readfp(fh)
except configparser.Error: except configparser.Error:
@ -231,7 +227,7 @@ def should_ignore(fileName, patterns):
if compiled.search(fileName): if compiled.search(fileName):
return pattern return pattern
except re.error as ex: except re.error as ex:
log.warning(unicode('Regex error ({msg}) for ignore pattern: {pattern}').format( log.warning(u('Regex error ({msg}) for ignore pattern: {pattern}').format(
msg=str(ex), msg=str(ex),
pattern=pattern, pattern=pattern,
)) ))
@ -243,13 +239,13 @@ def should_ignore(fileName, patterns):
def get_user_agent(plugin): def get_user_agent(plugin):
ver = sys.version_info ver = sys.version_info
python_version = '%d.%d.%d.%s.%d' % (ver[0], ver[1], ver[2], ver[3], ver[4]) python_version = '%d.%d.%d.%s.%d' % (ver[0], ver[1], ver[2], ver[3], ver[4])
user_agent = unicode('wakatime/{ver} ({platform}) Python{py_ver}').format( user_agent = u('wakatime/{ver} ({platform}) Python{py_ver}').format(
ver=__version__, ver=__version__,
platform=platform.platform(), platform=platform.platform(),
py_ver=python_version, py_ver=python_version,
) )
if plugin: if plugin:
user_agent = unicode('{user_agent} {plugin}').format( user_agent = u('{user_agent} {plugin}').format(
user_agent=user_agent, user_agent=user_agent,
plugin=plugin, plugin=plugin,
) )
@ -268,9 +264,9 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
if hidefilenames and targetFile is not None: if hidefilenames and targetFile is not None:
data['file'] = data['file'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1] data['file'] = data['file'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1]
if len(data['file'].strip('.').split('.', 1)) > 1: if len(data['file'].strip('.').split('.', 1)) > 1:
data['file'] = unicode('HIDDEN.{ext}').format(ext=data['file'].strip('.').rsplit('.', 1)[-1]) data['file'] = u('HIDDEN.{ext}').format(ext=data['file'].strip('.').rsplit('.', 1)[-1])
else: else:
data['file'] = unicode('HIDDEN') data['file'] = u('HIDDEN')
if stats.get('lines'): if stats.get('lines'):
data['lines'] = stats['lines'] data['lines'] = stats['lines']
if stats.get('language'): if stats.get('language'):
@ -284,16 +280,17 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
log.debug(data) log.debug(data)
# setup api request # setup api request
request = Request(url=url, data=str.encode(json.dumps(data))) request_body = json.dumps(data)
request = Request(url=url, data=str.encode(request_body) if is_py3 else request_body)
request.add_header('User-Agent', get_user_agent(plugin)) request.add_header('User-Agent', get_user_agent(plugin))
request.add_header('Content-Type', 'application/json') request.add_header('Content-Type', 'application/json')
auth = unicode('Basic {key}').format(key=bytes.decode(base64.b64encode(str.encode(key)))) auth = u('Basic {key}').format(key=u(base64.b64encode(str.encode(key) if is_py3 else key)))
request.add_header('Authorization', auth) request.add_header('Authorization', auth)
# add Olson timezone to request # add Olson timezone to request
tz = tzlocal.get_localzone() tz = tzlocal.get_localzone()
if tz: if tz:
request.add_header('TimeZone', unicode(tz.zone)) request.add_header('TimeZone', u(tz.zone))
# log time to api # log time to api
response = None response = None
@ -302,7 +299,7 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
except HTTPError as exc: except HTTPError as exc:
exception_data = { exception_data = {
'response_code': exc.getcode(), 'response_code': exc.getcode(),
sys.exc_info()[0].__name__: unicode(sys.exc_info()[1]), sys.exc_info()[0].__name__: u(sys.exc_info()[1]),
} }
if log.isEnabledFor(logging.DEBUG): if log.isEnabledFor(logging.DEBUG):
exception_data['traceback'] = traceback.format_exc() exception_data['traceback'] = traceback.format_exc()
@ -315,7 +312,7 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
log.error(exception_data) log.error(exception_data)
except: except:
exception_data = { exception_data = {
sys.exc_info()[0].__name__: unicode(sys.exc_info()[1]), sys.exc_info()[0].__name__: u(sys.exc_info()[1]),
} }
if log.isEnabledFor(logging.DEBUG): if log.isEnabledFor(logging.DEBUG):
exception_data['traceback'] = traceback.format_exc() exception_data['traceback'] = traceback.format_exc()
@ -365,7 +362,7 @@ def main(argv=None):
ignore = should_ignore(args.targetFile, args.ignore) ignore = should_ignore(args.targetFile, args.ignore)
if ignore is not False: if ignore is not False:
log.debug(unicode('File ignored because matches pattern: {pattern}').format( log.debug(u('File ignored because matches pattern: {pattern}').format(
pattern=ignore, pattern=ignore,
)) ))
return 0 return 0

View File

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
"""
wakatime.compat
~~~~~~~~~~~~~~~
For working with Python2 and Python3.
:copyright: (c) 2014 Alan Hamlett.
:license: BSD, see LICENSE for more details.
"""
import codecs
import io
import sys
is_py2 = (sys.version_info[0] == 2)
is_py3 = (sys.version_info[0] == 3)
if is_py2:
def u(text):
if isinstance(text, str):
return text.decode('utf-8')
return unicode(text)
open = codecs.open
basestring = basestring
elif is_py3:
def u(text):
if isinstance(text, bytes):
return text.decode('utf-8')
return str(text)
open = open
basestring = (str, bytes)

View File

@ -14,6 +14,7 @@ import os
import sys import sys
from .packages import simplejson as json from .packages import simplejson as json
from .compat import u
try: try:
from collections import OrderedDict from collections import OrderedDict
except ImportError: except ImportError:
@ -30,7 +31,7 @@ class CustomEncoder(json.JSONEncoder):
encoded = super(CustomEncoder, self).default(obj) encoded = super(CustomEncoder, self).default(obj)
except UnicodeDecodeError: except UnicodeDecodeError:
encoding = sys.getfilesystemencoding() encoding = sys.getfilesystemencoding()
obj = obj.decode(encoding, 'ignore').encode('utf-8') obj = u(obj)
encoded = super(CustomEncoder, self).default(obj) encoded = super(CustomEncoder, self).default(obj)
return encoded return encoded

View File

@ -13,18 +13,12 @@ import logging
import os import os
from .base import BaseProject from .base import BaseProject
from ..compat import u, open
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
# str is unicode in Python3
try:
unicode
except NameError:
unicode = str
class Git(BaseProject): class Git(BaseProject):
def process(self): def process(self):
@ -34,7 +28,7 @@ class Git(BaseProject):
def name(self): def name(self):
base = self._project_base() base = self._project_base()
if base: if base:
return unicode(os.path.basename(base)) return u(os.path.basename(base))
return None return None
def branch(self): def branch(self):
@ -42,8 +36,8 @@ class Git(BaseProject):
if base: if base:
head = os.path.join(self._project_base(), '.git', 'HEAD') head = os.path.join(self._project_base(), '.git', 'HEAD')
try: try:
with open(head) as fh: with open(head, 'r', encoding='utf-8') as fh:
return unicode(fh.readline().strip().rsplit('/', 1)[-1]) return u(fh.readline().strip().rsplit('/', 1)[-1])
except IOError: except IOError:
pass pass
return None return None

View File

@ -13,18 +13,12 @@ import logging
import os import os
from .base import BaseProject from .base import BaseProject
from ..compat import u, open
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
# str is unicode in Python3
try:
unicode
except NameError:
unicode = str
class Mercurial(BaseProject): class Mercurial(BaseProject):
def process(self): def process(self):
@ -33,18 +27,18 @@ class Mercurial(BaseProject):
def name(self): def name(self):
if self.configDir: if self.configDir:
return unicode(os.path.basename(os.path.dirname(self.configDir))) return u(os.path.basename(os.path.dirname(self.configDir)))
return None return None
def branch(self): def branch(self):
if self.configDir: if self.configDir:
branch_file = os.path.join(self.configDir, 'branch') branch_file = os.path.join(self.configDir, 'branch')
try: try:
with open(branch_file) as fh: with open(branch_file, 'r', encoding='utf-8') as fh:
return unicode(fh.readline().strip().rsplit('/', 1)[-1]) return u(fh.readline().strip().rsplit('/', 1)[-1])
except IOError: except IOError:
pass pass
return unicode('default') return u('default')
def _find_hg_config_dir(self, path): def _find_hg_config_dir(self, path):
path = os.path.realpath(path) path = os.path.realpath(path)

View File

@ -24,18 +24,12 @@ import logging
import os import os
from .base import BaseProject from .base import BaseProject
from ..compat import u
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
# str is unicode in Python3
try:
unicode
except NameError:
unicode = str
class ProjectMap(BaseProject): class ProjectMap(BaseProject):
def process(self): def process(self):
@ -68,5 +62,5 @@ class ProjectMap(BaseProject):
def name(self): def name(self):
if self.project: if self.project:
return unicode(self.project) return u(self.project)
return None return None

View File

@ -15,6 +15,7 @@ import platform
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from .base import BaseProject from .base import BaseProject
from ..compat import u, open
try: try:
from collections import OrderedDict from collections import OrderedDict
except ImportError: except ImportError:
@ -24,13 +25,6 @@ except ImportError:
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
# str is unicode in Python3
try:
unicode
except NameError:
unicode = str
class Subversion(BaseProject): class Subversion(BaseProject):
binary_location = None binary_location = None
@ -38,11 +32,11 @@ class Subversion(BaseProject):
return self._find_project_base(self.path) return self._find_project_base(self.path)
def name(self): def name(self):
return unicode(self.info['Repository Root'].split('/')[-1]) return u(self.info['Repository Root'].split('/')[-1])
def branch(self): def branch(self):
if self.base: if self.base:
unicode(os.path.basename(self.base)) u(os.path.basename(self.base))
return None return None
def _find_binary(self): def _find_binary(self):
@ -54,7 +48,7 @@ class Subversion(BaseProject):
'/usr/local/bin/svn', '/usr/local/bin/svn',
] ]
for location in locations: for location in locations:
with open(os.devnull, 'wb') as DEVNULL: with open(os.devnull, 'wb', encoding='utf-8') as DEVNULL:
try: try:
Popen([location, '--version'], stdout=DEVNULL, stderr=DEVNULL) Popen([location, '--version'], stdout=DEVNULL, stderr=DEVNULL)
self.binary_location = location self.binary_location = location

View File

@ -15,18 +15,12 @@ import logging
import os import os
from .base import BaseProject from .base import BaseProject
from ..compat import u, open
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
# str is unicode in Python3
try:
unicode
except NameError:
unicode = str
class WakaTime(BaseProject): class WakaTime(BaseProject):
def process(self): def process(self):
@ -37,9 +31,9 @@ class WakaTime(BaseProject):
if self.config: if self.config:
try: try:
with open(self.config) as fh: with open(self.config, 'r', encoding='utf-8') as fh:
self._project_name = unicode(fh.readline().strip()) self._project_name = u(fh.readline().strip())
self._project_branch = unicode(fh.readline().strip()) self._project_branch = u(fh.readline().strip())
except IOError as e: except IOError as e:
log.exception("Exception:") log.exception("Exception:")

View File

@ -13,6 +13,8 @@ import logging
import os import os
import sys import sys
from .compat import u, open
if sys.version_info[0] == 2: if sys.version_info[0] == 2:
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments2')) sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments2'))
else: else:
@ -22,11 +24,6 @@ from pygments.lexers import guess_lexer_for_filename
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
try:
unicode
except NameError:
unicode = str
# force file name extensions to be recognized as a certain language # force file name extensions to be recognized as a certain language
EXTENSIONS = { EXTENSIONS = {
@ -54,12 +51,12 @@ def guess_language(file_name):
return language return language
lexer = None lexer = None
try: try:
with open(file_name) as f: with open(file_name, 'r', encoding='utf-8') as fh:
lexer = guess_lexer_for_filename(file_name, f.read(512000)) lexer = guess_lexer_for_filename(file_name, fh.read(512000))
except: except:
pass pass
if lexer: if lexer:
return translate_language(unicode(lexer.name)) return translate_language(u(lexer.name))
else: else:
return None return None
@ -82,8 +79,8 @@ def translate_language(language):
def number_lines_in_file(file_name): def number_lines_in_file(file_name):
lines = 0 lines = 0
try: try:
with open(file_name) as f: with open(file_name, 'r', encoding='utf-8') as fh:
for line in f: for line in fh:
lines += 1 lines += 1
except IOError: except IOError:
return None return None