Skip to content

Commit

Permalink
cache jinja2 template (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Dec 29, 2020
1 parent 305e166 commit a968171
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions datamodel_code_generator/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
from abc import ABC, abstractmethod
from collections import defaultdict
from functools import lru_cache
from pathlib import Path
from typing import Any, DefaultDict, Dict, Iterator, List, Optional, Set

Expand Down Expand Up @@ -86,14 +87,17 @@ def represented_default(self) -> str:
return repr(self.default)


@lru_cache()
def get_template(template_file_path: Path) -> Template:
loader = FileSystemLoader(str(TEMPLATE_DIR / template_file_path.parent))
environment: Environment = Environment(loader=loader)
return environment.get_template(template_file_path.name)


class TemplateBase(ABC):
def __init__(self, template_file_path: Path) -> None:
self.template_file_path: Path = template_file_path
loader = FileSystemLoader(str(TEMPLATE_DIR / template_file_path.parent))
self.environment: Environment = Environment(loader=loader)
self._template: Template = self.environment.get_template(
template_file_path.name
)
self._template: Template = get_template(template_file_path)

@property
def template(self) -> Template:
Expand Down

0 comments on commit a968171

Please sign in to comment.