Commits

John Mulligan committed 6b613a9

start of a cli

  • Participants
  • Parent commits b3ea223

Comments (0)

Files changed (3)

muyhomepage2/app/build.py

+"""site builder
+"""
 
-
+import os
 import jinja2
 
 from muyhomepage2 import site
 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):
             
 
 if __name__ == '__main__':
-    import os
     os.chdir('_sample')
     config = TemplateConfig()
     build(config)

muyhomepage2/app/cli.py

+"""
+"""
+
+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]',
+        },
+    }

muyhomepage2/app/configuration.py

 
 class Config(object):
 
-    def __init__(self):
-        pass
+    def __init__(self, filename):
+        self._filename = filename
 
     def root(self):
         return '.'