MoinLight

Annotated moinformat/input/directory.py

176:47af441b48bf
2018-11-26 Paul Boddie Support linking to stylesheets based on the collection of available files.
paul@104 1
#!/usr/bin/env python
paul@104 2
paul@104 3
"""
paul@104 4
Directory input context.
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@104 22
from moinformat.input.common import Input
paul@104 23
from moinformat.utils.directory import Directory
paul@132 24
from os.path import sep
paul@104 25
paul@137 26
class DirectoryInput(Input):
paul@104 27
paul@104 28
    "A directory output context."
paul@104 29
paul@104 30
    name = "directory"
paul@104 31
paul@165 32
    def __init__(self, metadata):
paul@104 33
paul@165 34
        "Initialise the context with the given 'metadata'."
paul@104 35
paul@165 36
        if not metadata.has_key("input_filename"):
paul@165 37
            raise ValueError, metadata
paul@104 38
paul@165 39
        Input.__init__(self, metadata)
paul@165 40
        self.dir = Directory(metadata.get("input_filename"))
paul@104 41
paul@132 42
        # Support an encoding of the level separator for the filesystem.
paul@132 43
        # Where it is the same as the directory separator, documents are stored
paul@132 44
        # using nested directories, not as a flat list.
paul@132 45
paul@165 46
        self.level_sep = metadata.get("input_separator", sep)
paul@132 47
paul@132 48
    def all(self):
paul@132 49
paul@132 50
        "Return all pages in the context."
paul@132 51
paul@154 52
        # Ignore dotfiles.
paul@154 53
paul@154 54
        return map(self.to_pagename, self.dir.select_files("[!.]*"))
paul@132 55
paul@132 56
    # Page characteristics.
paul@132 57
paul@132 58
    def subpage_filenames(self, pagename):
paul@132 59
paul@132 60
        "Return the subpage filenames of 'pagename'."
paul@132 61
paul@132 62
        pattern = self.to_filename("%s/*" % pagename)
paul@137 63
        return self.dir.select_files(pattern)
paul@132 64
paul@132 65
    def subpages(self, pagename):
paul@132 66
paul@132 67
        "Return the subpages of 'pagename'."
paul@132 68
paul@132 69
        return map(self.to_pagename, self.subpage_filenames(pagename))
paul@132 70
paul@132 71
    # Page access methods.
paul@132 72
paul@104 73
    def readfile(self, filename, encoding=None):
paul@104 74
paul@104 75
        """
paul@104 76
        Return the contents of the file having the given 'filename' and optional
paul@104 77
        'encoding'.
paul@104 78
        """
paul@104 79
paul@137 80
        return self.readpath(self.dir.get_filename(filename), encoding)
paul@132 81
paul@132 82
    # NOTE: Translation methods should encode filenames appropriately.
paul@132 83
paul@132 84
    def to_filename(self, pagename):
paul@132 85
paul@132 86
        "Return the filename corresponding to 'pagename'."
paul@132 87
paul@132 88
        if sep == self.level_sep:
paul@132 89
            return pagename
paul@132 90
        else:
paul@132 91
            return self.level_sep.join(pagename.split("/"))
paul@132 92
paul@132 93
    def to_pagename(self, filename):
paul@132 94
paul@132 95
        "Return the pagename corresponding to 'filename'."
paul@132 96
paul@132 97
        if sep == self.level_sep:
paul@132 98
            return filename
paul@132 99
        else:
paul@132 100
            return "/".join(filename.split(self.level_sep))
paul@104 101
paul@104 102
input = DirectoryInput
paul@104 103
paul@104 104
# vim: tabstop=4 expandtab shiftwidth=4