0001#!/usr/bin/python
0002
0003# This module provides the "kid" command
0004
0005"""Usage: kid [options] file [args]
0006Expand a Kid template file.
0007
0008OPTIONS:
0009
0010  -e enc, --encoding=enc
0011          Specify the output character encoding.
0012          Default: utf-8
0013  -o outfile, --output=outfile
0014          Specify the output file.
0015          Default: standard output
0016  -s host:port, --server=host:port
0017          Specify the server address if
0018          you want to start the HTTP server.
0019          Instead of the Kid template,
0020          you can specify a base directory.
0021  -h, --help
0022          Print this help message and exit.
0023  -V, --version
0024          Print the Kid version number and exit.
0025
0026file:
0027  filename of the Kid template to be processed
0028  or "-" for reading the template from stdin.
0029
0030args:
0031  key=value or other arguments passed to the template.
0032"""
0033
0034__revision__ = "$Rev: 139 $"
0035__date__ = "$Date: 2005-03-14 19:28:22 -0500 (Mon, 14 Mar 2005) $"
0036__author__ = "Ryan Tomayko (rtomayko@gmail.com)"
0037__copyright__ = "Copyright 2004-2005, Ryan Tomayko"
0038__license__ = "MIT <http://www.opensource.org/licenses/mit-license.php>"
0039
0040import sys
0041from os.path import dirname, abspath
0042from getopt import getopt, GetoptError as gerror
0043
0044import kid
0045
0046def main():
0047    # get options
0048    try:
0049        opts, args = getopt(sys.argv[1:], 'e:o:s:hV',
0050            ['encoding=', 'output=', 'server=', 'help', 'version'])
0051    except gerror, e:
0052        sys.stderr.write(str(e) + '\n')
0053        sys.stdout.write(__doc__)
0054        sys.exit(2)
0055    enc = 'utf-8'
0056    outfile = server = None
0057    for o, a in opts:
0058        if o in ('-e', '--encoding'):
0059            enc = a
0060        elif o in ('-o', '--output'):
0061            outfile = a
0062        elif o in ('-s', '--server'):
0063            server = a
0064        elif o in ('-h', '--help'):
0065            sys.stdout.write(__doc__)
0066            sys.exit(0)
0067        elif o in ('-V', '--version'):
0068            from kid import __version__
0069            sys.stdout.write('Kid %s\n' % __version__)
0070            sys.exit(0)
0071    if server is None:
0072        if args:
0073            # get template file
0074            f = args.pop(0)
0075            sys.argv = [f]
0076            if f != '-':
0077                # make sure template dir is on sys.path
0078                path = abspath(dirname(f))
0079                if not path in sys.path:
0080                    sys.path.insert(0, path)
0081            else:
0082                f = sys.stdin.read()
0083            # get arguments for the template file
0084            kw = {}
0085            while args:
0086                a = args.pop(0).split('=', 1)
0087                if len(a) > 1:
0088                    kw[a[0]] = a[1]
0089                else:
0090                    sys.argv.append(a[0])
0091            # do not run as __main__ module
0092            sys.modules['__kid_main__'] = sys.modules['__main__']
0093            __name__ = '__kid_main__'
0094            del sys.modules['__main__']
0095            # load kid template as __main__ module
0096            module = kid.load_template(f, name='__main__', cache=0)
0097            # execute the template and write output
0098            if not outfile:
0099                    outfile = sys.stdout
0100            module.write(outfile, encoding=enc, **kw)
0101        else:
0102            sys.stderr.write('kid: No template file specified.\n')
0103            sys.stderr.write("     Try 'kid --help' for usage information.\n")
0104            sys.exit(2)
0105    else:
0106        if len(args) < 2:
0107            if outfile:
0108                stderr = file(outfile, 'a', 1)
0109                sys.stderr = stderr
0110            sys.stdout.write('Starting HTTP server ...\n')
0111            if args:
0112                # get base directory
0113                basedir = args.pop(0)
0114                from os import chdir
0115                chdir(basedir)
0116            from os import getcwd
0117            basedir = getcwd()
0118            sys.stdout.write('Base directory: %s\n' % basedir)
0119            if outfile:
0120                sys.stdout.write('Server log: %s\n' % outfile)
0121            if server == '-':
0122                server = 'localhost'
0123            sys.argv[1:] = [server]
0124            from kid.server import main
0125            main()
0126            if outfile:
0127                sys.stderr = sys.__stderr__
0128                out.close()
0129        else:
0130            sys.stderr.write('kid: Server does not need additional arguments.\n')
0131            sys.stderr.write("     Try 'kid --help' for usage information.\n")
0132            sys.exit(2)
0133
0134if __name__ == '__main__':
0135    main()