Format codebase w/ black.
[kiosk.git] / file_writer.py
1 import constants
2 import os
3
4
5 def remove_tricky_unicode(x):
6     try:
7         x = x.decode("utf-8")
8         x = x.replace("\u2018", "'").replace("\u2019", "'")
9         x = x.replace("\u201c", '"').replace("\u201d", '"')
10         x = x.replace("\u2e3a", "-").replace("\u2014", "-")
11     except:
12         pass
13     return x
14
15
16 class file_writer:
17     def __init__(self, filename):
18         self.full_filename = os.path.join(constants.pages_dir, filename)
19         self.f = open(self.full_filename, "wb")
20         self.xforms = [remove_tricky_unicode]
21
22     def add_xform(self, xform):
23         self.xforms.append(xform)
24
25     def write(self, data):
26         for xform in self.xforms:
27             data = xform(data)
28         self.f.write(data.encode("utf-8"))
29
30     def done(self):
31         self.f.close()
32
33     def close(self):
34         self.done()
35
36
37 # Test
38 # def toupper(x):
39 #    return x.upper()
40 #
41 # fw = file_writer("test")
42 # fw.add_xform(toupper)
43 # fw.write(u"This is a \u201ctest\u201d. \n")
44 # fw.done()