new compat file for python2 and python3 compatibility. read files as utf-8.

This commit is contained in:
Alan Hamlett 2014-09-30 09:09:30 -07:00
parent 138be33349
commit 4bad3541d9
9 changed files with 82 additions and 79 deletions

View file

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

View file

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

View file

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

View file

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

View file

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