inline_email_css: Move constants to top, remove chdir.

This commit is contained in:
wowol
2020-04-09 12:22:23 +02:00
committed by Tim Abbott
parent 0b5a87f98f
commit b3cc93f961

View File

@@ -1,12 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import errno
from premailer import Premailer from premailer import Premailer
from cssutils import profile from cssutils import profile
from cssutils.profiles import Profiles, properties, macros from cssutils.profiles import Profiles, properties, macros
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../') ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../')
EMAIL_TEMPLATES_PATH = os.path.join(ZULIP_PATH, 'templates', 'zerver', 'emails')
COMPILED_EMAIL_TEMPLATES_PATH = os.path.join(EMAIL_TEMPLATES_PATH, 'compiled')
CSS_SOURCE_PATH = os.path.join(EMAIL_TEMPLATES_PATH, "email.css")
def configure_cssutils() -> None: def configure_cssutils() -> None:
# These properties are not supported by cssutils by default and will # These properties are not supported by cssutils by default and will
@@ -43,28 +45,24 @@ def strip_unnecesary_tags(text: str) -> str:
if __name__ == "__main__": if __name__ == "__main__":
templates_to_inline = set() templates_to_inline = set()
for f in os.listdir(os.path.join(ZULIP_PATH, 'templates', 'zerver', 'emails')): for f in os.listdir(EMAIL_TEMPLATES_PATH):
if f.endswith('.source.html'): if f.endswith('.source.html'):
templates_to_inline.add(f.split('.source.html')[0]) templates_to_inline.add(f.split('.source.html')[0])
configure_cssutils() configure_cssutils()
os.chdir(os.path.join(ZULIP_PATH, 'templates', 'zerver', 'emails')) os.makedirs(COMPILED_EMAIL_TEMPLATES_PATH, exist_ok=True)
try: for template_name in templates_to_inline:
os.makedirs("compiled") template_html_source = template_name + ".source.html"
except OSError as e: compiled_template_path = os.path.join(COMPILED_EMAIL_TEMPLATES_PATH,
if e.errno != errno.EEXIST: template_name + ".html")
raise template_path = os.path.join(EMAIL_TEMPLATES_PATH, template_html_source)
for template in templates_to_inline: with open(template_path) as template_source_file:
template_html_source = template + ".source.html"
compiled_template_path = os.path.join(os.getcwd(), "compiled", template + ".html")
with open(template_html_source) as template_source_file:
template_str = template_source_file.read() template_str = template_source_file.read()
output = Premailer(template_str, output = Premailer(template_str,
external_styles=["email.css"]).transform() external_styles=[CSS_SOURCE_PATH]).transform()
output = escape_jinja2_characters(output) output = escape_jinja2_characters(output)
@@ -75,7 +73,7 @@ if __name__ == "__main__":
# template, since we'll end up with 2 copipes of those tags. # template, since we'll end up with 2 copipes of those tags.
# Thus, we strip this stuff out if the template extends # Thus, we strip this stuff out if the template extends
# another template. # another template.
if template != 'email_base_default': if template_name != 'email_base_default':
output = strip_unnecesary_tags(output) output = strip_unnecesary_tags(output)
if ('zerver/emails/compiled/email_base_default.html' in output or if ('zerver/emails/compiled/email_base_default.html' in output or