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