# 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/renderers.py
# class TemplateRenderer(object):
#     def __init__(self, templates_directory, template_name, item):
#         """
#         :type templates_directory: os.path
#         :type template_name: unicode
#         :type item: dict
#         """
#         self._templates_directory = templates_directory
#         self.template_name = template_name
#         self.item = item
# 
#     def _render_from_template(self, template_name, **kwargs):
#         loader = FileSystemLoader(self._templates_directory)
#         env = Environment(loader=loader)
#         template = env.get_template(template_name)
# 
#         render = template.render(**kwargs)
# 
#         render = render.replace("[[", "{{")
#         render = render.replace("]]", "}}")
# 
#         render = render.replace("[%", "{%")
#         render = render.replace("%]", "%}")
# 
#         return render
# 
#     def render(self, context):
#         """
# 
#         :type context: dict
#         """
#         if "_model" in self.item:
#             context.update({
#                 "current_model": self.item.get('_model')
#             })
# 
#         return self._render_from_template(self.template_name, **context)


import os
import shutil
import tempfile
from unittest import TestCase
from django_make_app.generators import TemplateFileAppGenerator
from django_make_app.renderers import TemplateRenderer
        ]

        for path_to_check in expected_generated_items:
            self.assertTrue(os.path.exists(path_to_check), msg="{0} does not exist".format(path_to_check))

    def test_generate_with_invalid_structure(self):
        this_dir = os.path.dirname(os.path.realpath(__file__))
        templates_dir = os.path.join(this_dir, "..", "django_make_app", "templates")

        expected_structure = {
            "type": "folder",
            "name": "web_app",
            "items": [
                {
                    "type": "unicorn",
                    "name": "__init__.py",
                    "template_name": "__init__.py.jinja2.html",
                    "renderer": TemplateRenderer
                }
            ]
        }

        in_app_schema = {
            "name": "web_app",
            "models": [
                {"name": "Article"},
                {"name": "Book"}
            ]
        }


# 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/renderers.py
# class TemplateRenderer(object):
#     def __init__(self, templates_directory, template_name, item):
#         """
#         :type templates_directory: os.path
#         :type template_name: unicode
#         :type item: dict
#         """
#         self._templates_directory = templates_directory
#         self.template_name = template_name
#         self.item = item
# 
#     def _render_from_template(self, template_name, **kwargs):
#         loader = FileSystemLoader(self._templates_directory)
#         env = Environment(loader=loader)
#         template = env.get_template(template_name)
# 
#         render = template.render(**kwargs)
# 
#         render = render.replace("[[", "{{")
#         render = render.replace("]]", "}}")
# 
#         render = render.replace("[%", "{%")
#         render = render.replace("%]", "%}")
# 
#         return render
# 
#     def render(self, context):
#         """
# 
#         :type context: dict
#         """
#         if "_model" in self.item:
#             context.update({
#                 "current_model": self.item.get('_model')
#             })
# 
#         return self._render_from_template(self.template_name, **context)


# Path: tests/test_generators.py
import os
import shutil
import tempfile
from unittest import TestCase
from django_make_app.generators import TemplateFileAppGenerator
from django_make_app.renderers import TemplateRenderer

        ]

        for path_to_check in expected_generated_items:
            self.assertTrue(os.path.exists(path_to_check), msg="{0} does not exist".format(path_to_check))

    def test_generate_with_invalid_structure(self):
        this_dir = os.path.dirname(os.path.realpath(__file__))
        templates_dir = os.path.join(this_dir, "..", "django_make_app", "templates")

        expected_structure = {
            "type": "folder",
            "name": "web_app",
            "items": [
                {
                    "type": "unicorn",
                    "name": "__init__.py",
                    "template_name": "__init__.py.jinja2.html",
                    "renderer": TemplateRenderer
                }
            ]
        }

        in_app_schema = {
            "name": "web_app",
            "models": [
                {"name": "Article"},
                {"name": "Book"}
            ]
        }


        app_generator = TemplateFileAppGenerator(self.dirpath, templates_dir, in_app_schema, expected_structure)