constance

Scripts for generating (an earlier obsolete version of) my personal web site
git clone https://code.djc.id.au/git/constance/
commit c24979dbacf316beb48a99b7fffcc6344ad2098b
parent 0bff1b3be8b33bc7f19d0035050ea28b82bf5eba
Author: Sam Kingston <sam@sjkwi.com.au>
Date:   Sun, 10 Oct 2010 19:01:43 +1000

replaced command-line arguments with config file, defaulting to ~/.constance

Diffstat:
Mblog.py | 12++++++------
Mconstance.py | 63++++++++++++++++++++++++++++++++++++++++++---------------------
Mhomepage.py | 7++++---
Mreading.py | 6+++---
Mtags.py | 4++--
Mtemplates/blog/entry.atom | 6+++---
Mtemplates/blog/entry.html | 2+-
Mtemplates/blog/index.atom | 8++++----
Mtemplates/homepage/firehose.atom | 8++++----
Mtemplates/homepage/index.html | 6------
Mtemplates/reading/entry.atom | 2+-
Mtemplates/reading/reading.atom | 8++++----
12 files changed, 74 insertions(+), 58 deletions(-)
diff --git a/blog.py b/blog.py
@@ -49,9 +49,9 @@ class BlogEntry(object):
         self.guid = self.metadata['guid']
         self.language = self.metadata.get('language', None)
 
-    def generate_atom(self, website):
+    def generate_atom(self, template_config):
         return template_loader.load('entry.atom').generate(item=self,
-                website=website)
+                template_config=template_config)
 
 class BlogEntrySet(object):
 
@@ -66,24 +66,24 @@ class BlogEntrySet(object):
     def __iter__(self):
         return iter(self.entries)
 
-def generate(dir, xslt, website):
+def generate(dir, xslt, template_config):
     entries = BlogEntrySet(dir)
     
     for entry in entries:
         rendered = template_loader.load('entry.html').generate(item=entry,
-                website=website).render('xhtml')
+                template_config=template_config).render('xhtml')
         transformed = str(xslt(lxml.etree.fromstring(rendered)))
         constance.output(os.path.join(dir, entry.id.encode('utf8') + '.html'), transformed)
     
     # index
     rendered = template_loader.load('index.html').generate(items=entries,
-            website=website).render('xhtml')
+            template_config=template_config).render('xhtml')
     transformed = str(xslt(lxml.etree.fromstring(rendered)))
     constance.output(os.path.join(dir, 'index.html'), transformed)
 
     # feed
     rendered = template_loader.load('index.atom').generate(items=entries,
-            website=website).render('xml')
+            template_config=template_config).render('xml')
     constance.output(os.path.join(dir, 'index.atom'), rendered)
 
     return entries
diff --git a/constance.py b/constance.py
@@ -5,6 +5,7 @@ import os, sys
 sys.path.insert(0, os.path.dirname(__file__))
 sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'lib'))
 
+from ConfigParser import SafeConfigParser
 import re
 import datetime
 import time
@@ -28,40 +29,60 @@ def output(filename, content):
     open(filename, 'w').write(content)
     print 'Wrote %s' % filename
 
+default_config = """[paths]
+blog = ./blog/
+reading_log =
+tags = ./tags/
+root = ./
+xslt = ./style.xsl
+
+[template]
+website = http://localhost
+"""
+
 def main():
+    config_filename = os.path.expanduser('~/.constance')
+
+    # set up argument parser
     parser = optparse.OptionParser()
-    parser.add_option('--blog-dir', metavar='DIR')
-    parser.add_option('--reading-log', metavar='FILENAME')
-    parser.add_option('--tags-dir', metavar='DIR')
-    parser.add_option('--root-dir', metavar='DIR')
-    parser.add_option('--xslt', metavar='FILENAME')
-    parser.add_option('--website', metavar='URL')
-    parser.set_defaults(blog_dir='./blog/',
-            reading_log=None,
-            tags_dir='./tags/',
-            root_dir='./',
-            xslt='./style.xsl',
-            website='http://localhost')
+    parser.add_option('--config', metavar='FILENAME')
+    parser.set_defaults(config=config_filename)
     options, args = parser.parse_args()
 
+    # populate config from default location (which would have been
+    # overidden by --config above, if given)
+    config = SafeConfigParser(allow_no_value=True)
+    with open(options.config, 'r') as fp:
+        config.readfp(fp)
+    template_config = dict(config.items('template'))
+
     # strip trailing slash if it was given
-    website = options.website
+    website = config.get('template', 'website')
     if website[-1] == '/':
         website = website[:-1]
 
-    xslt = lxml.etree.XSLT(lxml.etree.parse(options.xslt))
-    blog_entries = blog.generate(options.blog_dir, xslt, website)
-    if options.reading_log is not None:
-        reading_entries = reading.generate(options.reading_log, xslt, website)
+    xslt = lxml.etree.XSLT(lxml.etree.parse(config.get('paths', 'xslt')))
+
+    blog_entries = blog.generate(config.get('paths', 'blog'), xslt,
+            template_config=template_config)
+
+    rl_path = config.get('paths', 'reading_log')
+    if rl_path is not None and rl_path != '': # XXX allow_no_value is broken?
+        reading_entries = reading.generate(rl_path, xslt, 
+                template_config=template_config)
     else:
         reading_entries = []
