From a203a1fc10440948abd5bc0c97fe87e85d9c282a Mon Sep 17 00:00:00 2001 From: Reed Jones Date: Sat, 16 Mar 2024 23:47:20 -0700 Subject: [PATCH] Update templates.py Handling directories in a way that works on both Linux and Windows --- django_code_generator/templates.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/django_code_generator/templates.py b/django_code_generator/templates.py index 85222fc..6f65925 100644 --- a/django_code_generator/templates.py +++ b/django_code_generator/templates.py @@ -1,9 +1,10 @@ import os from pathlib import Path - +from django.conf import settings from django.db import models from django.template import Engine, Context - +from django.template.loaders.app_directories import get_app_template_dirs +from django.template.backends.base import BaseEngine from django_code_generator.exceptions import DjangoCodeGeneratorError, TemplateNotFound from django.template.loader import render_to_string @@ -33,14 +34,17 @@ def walk(path): yield from walk(node) -def get_template_directory(directories, template): - for directory in filter(lambda x: bool(x), directories): - template_dir = os.path.join(directory, template) - if os.path.lexists(template_dir): - return template_dir + +def get_template_directory(template_dirs, template): + # template_dirs = get_template_dirs() + for lookup_dir in template_dirs: + for dir in Path(lookup_dir).iterdir(): + if dir.is_dir() and dir.name == template: + return dir raise TemplateNotFound(directories, template) + class Template: def __init__(self, directory, app_name): self.directory = directory @@ -48,7 +52,7 @@ def __init__(self, directory, app_name): installed_apps = dict(get_apps()) self.app = installed_apps.get(app_name) - + print(f"{self} init with \n : {app_name} \n : {directory}") if self.app is None: raise DjangoCodeGeneratorError('App {} is not available'.format(app_name)) @@ -58,8 +62,8 @@ def render(self): 'code_generator_tags': 'django_code_generator.templatetags.code_generator_tags' }) for node in walk(path): - relative_path = relative(str(path), str(node)) - to_path = os.path.join(self.app.path, relative_path) + relative_path = Path(node).absolute().relative_to(Path(self.directory).absolute()) + to_path = Path(self.app.path).joinpath(relative_path) if node.is_dir(): os.makedirs(to_path, exist_ok=True) else: