# Path: django_make_app/generators.py
# class TemplateFileAppGenerator(object):
#     def __init__(self, base_directory, templates_directory, app_data, app_structure):
#         self._base_directory = base_directory
#         self._templates_directory = templates_directory
#         self._app_data = app_data
#         self._app_structure = app_structure
# 
#     def optimize_source_codes(self):
#         path_to_app = os.path.normpath(os.path.join(self._base_directory, self._app_data.get(YamlSchemaKeywords.APP_NAME), "**/*.py"))
# 
#         for filename in glob2.glob(path_to_app):
#             logger.debug("Optimizing {}".format(filename))
#             optimize_code(filename)
# 
#     def generate_app(self):
#         self._create_app_structure(self._base_directory, self._app_structure)
# 
#     def _render(self, rendered_class, item):
#         return rendered_class(templates_directory=self._templates_directory, template_name=item.get(StructureKeyword.TEMPLATE_NAME), item=item).render(context=self._app_data)
# 
#     def _create_app_structure(self, base_directory, structure):
#         item_type = structure.get(StructureKeyword.TYPE)
#         item_name = structure.get(StructureKeyword.NAME)
#         is_folder = item_type == StructureKeyword.FOLDER
#         is_file = item_type == StructureKeyword.FILE
# 
#         target_path = os.path.join(base_directory, item_name)
# 
#         if is_folder:
#             new_base_dir = target_path
#             self._make_directory(target_path)
#         elif is_file:
#             new_base_dir = os.path.dirname(target_path)
#             self._make_file(structure, target_path)
#         else:
#             raise ValueError("Unknown item type {}".format(item_type))
# 
#         for node_obj in structure.get(StructureKeyword.ITEMS, []):
#             self._create_app_structure(new_base_dir, node_obj)
# 
#     def _make_file(self, structure, target_path):
#         renderer = structure.get(StructureKeyword.RENDERER)
# 
#         with io.open(target_path, encoding='utf-8', mode="w+") as the_file:
#             the_file.write(self._render(renderer, structure))
# 
#     def _make_directory(self, target_path):
#         os.mkdir(target_path)
# 
# Path: django_make_app/io_utils.py
# def read_yaml_file(filename):
#     with io.open(filename, mode='r', encoding='utf-8') as the_file:
#         return yaml.safe_load(the_file)
# 
# Path: django_make_app/schema.py
# def normalize_schema(innn):
#     """
#     :type innn: dict
#     :rtype: dict
#     """
#     app_name = innn.get(YamlSchemaKeywords.APP_NAME)
#     normalized_models = [i for i in normalize_models(innn.get(YamlSchemaKeywords.MODELS))]
# 
#     for model in normalized_models:
#         for field in model.get('fields'):
#             field['class'] = field['class'].replace("__app__", app_name)
# 
#     return {
#         YamlSchemaKeywords.APP_NAME: app_name,
#         YamlSchemaKeywords.MODELS: normalized_models
#     }
# 
# class YamlSchemaKeywords(object):
#     APPS = "apps"
#     APP_NAME = "name"
#     MODELS = "models"
# 
# Path: django_make_app/structure.py
# def get_structure(data):
#     return prepare_structure(APP_STRUCTURE, data)


import io
import logging
import os
import shutil
import click
from django_make_app.generators import TemplateFileAppGenerator
from django_make_app.io_utils import read_yaml_file
from django_make_app.schema import normalize_schema, YamlSchemaKeywords
from django_make_app.structure import get_structure
# -*- encoding: utf-8 -*-
# ! python2

from __future__ import (absolute_import, division, print_function)




logger = logging.getLogger(__name__)
click.disable_unicode_literals_warning = False

YAML_FILENAME = u"app_schema.yaml"


@click.group()
def cli():
    pass


@cli.command()
def write_config():
    templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), u"templates", u"example.yaml")

    with io.open(templates_dir, mode=u'r', encoding=u'utf-8') as the_file:
        click.echo(the_file.read())


@cli.command()
@click.argument(u'app')
@click.option(u'--force', is_flag=True, help=u"Overwrite app directory, do not validate app argument.")

