#!/usr/bin/env python3
-import constants
+import logging
import os
+from uuid import uuid4
+
+import kiosk_constants
+
+
+logger = logging.getLogger(__file__)
class file_writer:
"""Helper context to write a pages file."""
def __init__(self, filename: str, *, transformations=[]):
- self.full_filename = os.path.join(constants.pages_dir, filename)
+ temp = "temp-" + str(uuid4())
+ self.temp_filename = os.path.join(kiosk_constants.pages_dir, temp)
+ self.full_filename = os.path.join(kiosk_constants.pages_dir, filename)
self.xforms = [file_writer.remove_tricky_unicode]
self.xforms.extend(transformations)
self.f = None
+ logger.info(f"Writing {self.temp_filename}...")
@staticmethod
def remove_tricky_unicode(x: str) -> str:
try:
- x = x.decode("utf-8")
x = x.replace("\u2018", "'").replace("\u2019", "'")
x = x.replace("\u201c", '"').replace("\u201d", '"')
x = x.replace("\u2e3a", "-").replace("\u2014", "-")
self.f.write(data.encode("utf-8"))
def __enter__(self):
- self.f = open(self.full_filename, "wb")
+ self.f = open(self.temp_filename, "wb")
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
self.close()
+ logger.info(f"Copying {self.temp_filename} to {self.full_filename}")
+ cmd = f'/bin/mv -f {self.temp_filename} "{self.full_filename}"'
+ os.system(cmd)
+ print(cmd)
def done(self):
self.close()
# Test
# def toupper(x):
# return x.upper()
-#
+
# with file_writer("test", transformations=[toupper]) as fw:
-# fw.write(u"Another test!!")
+# fw.write(u"Another test!!")