diff --git a/wakatime/base.py b/wakatime/base.py index c49b81a..6138337 100644 --- a/wakatime/base.py +++ b/wakatime/base.py @@ -34,7 +34,7 @@ from .offlinequeue import Queue from .packages import argparse from .packages import simplejson as json from .packages.requests.exceptions import RequestException -from .project import find_project +from .project import get_project_info from .session_cache import SessionCache from .stats import get_file_stats try: @@ -443,20 +443,12 @@ def main(argv=None): stats = get_file_stats(args.targetFile, notfile=args.notfile, lineno=args.lineno, cursorpos=args.cursorpos) - project = None + project, branch = None, None if not args.notfile: - project = find_project(args.targetFile, configs=configs) - branch = None - project_name = args.project - if project: - branch = project.branch() - if not project_name: - project_name = project.name() - if not project_name: - project_name = args.alternate_project + project, branch = get_project_info(configs=configs, args=args) kwargs = vars(args) - kwargs['project'] = project_name + kwargs['project'] = project kwargs['branch'] = branch kwargs['stats'] = stats diff --git a/wakatime/project.py b/wakatime/project.py index 949a76d..bb8b503 100644 --- a/wakatime/project.py +++ b/wakatime/project.py @@ -15,30 +15,70 @@ from .projects.git import Git from .projects.mercurial import Mercurial from .projects.projectmap import ProjectMap from .projects.subversion import Subversion -from .projects.wakatime import WakaTime +from .projects.wakatime_project_file import WakaTimeProjectFile log = logging.getLogger('WakaTime') # List of plugin classes to find a project for the current file path. -# Project plugins will be processed with priority in the order below. -PLUGINS = [ - WakaTime, +CONFIG_PLUGINS = [ + WakaTimeProjectFile, ProjectMap, +] +REV_CONTROL_PLUGINS = [ Git, Mercurial, Subversion, ] -def find_project(path, configs=None): - for plugin in PLUGINS: - plugin_name = plugin.__name__.lower() - plugin_configs = None - if configs and configs.has_section(plugin_name): - plugin_configs = dict(configs.items(plugin_name)) - project = plugin(path, configs=plugin_configs) +def get_project_info(configs=None, args=None): + """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) + + project = plugin_cls(args.targetFile, configs=plugin_configs) if project.process(): - return project + project_name = project.name() + branch_name = project.branch() + break + + if project_name is None: + project_name = args.project + + 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) + + project = plugin_cls(args.targetFile, configs=plugin_configs) + if project.process(): + project_name = project_name or project.name() + branch_name = branch_name or project.branch() + break + + if project_name is None: + project_name = args.alternate_project + + 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)) return None diff --git a/wakatime/projects/wakatime.py b/wakatime/projects/wakatime_project_file.py similarity index 92% rename from wakatime/projects/wakatime.py rename to wakatime/projects/wakatime_project_file.py index 41ec0d6..97a4652 100644 --- a/wakatime/projects/wakatime.py +++ b/wakatime/projects/wakatime_project_file.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ - wakatime.projects.wakatime - ~~~~~~~~~~~~~~~~~~~~~~~~~~ + wakatime.projects.wakatime_project_file + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Information from a .wakatime-project file about the project for a given file. First line of .wakatime-project sets the project @@ -21,7 +21,7 @@ from ..compat import u, open log = logging.getLogger('WakaTime') -class WakaTime(BaseProject): +class WakaTimeProjectFile(BaseProject): def process(self): self.config = self._find_config(self.path)