# Path: django_make_app/generators.py
# class TemplateFileAppGenerator(object):
#     def __init__(self, base_directory, templates_directory, app_data, app_structure):
#         self._base_directory = base_directory
#         self._templates_directory = templates_directory
#         self._app_data = app_data
#         self._app_structure = app_structure
# 
#     def optimize_source_codes(self):
#         path_to_app = os.path.normpath(os.path.join(self._base_directory, self._app_data.get(YamlSchemaKeywords.APP_NAME), "**/*.py"))
# 
#         for filename in glob2.glob(path_to_app):
#             logger.debug("Optimizing {}".format(filename))
#             optimize_code(filename)
# 
#     def generate_app(self):
#         self._create_app_structure(self._base_directory, self._app_structure)
# 
#     def _render(self, rendered_class, item):
#         return rendered_class(templates_directory=self._templates_directory, template_name=item.get(StructureKeyword.TEMPLATE_NAME), item=item).render(context=self._app_data)
# 
#     def _create_app_structure(self, base_directory, structure):
#         item_type = structure.get(StructureKeyword.TYPE)
#         item_name = structure.get(StructureKeyword.NAME)
#         is_folder = item_type == StructureKeyword.FOLDER
#         is_file = item_type == StructureKeyword.FILE
# 
#         target_path = os.path.join(base_directory, item_name)
# 
#         if is_folder:
#             new_base_dir = target_path
#             self._make_directory(target_path)
#         elif is_file:
#             new_base_dir = os.path.dirname(target_path)
#             self._make_file(structure, target_path)
#         else:
#             raise ValueError("Unknown item type {}".format(item_type))
# 
#         for node_obj in structure.get(StructureKeyword.ITEMS, []):
#             self._create_app_structure(new_base_dir, node_obj)
# 
#     def _make_file(self, structure, target_path):
#         renderer = structure.get(StructureKeyword.RENDERER)
# 
#         with io.open(target_path, encoding='utf-8', mode="w+") as the_file:
#             the_file.write(self._render(renderer, structure))
# 
#     def _make_directory(self, target_path):
#         os.mkdir(target_path)
# 
# Path: django_make_app/io_utils.py
# def read_yaml_file(filename):
#     with io.open(filename, mode='r', encoding='utf-8') as the_file:
#         return yaml.safe_load(the_file)
# 
# Path: django_make_app/schema.py
# def normalize_schema(innn):
#     """
#     :type innn: dict
#     :rtype: dict
#     """
#     app_name = innn.get(YamlSchemaKeywords.APP_NAME)
#     normalized_models = [i for i in normalize_models(innn.get(YamlSchemaKeywords.MODELS))]
# 
#     for model in normalized_models:
#         for field in model.get('fields'):
#             field['class'] = field['class'].replace("__app__", app_name)
# 
#     return {
#         YamlSchemaKeywords.APP_NAME: app_name,
#         YamlSchemaKeywords.MODELS: normalized_models
#     }
# 
# class YamlSchemaKeywords(object):
#     APPS = "apps"
#     APP_NAME = "name"
#     MODELS = "models"
# 
# Path: django_make_app/structure.py
# def get_structure(data):
#     return prepare_structure(APP_STRUCTURE, data)


# Path: django_make_app/runner.py
import io
import logging
import os
import shutil
import click
from django_make_app.generators import TemplateFileAppGenerator
from django_make_app.io_utils import read_yaml_file
from django_make_app.schema import normalize_schema, YamlSchemaKeywords
from django_make_app.structure import get_structure

# -*- encoding: utf-8 -*-
# ! python2

from __future__ import (absolute_import, division, print_function)




logger = logging.getLogger(__name__)
click.disable_unicode_literals_warning = False

YAML_FILENAME = u"app_schema.yaml"


@click.group()
def cli():
    pass


@cli.command()
def write_config():
    templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), u"templates", u"example.yaml")

    with io.open(templates_dir, mode=u'r', encoding=u'utf-8') as the_file:
        click.echo(the_file.read())


@cli.command()
@click.argument(u'app')
@click.option(u'--force', is_flag=True, help=u"Overwrite app directory, do not validate app argument.")

@click.option(u'--no-optimize', is_flag=True, help=u"Do not optimize generated source code using yapf and isort, "