2013-12-13 14:35:49 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
wakatime.projects.projectmap
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2016-04-18 22:26:28 +00:00
|
|
|
Use the ~/.wakatime.cfg file to set custom project names by matching files
|
|
|
|
with regex patterns. Project maps go under the [projectmap] config section.
|
2013-12-13 14:35:49 +00:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
[projectmap]
|
|
|
|
/home/user/projects/foo = new project name
|
2016-04-18 22:26:28 +00:00
|
|
|
/home/user/projects/bar(\d+)/ = project{0}
|
2013-12-13 14:35:49 +00:00
|
|
|
|
|
|
|
Will result in file `/home/user/projects/foo/src/main.c` to have
|
2016-04-18 22:26:28 +00:00
|
|
|
project name `new project name` and file `/home/user/projects/bar42/main.c`
|
|
|
|
to have project name `project42`.
|
2013-12-13 14:35:49 +00:00
|
|
|
|
|
|
|
:copyright: (c) 2013 Alan Hamlett.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
2016-04-18 22:26:28 +00:00
|
|
|
import re
|
2013-12-13 14:35:49 +00:00
|
|
|
|
|
|
|
from .base import BaseProject
|
2014-09-30 16:27:35 +00:00
|
|
|
from ..compat import u
|
2013-12-13 14:35:49 +00:00
|
|
|
|
|
|
|
|
2014-07-25 08:01:39 +00:00
|
|
|
log = logging.getLogger('WakaTime')
|
2013-12-13 14:35:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProjectMap(BaseProject):
|
|
|
|
|
|
|
|
def process(self):
|
|
|
|
if not self._configs:
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.project = self._find_project(self.path)
|
|
|
|
|
|
|
|
return self.project is not None
|
|
|
|
|
|
|
|
def _find_project(self, path):
|
|
|
|
path = os.path.realpath(path)
|
|
|
|
|
2016-04-18 22:26:28 +00:00
|
|
|
try:
|
|
|
|
for pattern, new_proj_name in self._configs.items():
|
|
|
|
try:
|
|
|
|
compiled = re.compile(pattern, re.IGNORECASE)
|
|
|
|
match = compiled.search(path)
|
|
|
|
if match:
|
|
|
|
return new_proj_name.format(*match.groups())
|
|
|
|
except re.error as ex:
|
|
|
|
log.warning(u('Regex error ({msg}) for projectmap pattern: {pattern}').format(
|
|
|
|
msg=u(ex),
|
|
|
|
pattern=u(pattern),
|
|
|
|
))
|
2016-07-06 21:25:21 +00:00
|
|
|
except TypeError:
|
2016-04-18 22:26:28 +00:00
|
|
|
pass
|
2013-12-13 14:35:49 +00:00
|
|
|
|
2016-04-18 22:26:28 +00:00
|
|
|
return None
|
2013-12-13 14:35:49 +00:00
|
|
|
|
|
|
|
def branch(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
if self.project:
|
2014-09-30 16:27:35 +00:00
|
|
|
return u(self.project)
|
2016-04-18 22:26:28 +00:00
|
|
|
return None
|