MoinLight

Annotated moinformat/themes/common.py

176:47af441b48bf
2018-11-26 Paul Boddie Support linking to stylesheets based on the collection of available files.
paul@161 1
#!/usr/bin/env python
paul@161 2
paul@161 3
"""
paul@161 4
Theming common functionality.
paul@161 5
paul@161 6
Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk>
paul@161 7
paul@161 8
This program is free software; you can redistribute it and/or modify it under
paul@161 9
the terms of the GNU General Public License as published by the Free Software
paul@161 10
Foundation; either version 3 of the License, or (at your option) any later
paul@161 11
version.
paul@161 12
paul@161 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@161 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@161 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@161 16
details.
paul@161 17
paul@161 18
You should have received a copy of the GNU General Public License along with
paul@161 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@161 20
"""
paul@161 21
paul@161 22
from os import listdir, makedirs
paul@161 23
from os.path import exists, isfile, join, split
paul@161 24
from shutil import copy
paul@161 25
paul@161 26
class Theme:
paul@161 27
paul@161 28
    "A common theme abstraction."
paul@161 29
paul@165 30
    def __init__(self, metadata):
paul@165 31
paul@165 32
        "Initialise the theme with the given 'metadata'."
paul@161 33
paul@165 34
        self.metadata = metadata
paul@161 35
paul@165 36
        # Obtain essential metadata.
paul@165 37
paul@165 38
        self.linker = metadata.get_linker()
paul@165 39
        self.output = metadata.get_output()
paul@161 40
paul@161 41
    def apply(self, text):
paul@161 42
paul@161 43
        "Apply this theme to the given 'text', returning a themed version."
paul@161 44
paul@161 45
        return text
paul@161 46
paul@161 47
    def get_resource_base(self):
paul@161 48
paul@161 49
        "Return the filesystem base of resources for instances of this class."
paul@161 50
paul@161 51
        return split(self.__class__.origin)[0]
paul@161 52
paul@161 53
    def get_resource(self, filename):
paul@161 54
paul@161 55
        "Return the complete path for the resource with the given 'filename'."
paul@161 56
paul@161 57
        base = self.get_resource_base()
paul@161 58
        return join(base, filename)
paul@161 59
paul@161 60
    def install_resource(self, filename, target=None):
paul@161 61
paul@161 62
        """
paul@161 63
        Install the resource with the given 'filename' into a location having
paul@161 64
        the given 'target' name (or 'filename' if 'target' is omitted).
paul@161 65
        """
paul@161 66
paul@165 67
        if not self.output.can_write():
paul@165 68
            return
paul@165 69
paul@161 70
        pathname = self.get_resource(filename)
paul@161 71
        outpath = self.output.get_filename(target or filename)
paul@161 72
paul@161 73
        self.copy(pathname, outpath)
paul@161 74
paul@161 75
    def copy(self, pathname, outpath):
paul@161 76
paul@161 77
        "Copy 'pathname' to 'outpath'."
paul@161 78
paul@161 79
        if isfile(pathname):
paul@161 80
            outdir = split(outpath)[0]
paul@161 81
            if outdir and not exists(outdir):
paul@161 82
                makedirs(outdir)
paul@161 83
            copy(pathname, outpath)
paul@161 84
        else:
paul@161 85
            if not exists(outpath):
paul@161 86
                makedirs(outpath)
paul@161 87
            for filename in listdir(pathname):
paul@161 88
                self.copy(join(pathname, filename), join(outpath, filename))
paul@161 89
paul@161 90
    def load_resource(self, filename):
paul@161 91
paul@161 92
        "Return the textual content of the resource with the given 'filename'."
paul@161 93
paul@161 94
        f = open(self.get_resource(filename))
paul@161 95
        try:
paul@161 96
            return f.read()
paul@161 97
        finally:
paul@161 98
            f.close()
paul@161 99
paul@161 100
# vim: tabstop=4 expandtab shiftwidth=4