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 def can_write(self): 78 79 "Return whether this context supports page writing." 80 81 return False 82 83 def writefile(self, text, filename, encoding=None): 84 85 """ 86 Write 'text' to the file having the given 'filename'. If the 87 optional 'encoding' is specified, override the general encoding. 88 89 Subclasses need to override this method for it to have an effect. 90 """ 91 92 pass 93 94 def writepage(self, text, pagename, encoding=None): 95 96 """ 97 Write 'text' to the file having the given 'pagename' and optional 98 'encoding'. 99 """ 100 101 return self.writefile(text, self.to_filename(pagename), encoding) 102 103 # Output methods. 104 105 def writepath(self, text, filename, encoding=None): 106 107 """ 108 Write 'text' to the file having the given 'filename'. If the 109 optional 'encoding' is specified, override the general encoding. 110 """ 111 112 f = codecs.open(filename, "w", encoding=encoding or self.encoding) 113 try: 114 f.write(text) 115 finally: 116 f.close() 117 118 def encode(s, encoding): 119 120 "Encode 's' using 'encoding' if Unicode." 121 122 if isinstance(s, unicode): 123 return s.encode(encoding) 124 else: 125 return s 126 127 # vim: tabstop=4 expandtab shiftwidth=4