M muyhomepage2/app/build.py +5 -4
@@ 1,5 1,7 @@
+"""site builder
+"""
-
+import os
import jinja2
from muyhomepage2 import site
@@ 11,8 13,8 @@ from muyhomepage2.app import configurati
ENCODING = 'utf-8'
class TemplateConfig(configuration.Config):
- def __init__(self):
- configuration.Config.__init__(self)
+ def __init__(self, filename):
+ configuration.Config.__init__(self, filename)
self._jenv = None
def templateloader(self):
@@ 79,7 81,6 @@ def compile_page(config, website, pctx):
if __name__ == '__main__':
- import os
os.chdir('_sample')
config = TemplateConfig()
build(config)
A => muyhomepage2/app/cli.py +74 -0
@@ 0,0 1,74 @@
+"""
+"""
+
+import sys, os
+import vanity.commander
+
+import muyhomepage2.app.build
+
+
+def main(argv):
+ """launch the desired subcommand"""
+ vanity.commander.main(OPTS, CMDS, argv)
+
+
+def abort(msg, code=1):
+ sys.stderr.write('error: %s\n' % msg)
+ sys.exit(code)
+
+def _newconfig(opts):
+ """get a new configuration object"""
+ if opts.get('config'):
+ cfiles = [opts.get('config')]
+ else:
+ cfiles = [
+ os.path.join('.', '.homepagerc'),
+ os.path.join('.', '.muyhomepagerc'),
+ ]
+ for cfile in cfiles:
+ if os.path.isfile(cfile):
+ return muyhomepage2.app.build.TemplateConfig(cfile)
+ abort('no configuration found')
+
+
+def build(sourcedir='.', **opts):
+ """construct a muyhomepage website"""
+ os.chdir(sourcedir)
+ config = _newconfig(opts)
+ muyhomepage2.app.build.build(config)
+
+
+def flushcache(sourcedir='.', **opts):
+ """empty all cache files"""
+ def zap(fn, verbose):
+ if verbose:
+ print 'flushing cache: %s' % fn
+ fp = open(fn, 'w')
+ try:
+ fp.write('')
+ finally:
+ fp.close()
+ os.chdir(sourcedir)
+ config = _newconfig(opts)
+ zap(config.page_cache(), opts.get('verbose'))
+ zap(config.tag_cache(), opts.get('verbose'))
+
+
+OPTS = [
+ ('config', 'C', '', 'path to configuration file'),
+ ('verbose', 'v', None, 'be verbose'),
+ ]
+
+
+CMDS = {
+ 'flushcache': {
+ 'target':flushcache,
+ 'opts':[],
+ 'help': ''
+ },
+ 'build': {
+ 'target': build,
+ 'opts': [],
+ 'help': '[SOURCEDIR]',
+ },
+ }
M muyhomepage2/app/configuration.py +2 -2
@@ 20,8 20,8 @@ DEFAULT_HANDLERS = {
class Config(object):
- def __init__(self):
- pass
+ def __init__(self, filename):
+ self._filename = filename
def root(self):
return '.'