support regex patterns in projectmap config section

This commit is contained in:
Alan Hamlett 2016-04-18 21:34:00 +02:00
parent 5466fa478a
commit 615fa5cdac
4 changed files with 43 additions and 28 deletions

View file

@ -2,3 +2,5 @@
debug = false debug = false
api_key = 1234 api_key = 1234
[projectmap] [projectmap]
samples/projects/proj.{3}_map/ = proj-map
samples/projects/project_map(\d+)/ = proj-map{0}

View file

@ -160,18 +160,27 @@ class LanguagesTestCase(utils.TestCase):
response.status_code = 0 response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with tempfile.NamedTemporaryFile() as fh: now = u(int(time.time()))
now = u(int(time.time())) entity = 'tests/samples/projects/project_map/emptyfile.txt'
entity = 'tests/samples/projects/project_map/emptyfile.txt' config = 'tests/samples/configs/project_map.cfg'
fh.write(open('tests/samples/configs/project_map.cfg').read().encode('utf-8')) args = ['--file', entity, '--config', config, '--time', now]
fh.write('{0} = proj-map'.format(os.path.realpath(os.path.dirname(os.path.dirname(entity)))).encode('utf-8'))
fh.flush()
config = fh.name execute(args)
args = ['--file', entity, '--config', config, '--time', now] self.assertEquals('proj-map', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
execute(args) def test_project_map_group_usage(self):
response = Response()
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
self.assertEquals('proj-map', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project']) now = u(int(time.time()))
entity = 'tests/samples/projects/project_map42/emptyfile.txt'
config = 'tests/samples/configs/project_map.cfg'
args = ['--file', entity, '--config', config, '--time', now]
execute(args)
self.assertEquals('proj-map42', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])

View file

@ -3,18 +3,18 @@
wakatime.projects.projectmap wakatime.projects.projectmap
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Use the ~/.wakatime.cfg file to set custom project names by Use the ~/.wakatime.cfg file to set custom project names by matching files
recursively matching folder paths. with regex patterns. Project maps go under the [projectmap] config section.
Project maps go under the [projectmap] config section.
For example: For example:
[projectmap] [projectmap]
/home/user/projects/foo = new project name /home/user/projects/foo = new project name
/home/user/projects/bar = project2 /home/user/projects/bar(\d+)/ = project{0}
Will result in file `/home/user/projects/foo/src/main.c` to have Will result in file `/home/user/projects/foo/src/main.c` to have
project name `new project name`. project name `new project name` and file `/home/user/projects/bar42/main.c`
to have project name `project42`.
:copyright: (c) 2013 Alan Hamlett. :copyright: (c) 2013 Alan Hamlett.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
@ -22,6 +22,7 @@
import logging import logging
import os import os
import re
from .base import BaseProject from .base import BaseProject
from ..compat import u from ..compat import u
@ -42,20 +43,23 @@ class ProjectMap(BaseProject):
def _find_project(self, path): def _find_project(self, path):
path = os.path.realpath(path) path = os.path.realpath(path)
if os.path.isfile(path):
path = os.path.split(path)[0]
if self._configs.get(path.lower()): try:
return self._configs.get(path.lower()) for pattern, new_proj_name in self._configs.items():
if self._configs.get('%s/' % path.lower()): # pragma: nocover try:
return self._configs.get('%s/' % path.lower()) compiled = re.compile(pattern, re.IGNORECASE)
if self._configs.get('%s\\' % path.lower()): # pragma: nocover match = compiled.search(path)
return self._configs.get('%s\\' % path.lower()) 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),
))
except TypeError: # pragma: nocover
pass
split_path = os.path.split(path) return None
if split_path[1] == '':
return None # pragma: nocover
return self._find_project(split_path[0])
def branch(self): def branch(self):
return None return None
@ -63,4 +67,4 @@ class ProjectMap(BaseProject):
def name(self): def name(self):
if self.project: if self.project:
return u(self.project) return u(self.project)
return None # pragma: nocover return None