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