2013-11-14 06:06:55 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2015-06-30 05:47:04 +03:00
|
|
|
wakatime.projects.wakatime_project_file
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2013-11-14 06:06:55 +04:00
|
|
|
|
|
|
|
Information from a .wakatime-project file about the project for
|
2013-12-13 19:37:54 +04:00
|
|
|
a given file. First line of .wakatime-project sets the project
|
|
|
|
name. Second line sets the current branch name.
|
2013-11-14 06:06:55 +04:00
|
|
|
|
|
|
|
:copyright: (c) 2013 Alan Hamlett.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
2015-08-25 10:42:37 +03:00
|
|
|
import sys
|
2013-11-14 06:06:55 +04:00
|
|
|
|
|
|
|
from .base import BaseProject
|
2014-09-30 20:27:35 +04:00
|
|
|
from ..compat import u, open
|
2013-11-14 06:06:55 +04:00
|
|
|
|
|
|
|
|
2014-07-25 12:01:39 +04:00
|
|
|
log = logging.getLogger('WakaTime')
|
2013-11-14 06:06:55 +04:00
|
|
|
|
|
|
|
|
2015-06-30 05:47:04 +03:00
|
|
|
class WakaTimeProjectFile(BaseProject):
|
2013-11-14 06:06:55 +04:00
|
|
|
|
|
|
|
def process(self):
|
|
|
|
self.config = self._find_config(self.path)
|
2013-12-13 19:37:54 +04:00
|
|
|
self._project_name = None
|
|
|
|
self._project_branch = None
|
|
|
|
|
2013-11-14 06:06:55 +04:00
|
|
|
if self.config:
|
2013-12-13 19:37:54 +04:00
|
|
|
|
|
|
|
try:
|
2014-09-30 20:27:35 +04:00
|
|
|
with open(self.config, 'r', encoding='utf-8') as fh:
|
|
|
|
self._project_name = u(fh.readline().strip())
|
|
|
|
self._project_branch = u(fh.readline().strip())
|
2015-09-29 13:11:25 +03:00
|
|
|
except UnicodeDecodeError: # pragma: nocover
|
2015-08-25 10:42:37 +03:00
|
|
|
try:
|
|
|
|
with open(self.config, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
|
|
|
self._project_name = u(fh.readline().strip())
|
|
|
|
self._project_branch = u(fh.readline().strip())
|
|
|
|
except:
|
2016-09-02 11:50:54 +03:00
|
|
|
log.traceback(logging.WARNING)
|
2015-09-29 13:11:25 +03:00
|
|
|
except IOError: # pragma: nocover
|
2016-09-02 11:50:54 +03:00
|
|
|
log.traceback(logging.WARNING)
|
2013-12-13 19:37:54 +04:00
|
|
|
|
2013-11-14 06:06:55 +04:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def name(self):
|
2013-12-13 19:37:54 +04:00
|
|
|
return self._project_name
|
2013-11-14 06:06:55 +04:00
|
|
|
|
|
|
|
def branch(self):
|
2013-12-13 19:37:54 +04:00
|
|
|
return self._project_branch
|
2013-11-14 06:06:55 +04:00
|
|
|
|
|
|
|
def _find_config(self, path):
|
|
|
|
path = os.path.realpath(path)
|
|
|
|
if os.path.isfile(path):
|
|
|
|
path = os.path.split(path)[0]
|
|
|
|
if os.path.isfile(os.path.join(path, '.wakatime-project')):
|
|
|
|
return os.path.join(path, '.wakatime-project')
|
|
|
|
split_path = os.path.split(path)
|
|
|
|
if split_path[1] == '':
|
|
|
|
return None
|
|
|
|
return self._find_config(split_path[0])
|