Skip to content
This repository
tree: 130b9dce45
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 90 lines (69 sloc) 2.327 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#!/usr/bin/env python

import os, sys, codecs, json, cherrypy
from optparse import OptionParser
from lanyon import Site

    
def main( argv ):
    command = argv[0]

    if command == "generate":
        _generate( argv[1:] )
    
    elif command == "serve":
        _start_server( argv[1:] )
    
    else:
        print 'Usage: lanyon (generate|serve) [options]';

def _start_server( args ):
    parser = OptionParser( 'usage: %prog serve [options]' )
    parser.add_option(
        '-c', '--config',
        dest="config_path",
        default="site-config.json",
        help="The path to the configuration file to use for site genration"
    )
    parser.add_option(
        '-p', '--port',
        dest="port",
        default="8080",
        help="The port to listen on"
    )

    (options, args) = parser.parse_args( args )

    print options.config_path

    config = _read_config( options.config_path )

    print "# Starting server on port %s: " % str( options.port )
    
    class WebRoot:
        pass
        
    cherrypy.config.update( {
        'environment': 'production',
        'log.error_file': 'site.log',
        'log.screen': True,
        'server.socket_port': int(options.port)
    } )
    
    cherrypy.quickstart( WebRoot(), '/', config = {
        '/': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': os.path.abspath(config['output_path']),
            'tools.staticdir.index': 'index.html'
        }
    } )

def _generate( args ):
    parser = OptionParser( 'usage: %prog generate [options]' )
    parser.add_option(
        '-c', '--config',
        dest="config_path",
        default="site-config.json",
        help="The path to the configuration file to use for site genration"
    )
    (options, args) = parser.parse_args( args )

    config = _read_config( options.config_path )

    site = Site( config = config )

    print "# Generating site: "
    site.generate()

def _read_config( config_path ):
    # TODO: Add safety net
    print "# Reading config '" + config_path + "':"
    full_path = os.path.join( os.getcwdu(), config_path )
    config_file = codecs.open( full_path, 'r', 'utf-8' )
    config = json.load( config_file )
    config_file.close()
    config_file = None

    return config

if __name__ == "__main__":
    main(sys.argv[1:])
Something went wrong with that request. Please try again.