constance.py (2219B) - raw
1 #!/usr/bin/env python 2 # vim:fileencoding=utf-8 3 4 import os, sys 5 sys.path.insert(0, os.path.dirname(__file__)) 6 sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'lib')) 7 8 from ConfigParser import SafeConfigParser 9 import re 10 import datetime 11 import time 12 import urllib 13 import codecs 14 import optparse 15 import genshi.template 16 import lxml.etree 17 18 import blog 19 import reading 20 import tags 21 import homepage 22 23 def output(filename, content): 24 assert isinstance(content, str) 25 26 # XXX ugh, nasty 27 content = re.sub(r'<!DOCTYPE html.*>', '<!DOCTYPE html>', content) 28 29 if os.path.exists(filename): 30 existing = open(filename, 'r').read() 31 if content == existing: 32 print 'Skipped %s' % filename 33 return 34 open(filename, 'w').write(content) 35 print 'Wrote %s' % filename 36 37 def main(): 38 # set up argument parser 39 parser = optparse.OptionParser() 40 parser.add_option('--config', metavar='FILENAME') 41 parser.set_defaults(config='~/.constance.conf') 42 options, args = parser.parse_args() 43 44 # populate config from default location (which would have been 45 # overidden by --config above, if given) 46 config = SafeConfigParser() 47 with codecs.open(os.path.expanduser(options.config), 'r', 'utf8') as fp: 48 config.readfp(fp) 49 50 if config.get('global', 'root'): 51 os.chdir(config.get('global', 'root')) 52 53 xslt = lxml.etree.XSLT(lxml.etree.parse(config.get('global', 'xslt'))) 54 55 if config.getboolean('blog', 'enabled'): 56 blog_entries = blog.generate('blog', xslt, config) 57 else: 58 blog_entries = [] 59 60 if config.getboolean('reading', 'enabled'): 61 reading_entries = reading.generate('reading_log.yaml', xslt, config) 62 else: 63 reading_entries = [] 64 65 if config.getboolean('tags', 'enabled'): 66 tags.generate('tags', xslt, blog_entries, config) 67 68 for filename in os.listdir('.'): 69 if filename.endswith('.html.in'): 70 transformed = str(xslt(lxml.etree.parse(filename))) 71 output(filename[:-3], transformed) 72 73 if config.getboolean('homepage', 'enabled'): 74 homepage.generate('', xslt, blog_entries, 75 reading_entries, config) 76 77 if __name__ == '__main__': 78 main()