-    tags.generate(options.tags_dir, xslt, blog_entries, website)
-    for filename in os.listdir(options.root_dir):
+
+    tags.generate(config.get('paths', 'tags'), xslt, blog_entries, 
+            template_config=template_config)
+
+    for filename in os.listdir(config.get('paths', 'root')):
         if filename.endswith('.html.in'):
             transformed = str(xslt(lxml.etree.parse(filename)))
             output(filename[:-3], transformed)
-    homepage.generate(options.root_dir, xslt, blog_entries, reading_entries,
-            website)
+
+    homepage.generate(config.get('paths', 'root'), xslt, blog_entries, 
+            reading_entries, template_config=template_config)
 
 if __name__ == '__main__':
     main()
diff --git a/homepage.py b/homepage.py
@@ -13,16 +13,17 @@ template_loader = genshi.template.TemplateLoader(
         os.path.join(os.path.dirname(__file__), 'templates', 'homepage'), 
         variable_lookup='strict')
 
-def generate(dir, xslt, blog_entries, reading_entries, website):
+def generate(dir, xslt, blog_entries, reading_entries, template_config):
     # index
     template = template_loader.load('index.html')
     rendered = template.generate(blog_entries=blog_entries, 
-            reading_entries=reading_entries, website=website).render('xhtml')
+            reading_entries=reading_entries, 
+            template_config=template_config).render('xhtml')
     transformed = str(xslt(lxml.etree.fromstring(rendered)))
     constance.output(os.path.join(dir, 'index.html'), transformed)
 
     # firehose
     rendered = template_loader.load('firehose.atom').generate(
             items=chain(blog_entries, reading_entries),
-            website=website).render('xml')
+            template_config=template_config).render('xml')
     constance.output(os.path.join(dir, 'firehose.atom'), rendered)
diff --git a/reading.py b/reading.py
@@ -39,17 +39,17 @@ class ReadingLogEntrySet(object):
     def __iter__(self):
         return iter(self.entries)
 
-def generate(filename, xslt, website):
+def generate(filename, xslt, template_config):
     entries = ReadingLogEntrySet(filename)
 
     rendered = template_loader.load('reading.html').generate(items=entries,
-            website=website).render('xhtml')
+            template_config=template_config).render('xhtml')
     transformed = str(xslt(lxml.etree.fromstring(rendered)))
     constance.output(os.path.join(os.path.dirname(filename), 'reading.html'), transformed)
 
     # feed
     rendered = template_loader.load('reading.atom').generate(items=entries,
-            website=website).render('xml')
+            template_config=template_config).render('xml')
     constance.output(os.path.join(os.path.dirname(filename), 'reading.atom'), rendered)
 
     return entries
diff --git a/tags.py b/tags.py
@@ -12,7 +12,7 @@ template_loader = genshi.template.TemplateLoader(
         os.path.join(os.path.dirname(__file__), 'templates', 'tags'), 
         variable_lookup='strict')
 
-def generate(dir, xslt, blog_entries, website):
+def generate(dir, xslt, blog_entries, template_config):
     tag_freqs = {}
     for entry in blog_entries:
         for tag in entry.tags:
@@ -25,6 +25,6 @@ def generate(dir, xslt, blog_entries, website):
         constance.output(os.path.join(dir, tag.encode('utf8') + '.html'), transformed)
 
     rendered = template_loader.load('index.html').generate(tag_freqs=tag_freqs,
-            website=website).render('xhtml')
+            template_config=template_config).render('xhtml')
     transformed = str(xslt(lxml.etree.fromstring(rendered)))
     constance.output(os.path.join(dir, 'index.html'), transformed)
diff --git a/templates/blog/entry.atom b/templates/blog/entry.atom
@@ -15,10 +15,10 @@ from viewutils import ATOM_TIME_FORMAT
     <name>Dan C</name>
     <email>djc@djc.id.au</email>
 </author>
-<category py:for="tag in item.tags" scheme="${website}/tags/" term="${tag}" />
-<link rel="alternate" href="${website}/blog/${urllib.quote(item.id.encode('utf8'), '')}" />
+<category py:for="tag in item.tags" scheme="${template_config.get('website')}/tags/" term="${tag}" />
+<link rel="alternate" href="${template_config.get('website')}/blog/${urllib.quote(item.id.encode('utf8'), '')}" />
 <title type="text">${item.title.striptags()}</title>
-<content type="xhtml" xml:base="${website}/blog/${urllib.quote(item.id.encode('utf8'), '')}">
+<content type="xhtml" xml:base="${template_config.get('website')}/blog/${urllib.quote(item.id.encode('utf8'), '')}">
     <div xmlns="http://www.w3.org/1999/xhtml">
         ${item.body}
     </div>
diff --git a/templates/blog/entry.html b/templates/blog/entry.html
@@ -13,7 +13,7 @@ from viewutils import tag_list
 <body>
     <div class="item blog-entry" py:attrs="(item.language is not None) and {'lang': item.language} or {}">
 
