1 #!/usr/bin/env python 2 3 """ 4 Input context common functionality. 5 6 Copyright (C) 2018, 2019 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 from os.path import split 23 import codecs 24 25 class Input: 26 27 "A common input context abstraction." 28 29 default_encoding = "utf-8" 30 31 def __init__(self, metadata): 32 33 "Initialise the input context with the given 'metadata'." 34 35 self.metadata = metadata 36 37 # Obtain essential metadata. 38 39 self.encoding = metadata.get("input_encoding", self.default_encoding) 40 41 def all(self): 42 43 "Return all pages in the context." 44 45 return [] 46 47 def all_attachments(self): 48 49 "Return all attachment filenames in the context." 50 51 return [] 52 53 def get_attachments(self, pagename): 54 55 "Return all attachment filenames for the given 'pagename'." 56 57 return [] 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 def subpages(self, pagename): 68 69 "Return the subpages of 'pagename'." 70 71 return [] 72 73 # Page access methods. 74 75 def readfile(self, filename, encoding=None): 76 77 """ 78 Return the contents of the file having the given 'filename' and optional 79 'encoding'. This implementation treats 'filename' as a path. 80 """ 81 82 return self.readpath(filename, encoding) 83 84 def readpage(self, pagename, encoding=None): 85 86 """ 87 Return the contents of the file having the given 'pagename' and optional 88 'encoding'. 89 """ 90 91 return self.readfile(self.to_filename(pagename), encoding) 92 93 # Input methods. 94 95 def readpath(self, filename, encoding=None): 96 97 """ 98 Return the contents of the file having the given 'filename'. If the 99 optional 'encoding' is specified, override the general encoding. 100 """ 101 102 f = codecs.open(filename, encoding=encoding or self.encoding) 103 try: 104 return f.read() 105 finally: 106 f.close() 107 108 # Name translation methods. 109 110 def to_filename(self, pagename): 111 112 "Return the filename corresponding to 'pagename'." 113 114 return pagename 115 116 def to_pagename(self, filename): 117 118 "Return the pagename corresponding to 'filename'." 119 120 # Take the leafname as the pagename from an arbitrary filename. 121 122 return split(filename)[-1] 123 124 # vim: tabstop=4 expandtab shiftwidth=4