Exemplo n.º 1
0
 async def render_file(self, file_path: str) -> None:
     if not file_path.endswith('.j2'):
         return
     file_destination_path = file_path[:-3]
     data = {}
     if file_destination_path.startswith(
             self._configuration.www_directory_path):
         resource = '/'.join(
             Path(file_destination_path[len(self._configuration.
                                            www_directory_path):].strip(
                                                os.sep)).parts)
         if self._configuration.multilingual:
             resource_parts = resource.lstrip('/').split('/')
             if resource_parts[0] in map(
                     lambda x: x.alias,
                     self._configuration.locales.values()):
                 resource = '/'.join(resource_parts[1:])
         data['page_resource'] = resource
     root_path = rootname(file_path)
     template_name = '/'.join(Path(relpath(file_path, root_path)).parts)
     template = FileSystemLoader(root_path).load(self._environment,
                                                 template_name,
                                                 self._environment.globals)
     with open(file_destination_path, 'w') as f:
         f.write(template.render(data))
     os.remove(file_path)
Exemplo n.º 2
0
 async def render_file(self, file_path: PathLike) -> None:
     file_path_str = str(file_path)
     if not file_path_str.endswith('.j2'):
         return
     file_destination_path_str = file_path_str[:-3]
     data = {}
     if file_destination_path_str.startswith(
             str(self._configuration.www_directory_path)):
         resource = '/'.join(
             Path(file_destination_path_str[
                 len(str(self._configuration.www_directory_path)):].strip(
                     os.sep)).parts)
         if self._configuration.multilingual:
             resource_parts = resource.lstrip('/').split('/')
             if resource_parts[0] in map(lambda x: x.alias,
                                         self._configuration.locales):
                 resource = '/'.join(resource_parts[1:])
         data['page_resource'] = resource
     root_path = rootname(file_path)
     template_name = '/'.join(Path(file_path).relative_to(root_path).parts)
     template = FileSystemLoader(root_path).load(self._environment,
                                                 template_name,
                                                 self._environment.globals)
     async with aiofiles.open(file_destination_path_str,
                              'w',
                              encoding='utf-8') as f:
         await f.write(template.render(data))
     os.remove(file_path)
Exemplo n.º 3
0
async def generate_configuration_file(destination_file_path: str,
                                      jinja2_environment: Environment,
                                      **kwargs) -> None:
    root_path = rootname(__file__)
    configuration_file_template_name = '/'.join(
        Path(
            path.relpath(
                path.join(path.dirname(__file__), 'assets', 'nginx.conf.j2'),
                root_path)).parts)
    template = FileSystemLoader(root_path).load(
        jinja2_environment, configuration_file_template_name,
        jinja2_environment.globals)
    makedirs(path.dirname(destination_file_path))
    with open(destination_file_path, 'w') as f:
        f.write(template.render(kwargs))