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, parameters=None): 31 32 "Initialise the output context with the optional 'parameters'." 33 34 self.parameters = parameters or {} 35 self.encoding = self.parameters.get("encoding") or self.default_encoding 36 self.reset() 37 38 def reset(self): 39 40 "Set up an output collector." 41 42 self.output = [] 43 44 def encode(self, text): 45 46 "Encode 'text' using the configured encoding." 47 48 return encode(text, self.encoding) 49 50 def out(self, text): 51 52 "Add 'text' to the output collector." 53 54 self.output.append(self.encode(text)) 55 56 # Page characteristics. 57 58 def parent(self, pagename): 59 60 "Return the parent of 'pagename'." 61 62 return "/" in pagename and pagename.rsplit("/", 1)[0] or None 63 64 # Serialisation methods. 65 66 def to_string(self): 67 68 "Return the output as a plain string." 69 70 s = "".join(self.output) 71 self.reset() 72 return s 73 74 # Serialisation methods. 75 76 def can_write(self): 77 78 "Return whether this context supports page writing." 79 80 return False 81 82 def writefile(self, text, filename, encoding=None): 83 84 """ 85 Write 'text' to the file having the given 'filename'. If the 86 optional 'encoding' is specified, override the general encoding. 87 88 Subclasses need to override this method for it to have an effect. 89 """ 90 91 pass 92 93 def writepage(self, text, pagename, encoding=None): 94 95 """ 96 Write 'text' to the file having the given 'pagename' and optional 97 'encoding'. 98 """ 99 100 return self.writefile(text, self.to_filename(pagename), encoding) 101 102 # Output methods. 103 104 def writepath(self, text, filename, encoding=None): 105 106 """ 107 Write 'text' to the file having the given 'filename'. If the 108 optional 'encoding' is specified, override the general encoding. 109 """ 110 111 f = codecs.open(filename, "w", encoding=encoding or self.encoding) 112 try: 113 f.write(text) 114 finally: 115 f.close() 116 117 def encode(s, encoding): 118 119 "Encode 's' using 'encoding' if Unicode." 120 121 if isinstance(s, unicode): 122 return s.encode(encoding) 123 else: 124 return s 125 126 # vim: tabstop=4 expandtab shiftwidth=4