summary refs log tree commit diff
path: root/synapse/config/_base.py
diff options
context:
space:
mode:
authorAndrew Morgan <andrew@amorgan.xyz>2021-04-22 18:31:10 +0100
committerAndrew Morgan <andrew@amorgan.xyz>2021-04-22 18:31:10 +0100
commit7bf3a6ee1835ea6b727f6f82bb5e743cab250dbb (patch)
tree5743ec7023786a5f0f4359e20be963c40ee0ff33 /synapse/config/_base.py
parentMerge commit '73ed289bd' into anoa/dinsic_release_1_31_0 (diff)
parentCheck if a user is in the room before sending a PowerLevel event on their beh... (diff)
downloadsynapse-7bf3a6ee1835ea6b727f6f82bb5e743cab250dbb.tar.xz
Merge commit '2e537a028' into anoa/dinsic_release_1_31_0
Diffstat (limited to 'synapse/config/_base.py')
-rw-r--r--synapse/config/_base.py42
1 files changed, 26 insertions, 16 deletions
diff --git a/synapse/config/_base.py b/synapse/config/_base.py

index 93344583e7..2ae1fc15cb 100644 --- a/synapse/config/_base.py +++ b/synapse/config/_base.py
@@ -204,11 +204,28 @@ class Config: with io_open(file_path, encoding="utf-8") as file_stream: return file_stream.read() + def read_template(self, filename: str) -> jinja2.Template: + """Load a template file from disk. + + This function will attempt to load the given template from the default Synapse + template directory. + + Files read are treated as Jinja templates. The templates is not rendered yet + and has autoescape enabled. + + Args: + filename: A template filename to read. + + Raises: + ConfigError: if the file's path is incorrect or otherwise cannot be read. + + Returns: + A jinja2 template. + """ + return self.read_templates([filename])[0] + def read_templates( - self, - filenames: List[str], - custom_template_directory: Optional[str] = None, - autoescape: bool = False, + self, filenames: List[str], custom_template_directory: Optional[str] = None, ) -> List[jinja2.Template]: """Load a list of template files from disk using the given variables. @@ -216,7 +233,8 @@ class Config: template directory. If `custom_template_directory` is supplied, that directory is tried first. - Files read are treated as Jinja templates. These templates are not rendered yet. + Files read are treated as Jinja templates. The templates are not rendered yet + and have autoescape enabled. Args: filenames: A list of template filenames to read. @@ -224,16 +242,12 @@ class Config: custom_template_directory: A directory to try to look for the templates before using the default Synapse template directory instead. - autoescape: Whether to autoescape variables before inserting them into the - template. - Raises: ConfigError: if the file's path is incorrect or otherwise cannot be read. Returns: A list of jinja2 templates. """ - templates = [] search_directories = [self.default_template_dir] # The loader will first look in the custom template directory (if specified) for the @@ -250,7 +264,7 @@ class Config: search_directories.insert(0, custom_template_directory) loader = jinja2.FileSystemLoader(search_directories) - env = jinja2.Environment(loader=loader, autoescape=autoescape) + env = jinja2.Environment(loader=loader, autoescape=jinja2.select_autoescape(),) # Update the environment with our custom filters env.filters.update( @@ -260,12 +274,8 @@ class Config: } ) - for filename in filenames: - # Load the template - template = env.get_template(filename) - templates.append(template) - - return templates + # Load the templates + return [env.get_template(filename) for filename in filenames] def _format_ts_filter(value: int, format: str):