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

__all__ = ('PyTempl', 'PyModuleTempl')

import os
from os import path

from ulib.base.lines import Lines
from ulib.base.input import is_yes
from ulib.base.output import etitle, estep, eend
from base import Templ, FileTempl, register_templates

class PyTempl(FileTempl):
    TEMPLATE = ('python file', 'python', 'pyf', 'py')
    EXT = ('.py', '.pyw')

    VPS, ACPS, NCPS, ICPS = FileTempl.PS(icps=dict(utools=is_yes))

    utools = True

    MODE = u"python"

    SHEBANG = u"""#!/usr/bin/env python"""
    DOC = u'''\
u"""%%(scriptname)s:

USAGE
    %%(scriptname)s [options]"""
'''
    TRUEFALSE = u"""\
try: True, False
except: True, False = 1, 0
"""
    UTOOLS = u"""\
import os, sys
from os import path

from ulib.all import *

def display_help():
    uprint(__doc__ %% globals())

def run_%(basename)s():
    options, longoptions = build_options([
        ('h', 'help', "Afficher l'aide"),
        ])
    options, args = get_args(None, options, longoptions)
    for option, value in options:
        if option in ('-h', '--help'):
            display_help()
            sys.exit(0)

if __name__ == '__main__':
    run_%(basename)s()
"""
    ALL = u"""\
__all__ = ()
"""

    def get_lines(self):
        lines = Lines()
        modeline = self.get_modeline(nl=True)
        if self.executable:
            self.appendtext(lines, self.SHEBANG)
            self.appendtext(lines, modeline)
            self.appendtext(lines, self.DOC)
            #self.appendtext(lines, self.TRUEFALSE)
            if self.utools:
                self.appendtext(lines, self.UTOOLS)
        else:
            self.appendtext(lines, modeline)
            self.appendtext(lines, self.ALL)
            #self.appendtext(lines, self.TRUEFALSE)
        return lines

class PyModuleTempl(Templ):
    TEMPLATE = ('python module', 'pym')

    def create(self):
        def mkdir():
            if not path.isdir(self.file):
                os.mkdir(self.file)

        etitle(self.file)
        try:
            self._check_file()

            estep(u"Création du répertoire", mkdir)

            init = path.join(self.file, '__init__.py')
            PyTempl(init, **self.get_properties('nl', 'overwrite', 'encoding')).create()
        finally:
            eend()

register_templates(PyTempl, PyModuleTempl)
