2013-07-08 05:38:01 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
wakatime.project
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Returns a project for the given file.
|
|
|
|
|
|
|
|
:copyright: (c) 2013 Alan Hamlett.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from .projects.git import Git
|
|
|
|
from .projects.mercurial import Mercurial
|
2013-12-13 18:35:49 +04:00
|
|
|
from .projects.projectmap import ProjectMap
|
2013-07-08 05:38:01 +04:00
|
|
|
from .projects.subversion import Subversion
|
2015-06-30 05:47:04 +03:00
|
|
|
from .projects.wakatime_project_file import WakaTimeProjectFile
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
|
2014-07-25 12:01:39 +04:00
|
|
|
log = logging.getLogger('WakaTime')
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-12-13 18:35:49 +04:00
|
|
|
|
|
|
|
# List of plugin classes to find a project for the current file path.
|
2015-06-30 05:47:04 +03:00
|
|
|
CONFIG_PLUGINS = [
|
|
|
|
WakaTimeProjectFile,
|
2013-12-13 18:35:49 +04:00
|
|
|
ProjectMap,
|
2015-06-30 05:47:04 +03:00
|
|
|
]
|
|
|
|
REV_CONTROL_PLUGINS = [
|
2013-07-08 05:38:01 +04:00
|
|
|
Git,
|
|
|
|
Mercurial,
|
|
|
|
Subversion,
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2016-04-29 01:04:46 +03:00
|
|
|
def get_project_info(configs, heartbeat):
|
2015-06-30 05:47:04 +03:00
|
|
|
"""Find the current project and branch.
|
|
|
|
|
|
|
|
First looks for a .wakatime-project file. Second, uses the --project arg.
|
|
|
|
Third, uses the folder name from a revision control repository. Last, uses
|
|
|
|
the --alternate-project arg.
|
|
|
|
|
|
|
|
Returns a project, branch tuple.
|
|
|
|
"""
|
|
|
|
|
|
|
|
project_name, branch_name = None, None
|
|
|
|
|
|
|
|
for plugin_cls in CONFIG_PLUGINS:
|
|
|
|
|
|
|
|
plugin_name = plugin_cls.__name__.lower()
|
|
|
|
plugin_configs = get_configs_for_plugin(plugin_name, configs)
|
|
|
|
|
2016-04-29 01:04:46 +03:00
|
|
|
project = plugin_cls(heartbeat['entity'], configs=plugin_configs)
|
2013-07-10 11:14:44 +04:00
|
|
|
if project.process():
|
2015-09-29 13:11:25 +03:00
|
|
|
project_name = project_name or project.name()
|
2015-06-30 05:47:04 +03:00
|
|
|
branch_name = project.branch()
|
|
|
|
break
|
|
|
|
|
|
|
|
if project_name is None:
|
2016-04-29 01:04:46 +03:00
|
|
|
project_name = heartbeat.get('project')
|
2015-06-30 05:47:04 +03:00
|
|
|
|
|
|
|
if project_name is None or branch_name is None:
|
|
|
|
|
|
|
|
for plugin_cls in REV_CONTROL_PLUGINS:
|
|
|
|
|
|
|
|
plugin_name = plugin_cls.__name__.lower()
|
|
|
|
plugin_configs = get_configs_for_plugin(plugin_name, configs)
|
|
|
|
|
2016-04-29 01:04:46 +03:00
|
|
|
project = plugin_cls(heartbeat['entity'], configs=plugin_configs)
|
2015-06-30 05:47:04 +03:00
|
|
|
if project.process():
|
|
|
|
project_name = project_name or project.name()
|
|
|
|
branch_name = branch_name or project.branch()
|
|
|
|
break
|
|
|
|
|
|
|
|
if project_name is None:
|
2016-04-29 01:04:46 +03:00
|
|
|
project_name = heartbeat.get('alternate_project')
|
2015-06-30 05:47:04 +03:00
|
|
|
|
|
|
|
return project_name, branch_name
|
|
|
|
|
|
|
|
|
|
|
|
def get_configs_for_plugin(plugin_name, configs):
|
|
|
|
if configs and configs.has_section(plugin_name):
|
|
|
|
return dict(configs.items(plugin_name))
|
2013-07-21 21:44:23 +04:00
|
|
|
return None
|