1 #!/usr/bin/env python 2 3 """ 4 Output context common functionality. 5 6 Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 import codecs 23 24 class Output: 25 26 "A common output context abstraction." 27 28 default_encoding = "utf-8" 29 30 def __init__(self, metadata): 31 32 "Initialise the output context with the 'metadata'." 33 34 self.metadata = metadata 35 36 # Obtain essential metadata. 37 38 self.encoding = metadata.get("output_encoding", self.default_encoding) 39 self.reset() 40 41 def reset(self): 42 43 "Set up an output collector." 44 45 self.output = [] 46 47 def encode(self, text): 48 49 "Encode 'text' using the configured encoding." 50 51 return encode(text, self.encoding) 52 53 def out(self, text): 54 55 "Add 'text' to the output collector." 56 57 self.output.append(self.encode(text)) 58 59 # Page characteristics. 60 61 def parent(self, pagename): 62 63 "Return the parent of 'pagename'." 64 65 return "/" in pagename and pagename.rsplit("/", 1)[0] or None 66 67 # Serialisation methods. 68 69 def to_string(self): 70 71 "Return the output as a plain string." 72 73 s = "".join(self.output) 74 self.reset() 75 return s 76 77 # Serialisation methods. 78 79 def can_write(self): 80 81 "Return whether this context supports page writing." 82 83 return False 84 85 def writefile(self, text, filename, encoding=None): 86 87 """ 88 Write 'text' to the file having the given 'filename'. If the 89 optional 'encoding' is specified, override the general encoding. 90 91 Subclasses need to override this method for it to have an effect. 92 """ 93 94 pass 95 96 def writepage(self, text, pagename, encoding=None): 97 98 """ 99 Write 'text' to the file having the given 'pagename' and optional 100 'encoding'. 101 """ 102 103 return self.writefile(text, self.to_filename(pagename), encoding) 104 105 # Output methods. 106 107 def writepath(self, text, filename, encoding=None): 108 109 """ 110 Write 'text' to the file having the given 'filename'. If the 111 optional 'encoding' is specified, override the general encoding. 112 """ 113 114 f = codecs.open(filename, "w", encoding=encoding or self.encoding) 115 try: 116 f.write(text) 117 finally: 118 f.close() 119 120 def encode(s, encoding): 121 122 "Encode 's' using 'encoding' if Unicode." 123 124 if isinstance(s, unicode): 125 return s.encode(encoding) 126 else: 127 return s 128 129 # vim: tabstop=4 expandtab shiftwidth=4