From 7bd6e9504d93b0885081ce5616e3711e74375f04 Mon Sep 17 00:00:00 2001 From: Taka Date: Fri, 4 Jan 2019 08:11:07 +0000 Subject: [PATCH] Specify the encoding when generate snippet images. --- vasl_templates/webapp/utils.py | 14 ++++++++++++-- vasl_templates/webapp/webdriver.py | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/vasl_templates/webapp/utils.py b/vasl_templates/webapp/utils.py index b2db34e..41f8637 100644 --- a/vasl_templates/webapp/utils.py +++ b/vasl_templates/webapp/utils.py @@ -52,15 +52,25 @@ class MsgStore: class TempFile: """Manage a temp file that can be closed while it's still being used.""" - def __init__( self, mode="wb", extn=None ): + def __init__( self, mode="wb", extn=None, encoding=None ): self.mode = mode self.extn = extn + self.encoding = encoding self.temp_file = None self.name = None def __enter__( self ): """Allocate a temp file.""" - self.temp_file = tempfile.NamedTemporaryFile( mode=self.mode, suffix=self.extn, delete=False ) + if self.encoding: + encoding = self.encoding + else: + encoding = "utf-8" if "b" not in self.mode else None + self.temp_file = tempfile.NamedTemporaryFile( + mode = self.mode, + encoding = encoding, + suffix = self.extn, + delete = False + ) self.name = self.temp_file.name return self diff --git a/vasl_templates/webapp/webdriver.py b/vasl_templates/webapp/webdriver.py index 01ba455..6f4ea04 100644 --- a/vasl_templates/webapp/webdriver.py +++ b/vasl_templates/webapp/webdriver.py @@ -117,7 +117,8 @@ class WebDriver: bbox = diff.getbbox() return img.crop( bbox ) - with TempFile(extn=".html",mode="w") as html_tempfile, TempFile(extn=".png") as screenshot_tempfile: + with TempFile( extn=".html", mode="w", encoding="utf-8" ) as html_tempfile, \ + TempFile( extn=".png" ) as screenshot_tempfile: # NOTE: We could do some funky Javascript stuff to load the browser directly from the string, # but using a temp file is straight-forward and pretty much guaranteed to work :-/