0001
0002
0003
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
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
0074 f = args.pop(0)
0075 sys.argv = [f]
0076
0077 path = abspath(dirname(f))
0078 if not path in sys.path:
0079 sys.path.insert(0, path)
0080
0081 kw = {}
0082 while args:
0083 a = args.pop(0).split('=', 1)
0084 if len(a) > 1:
0085 kw[a[0]] = a[1]
0086 else:
0087 sys.argv.append(a[0])
0088
0089 sys.modules['__kid_main__'] = sys.modules['__main__']
0090 __name__ = '__kid_main__'
0091 del sys.modules['__main__']
0092
0093 module = kid.load_template(f, name='__main__', cache=0)
0094
0095 if not outfile:
0096 outfile = sys.stdout
0097 module.write(outfile, encoding=enc, **kw)
0098 else:
0099 sys.stderr.write('kid: No template file specified.\n')
0100 sys.stderr.write(" Try 'kid --help' for usage information.\n")
0101 sys.exit(2)
0102 else:
0103 if len(args) < 2:
0104 if outfile:
0105 stderr = file(outfile, 'a', 1)
0106 sys.stderr = stderr
0107 sys.stdout.write('Starting HTTP server ...\n')
0108 if args:
0109
0110 basedir = args.pop(0)
0111 from os import chdir
0112 chdir(basedir)
0113 from os import getcwd
0114 basedir = getcwd()
0115 sys.stdout.write('Base directory: %s\n' % basedir)
0116 if outfile:
0117 sys.stdout.write('Server log: %s\n' % outfile)
0118 if server == '-':
0119 server = 'localhost'
0120 sys.argv[1:] = [server]
0121 from kid.server import main
0122 main()
0123 if outfile:
0124 sys.stderr = sys.__stderr__
0125 out.close()
0126 else:
0127 sys.stderr.write('kid: Server does not need additional arguments.\n')
0128 sys.stderr.write(" Try 'kid --help' for usage information.\n")
0129 sys.exit(2)
0130
0131if __name__ == '__main__':
0132 main()