1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/moinformat/themes/common.py Mon Aug 13 17:55:16 2018 +0200
1.3 @@ -0,0 +1,97 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Theming common functionality.
1.8 +
1.9 +Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from os import listdir, makedirs
1.26 +from os.path import exists, isfile, join, split
1.27 +from shutil import copy
1.28 +
1.29 +class Theme:
1.30 +
1.31 + "A common theme abstraction."
1.32 +
1.33 + def __init__(self, output, linker, pagename):
1.34 +
1.35 + """
1.36 + Initialise the theme with the given 'output' context, 'linker' and
1.37 + 'pagename'.
1.38 + """
1.39 +
1.40 + self.output = output
1.41 + self.linker = linker
1.42 + self.pagename = pagename
1.43 +
1.44 + def apply(self, text):
1.45 +
1.46 + "Apply this theme to the given 'text', returning a themed version."
1.47 +
1.48 + return text
1.49 +
1.50 + def get_resource_base(self):
1.51 +
1.52 + "Return the filesystem base of resources for instances of this class."
1.53 +
1.54 + return split(self.__class__.origin)[0]
1.55 +
1.56 + def get_resource(self, filename):
1.57 +
1.58 + "Return the complete path for the resource with the given 'filename'."
1.59 +
1.60 + base = self.get_resource_base()
1.61 + return join(base, filename)
1.62 +
1.63 + def install_resource(self, filename, target=None):
1.64 +
1.65 + """
1.66 + Install the resource with the given 'filename' into a location having
1.67 + the given 'target' name (or 'filename' if 'target' is omitted).
1.68 + """
1.69 +
1.70 + pathname = self.get_resource(filename)
1.71 + outpath = self.output.get_filename(target or filename)
1.72 +
1.73 + self.copy(pathname, outpath)
1.74 +
1.75 + def copy(self, pathname, outpath):
1.76 +
1.77 + "Copy 'pathname' to 'outpath'."
1.78 +
1.79 + if isfile(pathname):
1.80 + outdir = split(outpath)[0]
1.81 + if outdir and not exists(outdir):
1.82 + makedirs(outdir)
1.83 + copy(pathname, outpath)
1.84 + else:
1.85 + if not exists(outpath):
1.86 + makedirs(outpath)
1.87 + for filename in listdir(pathname):
1.88 + self.copy(join(pathname, filename), join(outpath, filename))
1.89 +
1.90 + def load_resource(self, filename):
1.91 +
1.92 + "Return the textual content of the resource with the given 'filename'."
1.93 +
1.94 + f = open(self.get_resource(filename))
1.95 + try:
1.96 + return f.read()
1.97 + finally:
1.98 + f.close()
1.99 +
1.100 +# vim: tabstop=4 expandtab shiftwidth=4