"""Deal with one profile documentation

Copyright (c) 2015 Francesco Montesano
MIT Licence
"""

import os
import subprocess as sp

import dodocs.config as dconf
import dodocs.logger as dlog
import dodocs.utils as dutils

from dodocs.mkdoc import vcs
from dodocs.mkdoc import pyvenvex


def main(profile, args):
    """Make the documentation for on project

    Parameters
    ----------
    profile : string
        name of the profile
    args : namespace
        parsed command line arguments
    """
    log = dlog.getLogger()

    for s in dconf.get_projects(profile):
        log.debug("building project %s", s)
        try:
            build_project(profile, s, args)
        except Exception:
            msg = "something bad happened"
            log.exception(msg)
            continue


def build_project(profile, project, args):
    """Build the project documentation.

    * Fetch the code
    * if it's python:
        * make a virtualenv, if it does not exists already
        * pip install -e
    * build the documentation
    * if it's python:
        * deactivate the virtualenv

    Parameters
    ----------
    profile : string
        name of the profile
    project : string
        name of the project
    args : namespace
        parsed command line arguments
    """
    conf = dconf.get_config(profile)
    log = dlog.getLogger()

    # create the directory
    dutils.mk_project(profile, project)

    # cd into it
    with dutils.cd_project(profile, project):
        log.debug("cd'ed into the project '%s' directory", project)
        vcs_type = conf.get(project, "vcs")
        project_path = conf.get(project, "project_path")

        vcs.get_or_update_source(vcs_type, project_path)
        log.debug("%s repository updated", vcs_type)

        # if the project is python deal with virtual environment
        language = conf.get(project, "language").lower()
        if language == "python3":
            venv_bin = pyvenvex.venv_bin(profile, language)
            log.debug("virtualenv bin directory '%s'", venv_bin)
        else:
            raise ValueError("for now I don't know with language"
                             " {}".format(language))

        # if it's any python, install it in developer mode
        py_install = conf.get(project, "py-install")
        log.debug("install %s? %s", project, py_install)
        if ("python" in language and
                py_install.lower() not in ['no', 'none']):
            pip = os.path.join(venv_bin, 'pip')
            cmd = [pip, 'install', '-e']
            if py_install.lower() == 'yes':
                cmd += ['.']
            else:
                cmd += ['.[{}]'.format(py_install)]
            log.debug("running '%s'", " ".join(cmd))

            p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE,
                         universal_newlines=True)
            stdout, stderr = p.communicate()
            if stdout:
                log.debug(str(stdout).replace('\n\n', '\n'))
            if stderr:
                log.error(stderr)
            if p.returncode > 0:
                log.error("pip install return code '%d'", p.returncode)

        # build the documentation

@@ -93,43 +93,8 @@
 %22%22%0A%0A
-import os%0Aimport subprocess as sp%0A%0A
 impo
@@ -208,45 +208,16 @@
 ort 
-vcs%0Afrom dodocs.mkdoc import pyvenvex
+builders
 %0A%0A%0Ad
@@ -1489,1621 +1489,131 @@
 ct)%0A
-        vcs_type = conf.get(project, %22vcs%22)%0A        project_path = conf.get(project, %22project_path%22)%0A%0A        vcs.get_or_update_source(vcs_type, project_path)%0A        log.debug(%22%25s repository updated%22, vcs_type)%0A%0A        # if the project is python deal with virtual environment%0A        language = conf.get(project, %22language%22).lower()%0A        if language == %22python3%22:%0A            venv_bin = pyvenvex.venv_bin(profile, language)%0A            log.debug(%22virtualenv bin directory '%25s'%22, venv_bin)%0A        else:%0A            raise ValueError(%22for now I don't know with language%22%0A                             %22 %7B%7D%22.format(language))%0A%0A        # if it's any python, install it in developer mode%0A        py_install = conf.get(project, %22py-install%22)%0A        log.debug(%22install %25s? %25s%22, project, py_install)%0A        if (%22python%22 in language and%0A                py_install.lower() not in %5B'no', 'none'%5D):%0A            pip = os.path.join(venv_bin, 'pip')%0A            cmd = %5Bpip, 'install', '-e'%5D%0A            if py_install.lower() == 'yes':%0A                cmd += %5B'.'%5D%0A            else:%0A                cmd += %5B'.%5B%7B%7D%5D'.format(py_install)%5D%0A            log.debug(%22running '%25s'%22, %22 %22.join(cmd))%0A%0A            p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE,%0A                         universal_newlines=True)%0A            stdout, stderr = p.communicate()%0A            if stdout:%0A                log.debug(str(stdout).replace('%5Cn%5Cn', '%5Cn'))%0A            if stderr:%0A                log.error(stderr)%0A            if p.returncode %3E 0:%0A                log.error(%22pip install return code '%25d'%22, p.returncode)%0A%0A        # build the documentation
+%0A        builder = builders.picker(profile, project, conf, log)%0A%0A        # build the documentation%0A        builder.build_doc()
 %0A

Move most of the code to the builders