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