paul@104 | 1 | #!/usr/bin/env python |
paul@104 | 2 | |
paul@104 | 3 | """ |
paul@104 | 4 | Input context common functionality. |
paul@104 | 5 | |
paul@230 | 6 | Copyright (C) 2018, 2019 Paul Boddie <paul@boddie.org.uk> |
paul@104 | 7 | |
paul@104 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@104 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@104 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@104 | 11 | version. |
paul@104 | 12 | |
paul@104 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@104 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@104 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@104 | 16 | details. |
paul@104 | 17 | |
paul@104 | 18 | You should have received a copy of the GNU General Public License along with |
paul@104 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@104 | 20 | """ |
paul@104 | 21 | |
paul@277 | 22 | from moinformat.utils.file import readfile |
paul@132 | 23 | from os.path import split |
paul@104 | 24 | import codecs |
paul@104 | 25 | |
paul@104 | 26 | class Input: |
paul@104 | 27 | |
paul@104 | 28 | "A common input context abstraction." |
paul@104 | 29 | |
paul@104 | 30 | default_encoding = "utf-8" |
paul@104 | 31 | |
paul@165 | 32 | def __init__(self, metadata): |
paul@165 | 33 | |
paul@165 | 34 | "Initialise the input context with the given 'metadata'." |
paul@104 | 35 | |
paul@165 | 36 | self.metadata = metadata |
paul@104 | 37 | |
paul@165 | 38 | # Obtain essential metadata. |
paul@165 | 39 | |
paul@165 | 40 | self.encoding = metadata.get("input_encoding", self.default_encoding) |
paul@104 | 41 | |
paul@132 | 42 | def all(self): |
paul@132 | 43 | |
paul@132 | 44 | "Return all pages in the context." |
paul@132 | 45 | |
paul@132 | 46 | return [] |
paul@132 | 47 | |
paul@230 | 48 | def all_attachments(self): |
paul@230 | 49 | |
paul@230 | 50 | "Return all attachment filenames in the context." |
paul@230 | 51 | |
paul@230 | 52 | return [] |
paul@230 | 53 | |
paul@230 | 54 | def get_attachments(self, pagename): |
paul@230 | 55 | |
paul@230 | 56 | "Return all attachment filenames for the given 'pagename'." |
paul@230 | 57 | |
paul@230 | 58 | return [] |
paul@230 | 59 | |
paul@132 | 60 | # Page characteristics. |
paul@132 | 61 | |
paul@132 | 62 | def parent(self, pagename): |
paul@132 | 63 | |
paul@132 | 64 | "Return the parent of 'pagename'." |
paul@132 | 65 | |
paul@139 | 66 | return "/" in pagename and pagename.rsplit("/", 1)[0] or None |
paul@132 | 67 | |
paul@132 | 68 | def subpages(self, pagename): |
paul@132 | 69 | |
paul@132 | 70 | "Return the subpages of 'pagename'." |
paul@132 | 71 | |
paul@132 | 72 | return [] |
paul@132 | 73 | |
paul@132 | 74 | # Page access methods. |
paul@132 | 75 | |
paul@104 | 76 | def readfile(self, filename, encoding=None): |
paul@104 | 77 | |
paul@104 | 78 | """ |
paul@132 | 79 | Return the contents of the file having the given 'filename' and optional |
paul@132 | 80 | 'encoding'. This implementation treats 'filename' as a path. |
paul@132 | 81 | """ |
paul@132 | 82 | |
paul@132 | 83 | return self.readpath(filename, encoding) |
paul@132 | 84 | |
paul@132 | 85 | def readpage(self, pagename, encoding=None): |
paul@132 | 86 | |
paul@132 | 87 | """ |
paul@132 | 88 | Return the contents of the file having the given 'pagename' and optional |
paul@132 | 89 | 'encoding'. |
paul@132 | 90 | """ |
paul@132 | 91 | |
paul@132 | 92 | return self.readfile(self.to_filename(pagename), encoding) |
paul@132 | 93 | |
paul@132 | 94 | # Input methods. |
paul@132 | 95 | |
paul@132 | 96 | def readpath(self, filename, encoding=None): |
paul@132 | 97 | |
paul@132 | 98 | """ |
paul@104 | 99 | Return the contents of the file having the given 'filename'. If the |
paul@104 | 100 | optional 'encoding' is specified, override the general encoding. |
paul@104 | 101 | """ |
paul@104 | 102 | |
paul@277 | 103 | return readfile(filename, encoding or self.encoding) |
paul@104 | 104 | |
paul@132 | 105 | # Name translation methods. |
paul@132 | 106 | |
paul@132 | 107 | def to_filename(self, pagename): |
paul@132 | 108 | |
paul@132 | 109 | "Return the filename corresponding to 'pagename'." |
paul@132 | 110 | |
paul@132 | 111 | return pagename |
paul@132 | 112 | |
paul@132 | 113 | def to_pagename(self, filename): |
paul@132 | 114 | |
paul@132 | 115 | "Return the pagename corresponding to 'filename'." |
paul@132 | 116 | |
paul@132 | 117 | # Take the leafname as the pagename from an arbitrary filename. |
paul@132 | 118 | |
paul@132 | 119 | return split(filename)[-1] |
paul@132 | 120 | |
paul@104 | 121 | # vim: tabstop=4 expandtab shiftwidth=4 |