From 2681c4801d3be420ff42ff8f8877e25b95610470 Mon Sep 17 00:00:00 2001 From: Karol Zlot <11590960+karolzlot@users.noreply.github.com> Date: Mon, 6 Jun 2022 23:44:07 +0200 Subject: [PATCH 1/4] Add first type hints --- docxtpl/template.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/docxtpl/template.py b/docxtpl/template.py index 79be84a..4467a09 100644 --- a/docxtpl/template.py +++ b/docxtpl/template.py @@ -5,6 +5,8 @@ Created : 2015-03-12 @author: Eric Lapouyade """ +from os import PathLike +from typing import Any, Optional, IO, Union, Dict from .subdoc import Subdoc import functools import io @@ -34,8 +36,8 @@ class DocxTemplate(object): HEADER_URI = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" FOOTER_URI = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" - def __init__(self, template_file): - self.template_file = template_file + def __init__(self, template_file_or_path: Union[IO[bytes], str, PathLike[str]]): + self.template_file_or_path = template_file_or_path self.reset_replacements() self.docx = None self.is_rendered = False @@ -43,7 +45,7 @@ class DocxTemplate(object): def init_docx(self): if not self.docx or self.is_rendered: - self.docx = Document(self.template_file) + self.docx = Document(self.template_file_or_path) self.is_rendered = False def render_init(self): @@ -322,7 +324,7 @@ class DocxTemplate(object): new_part.load_rel(rel.reltype, rel._target, rel.rId, rel.is_external) self.docx._part._rels[relKey]._target = new_part - def render(self, context, jinja_env=None, autoescape=False): + def render(self, context: Dict[str, Any], jinja_env: Optional[Environment]=None, autoescape: bool=False): # init template working attributes self.render_init() @@ -710,14 +712,14 @@ class DocxTemplate(object): return self.docx._part.relate_to(url, REL_TYPE.HYPERLINK, is_external=True) - def save(self, filename, *args, **kwargs): + def save(self, output_file_or_path: Union[IO[bytes], str, PathLike[str]], *args, **kwargs): # case where save() is called without doing rendering # ( user wants only to replace image/embedded/zipname ) if not self.is_saved and not self.is_rendered: - self.docx = Document(self.template_file) + self.docx = Document(self.template_file_or_path) self.pre_processing() - self.docx.save(filename, *args, **kwargs) - self.post_processing(filename) + self.docx.save(output_file_or_path, *args, **kwargs) + self.post_processing(output_file_or_path) self.is_saved = True def get_undeclared_template_variables(self, jinja_env=None): From 43ceb384657d3c10df362c55617d0fa4d0c73801 Mon Sep 17 00:00:00 2001 From: Karol Zlot <11590960+karolzlot@users.noreply.github.com> Date: Mon, 6 Jun 2022 23:49:48 +0200 Subject: [PATCH 2/4] Add type hints to get_undeclared_template_variables --- docxtpl/template.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docxtpl/template.py b/docxtpl/template.py index 4467a09..cb8219d 100644 --- a/docxtpl/template.py +++ b/docxtpl/template.py @@ -6,7 +6,7 @@ Created : 2015-03-12 """ from os import PathLike -from typing import Any, Optional, IO, Union, Dict +from typing import Any, Optional, IO, Union, Dict, Set from .subdoc import Subdoc import functools import io @@ -36,7 +36,7 @@ class DocxTemplate(object): HEADER_URI = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" FOOTER_URI = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" - def __init__(self, template_file_or_path: Union[IO[bytes], str, PathLike[str]]): + def __init__(self, template_file_or_path: Union[IO[bytes], str, PathLike[str]]) -> None: self.template_file_or_path = template_file_or_path self.reset_replacements() self.docx = None @@ -324,7 +324,7 @@ class DocxTemplate(object): new_part.load_rel(rel.reltype, rel._target, rel.rId, rel.is_external) self.docx._part._rels[relKey]._target = new_part - def render(self, context: Dict[str, Any], jinja_env: Optional[Environment]=None, autoescape: bool=False): + def render(self, context: Dict[str, Any], jinja_env: Optional[Environment]=None, autoescape: bool=False) -> None: # init template working attributes self.render_init() @@ -722,7 +722,7 @@ class DocxTemplate(object): self.post_processing(output_file_or_path) self.is_saved = True - def get_undeclared_template_variables(self, jinja_env=None): + def get_undeclared_template_variables(self, jinja_env: Optional[Environment]=None) -> Set[str]: self.init_docx() xml = self.get_xml() xml = self.patch_xml(xml) From 25a6e42850ff92f175949cf148f55b823181d26c Mon Sep 17 00:00:00 2001 From: Karol Zlot <11590960+karolzlot@users.noreply.github.com> Date: Sun, 12 Jun 2022 10:43:03 +0200 Subject: [PATCH 3/4] Reverse name changes --- docxtpl/template.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docxtpl/template.py b/docxtpl/template.py index cb8219d..a5ff6fa 100644 --- a/docxtpl/template.py +++ b/docxtpl/template.py @@ -36,8 +36,8 @@ class DocxTemplate(object): HEADER_URI = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" FOOTER_URI = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" - def __init__(self, template_file_or_path: Union[IO[bytes], str, PathLike[str]]) -> None: - self.template_file_or_path = template_file_or_path + def __init__(self, template_file: Union[IO[bytes], str, PathLike[str]]) -> None: + self.template_file = template_file self.reset_replacements() self.docx = None self.is_rendered = False @@ -45,7 +45,7 @@ class DocxTemplate(object): def init_docx(self): if not self.docx or self.is_rendered: - self.docx = Document(self.template_file_or_path) + self.docx = Document(self.template_file) self.is_rendered = False def render_init(self): @@ -712,14 +712,14 @@ class DocxTemplate(object): return self.docx._part.relate_to(url, REL_TYPE.HYPERLINK, is_external=True) - def save(self, output_file_or_path: Union[IO[bytes], str, PathLike[str]], *args, **kwargs): + def save(self, filename: Union[IO[bytes], str, PathLike[str]], *args, **kwargs): # case where save() is called without doing rendering # ( user wants only to replace image/embedded/zipname ) if not self.is_saved and not self.is_rendered: - self.docx = Document(self.template_file_or_path) + self.docx = Document(self.template_file) self.pre_processing() - self.docx.save(output_file_or_path, *args, **kwargs) - self.post_processing(output_file_or_path) + self.docx.save(filename, *args, **kwargs) + self.post_processing(filename) self.is_saved = True def get_undeclared_template_variables(self, jinja_env: Optional[Environment]=None) -> Set[str]: From bda05f2101eca2d2fb71026b047deea306162522 Mon Sep 17 00:00:00 2001 From: Karol Zlot <11590960+karolzlot@users.noreply.github.com> Date: Sun, 12 Jun 2022 10:44:24 +0200 Subject: [PATCH 4/4] return None --- docxtpl/template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docxtpl/template.py b/docxtpl/template.py index a5ff6fa..f2d39ba 100644 --- a/docxtpl/template.py +++ b/docxtpl/template.py @@ -712,7 +712,7 @@ class DocxTemplate(object): return self.docx._part.relate_to(url, REL_TYPE.HYPERLINK, is_external=True) - def save(self, filename: Union[IO[bytes], str, PathLike[str]], *args, **kwargs): + def save(self, filename: Union[IO[bytes], str, PathLike[str]], *args, **kwargs) -> None: # case where save() is called without doing rendering # ( user wants only to replace image/embedded/zipname ) if not self.is_saved and not self.is_rendered: