# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
import ulib.base.i_need_py23

__all__ = ()

import os
from os import path

from tasksrc import TasksrcFile

class Project(object):
    _cwd = None
    _tsrc = None
    _projsdir = None
    _projdir = None

    def __init__(self, cwd=None, tsrc=None):
        if cwd is None: cwd = os.getcwd()
        self._cwd = cwd
        if not isinstance(tsrc, TasksrcFile):
            tsrc = TasksrcFile(tsrc)
        self._tsrc = tsrc

    def _compute(self):
        pass

    def is_in_projdir(self):
        u"""Tester si le répertoire courant se trouve dans un répertoire de
        projet.
        """
        if self._projdir is None: self._compute()
        return self._projdir is not None

    def get_projsdir(self):
        u"""Retourner le répertoire des projets correspondant au répertoire courant.
        """
        if self._projsdir is None: self._compute()
        return self._projsdir

    def get_projdir(self):
        u"""Retourner le répertoire de projet correspondant au répertoire
        courant.

        @return: le répertoire de projet, ou None si le répertoire courant
        n'est pas dans un répertoire de projet.
        """
        if self._projdir is None: self._compute()
        return self._projdir

    WIKI1DIR = "doc/wiki"
    WIKI2DIR = "wiki"

    def _get_file(self, name):
        projdir = self.get_projdir()
        if projdir is None: return None
        for dir in (self.WIKI1DIR, self.WIKI2DIR, None):
            basedir = projdir
            if dir is not None: basedir = path.join(basedir, dir)
            tasksfile = path.join(basedir, name)
            if path.exists(tasksfile):
                return tasksfile
        return None

    TASKSFILE = "Tasks.txt"
    def get_tasksfile(self):
        u"""
        """
        return self._get_file(self.TASKSFILE)

    PURGEDFILE = "Purged.txt"
    def get_purgedfile(self):
        u"""
        """
        return self._get_file(self.PURGEDFILE)
