From 1dc0670c01211a1d1e8b99facea1710949ee506d Mon Sep 17 00:00:00 2001 From: Christian Franke Date: Sat, 8 Oct 2011 16:21:04 +0200 Subject: Add plugin api to template.py, add news plugin --- scripts/news.py | 25 ++++++++ scripts/plugin.py | 24 ++++++++ scripts/template.conf | 3 + scripts/template.py | 166 ++++++++++++++++++++++++-------------------------- 4 files changed, 133 insertions(+), 85 deletions(-) create mode 100644 scripts/news.py create mode 100644 scripts/plugin.py (limited to 'scripts') diff --git a/scripts/news.py b/scripts/news.py new file mode 100644 index 0000000..ca9dfb4 --- /dev/null +++ b/scripts/news.py @@ -0,0 +1,25 @@ +import template +import plugin +import string +import os + +class NewsPlugin: + def __init__(self): + newspath = os.path.join(template.path, 'news') + newsfiles = [ os.path.join(newspath, entry) for entry in sorted(os.listdir(newspath)) ] + + self._news = [] + for newsfile in reversed(newsfiles): + self._news.append(open(newsfile, 'r').read()) + + def process_content(self, page_content): + return_value = {} + for filename, content in page_content.iteritems(): + template = string.Template(content) + return_value[filename] = template.safe_substitute( + news = ''.join(self._news), + latest_news = ''.join(self._news[:3]), + ) + return return_value + +plugin.plugin_manager.register(NewsPlugin()) diff --git a/scripts/plugin.py b/scripts/plugin.py new file mode 100644 index 0000000..785a219 --- /dev/null +++ b/scripts/plugin.py @@ -0,0 +1,24 @@ +class PluginManager(object): + def __init__(self): + self.plugins = [] + def register(self, plugin): + self.plugins.append(plugin) + def __getattr__(self, name): + def caller(data, *args, **kwargs): + return_value = data + processed = False + for plugin in self.plugins: + try: + method = getattr(plugin, name) + return_value = method(data, *args, **kwargs) + processed = True + except KeyError: + pass + except Exception: + sys.excepthook(*sys.exc_info()) + if not processed: + print "Warning: no plugin implemented '%s'" % name + return return_value + return caller + +plugin_manager = PluginManager() diff --git a/scripts/template.conf b/scripts/template.conf index 339871a..2f9ee42 100644 --- a/scripts/template.conf +++ b/scripts/template.conf @@ -5,3 +5,6 @@ [Default] path = ../template target = ../public/ + +[Plugins] +news=load diff --git a/scripts/template.py b/scripts/template.py index a4d4e9e..c5d348a 100755 --- a/scripts/template.py +++ b/scripts/template.py @@ -1,8 +1,15 @@ #!/usr/bin/env python #Filename: newtemp.py #Author: stayawake@lavabit.com - - +# --- +# Copyright (C) 2011 Christian Franke +# +# Permission is hereby granted to use and distribute this +# file for any purpose, provided the above copyright notice +# is kept in place. +# + +import imp import os import os.path import sys @@ -12,104 +19,93 @@ import datetime from string import Template import ConfigParser -def readdir(directory): - return_value = {"content.html":"", "keywords":"", "desc_de":"", "desc_en":""} - - for c in return_value: - f = open(os.path.join(directory, c), "r") - return_value[c] = f.read() - f.close - return return_value - -def substitute(target, prefix, htmltemplate, datenow, topic, content): - - #create template-string - htmltext = Template(htmltemplate) - htmltext = htmltext.substitute( - template_date = datenow, - template_chattopic = topic, - template_keywords = content["keywords"], - template_content = content["content.html"], - template_desc_de = content["desc_de"], - template_desc_en = content["desc_en"] - ) - - #write it to the outputfile - outputfile = open(os.path.join(target, page + ".html"), "w") - outputfile.write(htmltext) - outputfile.close() - - -def readarguments(argv): - - global configfile - global verbose - - try: - opts, args = getopt.getopt(argv, "c:vh", ["config=", "verbose", "help"]) - except getopt.GetoptError: - print "There is a problem with your argument(s)" - sys.exit(2) - for opt, arg in opts: - if opt in ("-c", "--config"): - configfile = arg - elif opt in ("-v", "--verbose"): - verbose = True - elif opt in ("-h", "--help"): - print "Please read the non-existing man page for further assistance" - sys.exit(0) - +import plugin + +class Page: + def __init__(self, name): + self.name = name + data = {"content.html":"", "keywords":"", "desc_de":"", "desc_en":""} + + for c in data: + f = open(os.path.join(pagepath, name, c), "r") + data[c] = f.read() + f.close + + self._data = plugin.plugin_manager.process_content(data) + + @property + def html(self): + #create template-string + htmltext = Template(htmltemplate) + htmltext = htmltext.substitute( + template_date = datenow, + template_chattopic = topic, + template_keywords = self._data["keywords"], + template_content = self._data["content.html"], + template_desc_de = self._data["desc_de"], + template_desc_en = self._data["desc_en"] + ) + return htmltext + + def render_and_save(self): + #write it to the outputfile + outputfile = open(os.path.join(targetdir, self.name + '.html'), "w") + outputfile.write(self.html) + outputfile.close() configfile = "template.conf" verbose = False +opts, args = getopt.getopt(sys.argv[1:], "c:vh", ["config=", "verbose", "help"]) +for opt, arg in opts: + if opt in ("-c", "--config"): + configfile = arg + elif opt in ("-v", "--verbose"): + verbose = True + elif opt in ("-h", "--help"): + print "Please read the non-existing man page for further assistance" + sys.exit(0) + basedir = os.path.dirname(os.path.abspath(__file__)) -readarguments(sys.argv[1:]) +def makeabs(path): + if not os.path.isabs(path): + return os.path.join(basedir, path) + return path +configfile = makeabs(configfile) config = ConfigParser.ConfigParser() -if not os.path.isabs(configfile): - configfile = os.path.join(basedir, configfile) config.read(configfile) -path = config.get("Default", "path") -if not os.path.isabs(path): - path = os.path.join(basedir, path) - -target = config.get("Default", "target") -if not os.path.isabs(target): - target = os.path.join(basedir, target) +path = makeabs(config.get("Default", "path")) +targetdir = makeabs(config.get("Default", "target")) pagepath = os.path.join(path, "pages") templatepath = os.path.join(path, "template") topicpath = os.path.join(path, "topic") -if verbose: - print "Pagepath: %s" % pagepath - print "Templatepath: %s" % templatepath - print "topicpath: %s" % topicpath - #set template -f = open(os.path.join(templatepath, "template.html"), "r") -htmltemplate = f.read() -f.close -if verbose: - print "template read" - -#set topic -f = open(os.path.join(topicpath, "topic"), "r") -topic = f.read() -f.close -if verbose: - print "topic read" - -#set date +htmltemplate = open(os.path.join(templatepath, "template.html"), "r").read() +topic = open(os.path.join(topicpath, "topic"), "r").read() datenow = time.strftime("%Y-%m-%dT%H:%M:%S +0100") -if verbose: - print "Set date: " + datenow -for page in os.listdir(pagepath): - prefix = os.path.join(pagepath, page) - page_data = readdir(prefix) - substitute(target, prefix, htmltemplate, datenow, topic, page_data) +if __name__ == '__main__': if verbose: - print "%s.html written" % prefix + print "Pagepath: %s" % pagepath + print "Templatepath: %s" % templatepath + print "topicpath: %s" % topicpath + print "datenow: %s" % datenow + + pluginlist = config.items('Plugins') + for pluginname, pluginarg in pluginlist: + modinfo = imp.find_module(pluginname) + try: + imp.load_module(pluginname, *modinfo) + finally: + modinfo[0].close() + +if __name__ == '__main__': + for page in os.listdir(pagepath): + Page(page).render_and_save() + if verbose: + print "%s.html written" % prefix + # vi: noexpandtab:tabstop=8:shiftwidth=8 -- cgit v1.2.1