sublime-wakatime/packages/wakatime/projects/projectfile.py

56 lines
1.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
2017-11-06 06:51:43 +03:00
wakatime.projects.projectfile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Information from a .wakatime-project file about the project for
a given file. First line of .wakatime-project sets the project
name. Second line sets the current branch name.
:copyright: (c) 2013 Alan Hamlett.
:license: BSD, see LICENSE for more details.
"""
import logging
2015-08-25 10:42:37 +03:00
import sys
from .base import BaseProject
2014-09-30 20:27:35 +04:00
from ..compat import u, open
2018-01-05 10:33:07 +03:00
from ..utils import find_project_file
log = logging.getLogger('WakaTime')
2017-11-06 06:51:43 +03:00
class ProjectFile(BaseProject):
def process(self):
2018-01-05 10:33:07 +03:00
self.config = find_project_file(self.path)
self._project_name = None
self._project_branch = None
if self.config:
try:
2014-09-30 20:27:35 +04:00
with open(self.config, 'r', encoding='utf-8') as fh:
2018-01-05 10:33:07 +03:00
self._project_name = u(fh.readline().strip()) or None
self._project_branch = u(fh.readline().strip()) or None
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:
2018-01-05 10:33:07 +03:00
self._project_name = u(fh.readline().strip()) or None
self._project_branch = u(fh.readline().strip()) or None
2015-08-25 10:42:37 +03:00
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)
return True
return False
def name(self):
return self._project_name
def branch(self):
return self._project_branch