10 logger = logging.getLogger(__name__)
14 """Helper context to write a pages file."""
16 def __init__(self, filename: str, *, transformations=[]):
17 temp = "temp-" + str(uuid4())
18 self.temp_filename = os.path.join(kiosk_constants.pages_dir, temp)
19 self.full_filename = os.path.join(kiosk_constants.pages_dir, filename)
20 self.xforms = [file_writer.remove_tricky_unicode]
21 self.xforms.extend(transformations)
23 logger.info(f"Writing {self.temp_filename}...")
26 def remove_tricky_unicode(x: str) -> str:
28 x = x.replace("\u2018", "'").replace("\u2019", "'")
29 x = x.replace("\u201c", '"').replace("\u201d", '"')
30 x = x.replace("\u2e3a", "-").replace("\u2014", "-")
35 def write(self, data):
36 for xform in self.xforms:
38 self.f.write(data.encode("utf-8"))
41 self.f = open(self.temp_filename, "wb")
44 def __exit__(self, exc_type, exc_value, exc_traceback):
46 logger.info(f"Copying {self.temp_filename} to {self.full_filename}")
47 cmd = f'/bin/mv -f {self.temp_filename} "{self.full_filename}"'
62 # with file_writer("test", transformations=[toupper]) as fw:
63 # fw.write(u"Another test!!")