-        <h1 class="entry-title"><a href="${website}/blog/${item.id}" rel="bookmark">${item.title}</a></h1>
+        <h1 class="entry-title"><a href="${template_config.get('website')}/blog/${item.id}" rel="bookmark">${item.title}</a></h1>
 
         <div class="date published">${item.publication_date.strftime(str('%-1d %b %Y'))}</div>
 
diff --git a/templates/blog/index.atom b/templates/blog/index.atom
@@ -7,15 +7,15 @@ from viewutils import ATOM_TIME_FORMAT
 sorted_items = sorted(items, key=lambda item: item.publication_date, reverse=True)
 ?>
 
-<id>${website}/blog/index.atom</id>
+<id>${template_config.get('website')}/blog/index.atom</id>
 <title type="text">Dan’s blog</title>
-<link rel="self" type="application/atom+xml" href="${website}/blog/index.atom" />
-<link rel="alternate" href="${website}/blog/" />
+<link rel="self" type="application/atom+xml" href="${template_config.get('website')}/blog/index.atom" />
+<link rel="alternate" href="${template_config.get('website')}/blog/" />
 <generator>constance</generator>
 <updated py:if="sorted_items">${max(item.modified_date for item in sorted_items).strftime(ATOM_TIME_FORMAT)}</updated>
 
 <py:for each="item in sorted_items">
-    ${item.generate_atom(website)}
+    ${item.generate_atom(template_config)}
 </py:for>
 
 </feed>
diff --git a/templates/homepage/firehose.atom b/templates/homepage/firehose.atom
@@ -7,15 +7,15 @@ from viewutils import ATOM_TIME_FORMAT
 sorted_items = sorted(items, key=lambda item: item.publication_date, reverse=True)
 ?>
 
-<id>${website}/firehose.atom</id>
+<id>${template_config.get('website')}/firehose.atom</id>
 <title type="text">Dan’s firehose</title>
-<link rel="self" type="application/atom+xml" href="${website}/firehose.atom" />
-<link rel="alternate" href="${website}/" />
+<link rel="self" type="application/atom+xml" href="${template_config.get('website')}/firehose.atom" />
+<link rel="alternate" href="${template_config.get('website')}/" />
 <generator>constance</generator>
 <updated py:if="sorted_items">${max(item.modified_date for item in sorted_items).strftime(ATOM_TIME_FORMAT)}</updated>
 
 <py:for each="item in sorted_items">
-    ${item.generate_atom(website)}
+    ${item.generate_atom(template_config)}
 </py:for>
 
 </feed>
diff --git a/templates/homepage/index.html b/templates/homepage/index.html
@@ -9,12 +9,6 @@ from viewutils import markdown, mini_markdown, tag_list
 
 <head>
     <link rel="alternate" type="application/atom+xml" title="Atom feed" href="firehose.atom" />
-    <meta name="ICBM" content="-27.485859,152.983643" />
-    <link rel="openid.server" href="http://www.myopenid.com/server" />
-    <link rel="openid.delegate" href="http://djc.myopenid.com/" />
-    <link rel="openid2.local_id" href="http://djc.myopenid.com" />
-    <link rel="openid2.provider" href="http://www.myopenid.com/server" />
-    <meta http-equiv="X-XRDS-Location" content="http://www.myopenid.com/xrds?username=djc.myopenid.com" />
     <title>Dan’s homepage</title>
 </head>
 
diff --git a/templates/reading/entry.atom b/templates/reading/entry.atom
@@ -13,7 +13,7 @@ from viewutils import ATOM_TIME_FORMAT
     <name>Dan C</name>
     <email>djc@djc.id.au</email>
 </author>
-<category py:for="tag in item.tags" scheme="${website}/tags/" term="${tag}" />
+<category py:for="tag in item.tags" scheme="${template_config.get('website')}/tags/" term="${tag}" />
 <title type="text">${item.title.striptags()} by ${item.author}</title>
 <summary py:if="item.rating" type="text">${item.rating} stars</summary>
 <content type="xhtml">
diff --git a/templates/reading/reading.atom b/templates/reading/reading.atom
@@ -7,15 +7,15 @@ from viewutils import ATOM_TIME_FORMAT
 sorted_items = sorted(items, key=lambda item: item.publication_date, reverse=True)
 ?>
 
-<id>${website}/reading.atom</id>
+<id>${template_config.get('website')}/reading.atom</id>
 <title type="text">Dan’s reading log</title>
-<link rel="self" type="application/atom+xml" href="${website}/reading.atom" />
-<link rel="alternate" href="${website}/reading" />
+<link rel="self" type="application/atom+xml" href="${template_config.get('website')}/reading.atom" />
+<link rel="alternate" href="${template_config.get('website')}/reading" />
 <generator>constance</generator>
 <updated py:if="sorted_items">${max(item.modified_date for item in sorted_items).strftime(ATOM_TIME_FORMAT)}</updated>
 
 <py:for each="item in sorted_items">
-    ${item.generate_atom(website)}
+    ${item.generate_atom(template_config)}
 </py:for>
 
 </feed>