From 0390c740618c02b92532b876497c6b3c37887922 Mon Sep 17 00:00:00 2001 From: schmelczerandras Date: Thu, 16 Jul 2020 16:35:41 +0200 Subject: [PATCH] Add error page generator --- frontend/error-pages/build.py | 43 ++++++++++++++++++++++++++++++ frontend/error-pages/template.html | 36 +++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 frontend/error-pages/build.py create mode 100644 frontend/error-pages/template.html diff --git a/frontend/error-pages/build.py b/frontend/error-pages/build.py new file mode 100644 index 0000000..f9900c9 --- /dev/null +++ b/frontend/error-pages/build.py @@ -0,0 +1,43 @@ +from typing import NamedTuple, Dict, List, Type +from os import makedirs, path + + +template_path = 'template.html' +result_path = 'built' + +class Substitutions(NamedTuple): + title: str + description: str + +error_messages: Dict[str, Substitutions] = { + '403': Substitutions( + title='403 - Forbidden', + description='You are not allowed to view this resource.' + ), + '404': Substitutions( + title='404 - Not found', + description='The requested resource cannot be found.' + ), + '50x': Substitutions( + title='50x - Server error', + description='It\'s my fault.' + ), +} + + +def substitute(source: str, substitutions: Substitutions) -> str: + for i, property_name in enumerate(Substitutions._fields): + source = source.replace(f'{{{property_name}}}', str(substitutions[i])) + return source + + +if __name__ == '__main__': + with open(template_path) as f: + template = f.read() + + makedirs(result_path, mode=0o440, exist_ok=True) + + for name, substitutions in error_messages.items(): + with open(path.join(result_path, f'{name}.html'), 'w') as f: + html = substitute(template, substitutions) + f.write(html) diff --git a/frontend/error-pages/template.html b/frontend/error-pages/template.html new file mode 100644 index 0000000..82a55c3 --- /dev/null +++ b/frontend/error-pages/template.html @@ -0,0 +1,36 @@ + + + + + {title} + + + + + + +

{description}

+ +