0001"""Command line interface."""
0002
0003usage = """
0004Generate documentation for Python projects.
0005
0006OPTIONS:
0007
0008  -d, --dest=DIR     Directory where documentation should be generated.
0009  -i, --documents=FILE...
0010                     Comma seperated list of files
0011  -c, --title=TEXT   The title of the documentation set. This is used in
0012                     various places in generated documentation and defaults
0013                     to "Module Reference". If you include a document named
0014                     "index", the title from that document is used.
0015  -f, --force        Force creation of documentation files even if source
0016                     files are older or the same age.
0017      --license=NAME Include a standard license document. Current options
0018                     are "gnu" for the GNU Free Documentation License and
0019                     "cc" for a Creative Commons Attribution, NonCommercial,
0020                     Copyleft license.
0021  -x, --xhtml        Generate XHTML 1.0 instead of HTML 4.01.
0022                     HTML 4.01 is the default due to browser compatibility
0023                     issues with XHTML 1.0.
0024  -e, --ext=TEXT     The file extension to use when writing (X)HTML files.
0025                     The default is '.html'
0026      --stages=LIST  Specify the list of stages that should be performed.
0027                     This allows only portions of the generation to take
0028                     place. Available stages are: copy, docs, reference,
0029                     index, and source.
0030  -t, --templates=.. The directory where we should look for templates. See
0031                     the 'pudge.templates' package directory for the default
0032                     template set.
0033      --theme=NAME   The name of a built-in theme (overrides --templates).
0034      --trac=URL     Adds navigational links to a Trac site.
0035      
0036  -v, --verbose      Verbose output.
0037  -q, --quiet        Shutup unless something important happens.
0038  -h, --help         print this help screen.
0039
0040Examples:
0041
0042Generate documentation for 'foo' to current directory:
0043
0044  $ pudge -m foo
0045
0046Generate documentation for 'foo' module/package and two documents to 'build/doc':
0047
0048  $ pudge --modules=foo --documents=docs/guide.rst,docs/reference.rst \
0049          --dest=build/doc
0050
0051Generate documentation for the 'foo' module/package and license the work
0052under the GNU Free Documentation License:
0053
0054  $ pudge --license=gnu --modules=foo
0055
0056Pudge is Copyright (c) 2005 by Ryan Tomayko <http://naeblis.cx/rtomayko/>
0057"""
0058
0059import sys
0060import getopt
0061import logging
0062
0063import pudge
0064import pudge.generator
0065
0066def main():
0067    handler = logging.StreamHandler()
0068    formatter = logging.Formatter("%(message)s")
0069    handler.setFormatter(formatter)
0070    pudge.log.addHandler(handler)
0071    pudge.log.setLevel(logging.INFO)
0072    command = PudgeCommand()
0073    command.process_command()
0074
0075class PudgeCommand:
0076    """Handles command line invocation."""
0077
0078    def __init__(self, command=None, args=[]):
0079        self.command = command or sys.argv[0]
0080        self.args = args or sys.argv[1:]
0081
0082    def usage(self, msg=None, exit_with=None):
0083        if msg:
0084            print msg
0085            print "  try: %s --help" % (self.command, )
0086        else:
0087            print 'usage: %s [OPTIONS]' % (self.command, )
0088            print usage.strip()
0089        if exit_with is not None:
0090            sys.exit(exit_with)
0091
0092    def parse_arguments(self):
0093        opts, args = getopt.getopt(self.args, 'hfxqve:d:i:l:m:t:',
0094                                   ['help', 'force', 'xhtml', 'quiet',
0095                                    'verbose',
0096                                    'ext=', 'dest=', 'documents=',
0097                                    'license=', 'trac=',
0098                                    'title=', 'theme=', 'modules=',
0099                                    'templates='])
0100        # TODO handle ImportError, AttributeError, SyntaxError etc...
0101        generator = pudge.generator.Generator()
0102        for o, a in opts:
0103            if o in ('-h', '--help'):
0104                return self.usage
0105            elif o in ('-d', '--dest'):
0106                generator.dest = a
0107            elif o in ('-e', '--ext'):
0108                generator.file_extension = a
0109            elif o in ('-f', '--force'):
0110                generator.force = 1
0111            elif o in ('-q', '--quiet'):
0112                pudge.log.setLevel(logging.WARN)
0113            elif o in ('-v', '--verbose'):
0114                pudge.log.setLevel(logging.DEBUG)
0115            elif o in ('-l', '--title'):
0116                generator.title = a
0117            elif o == '--license':
0118                assert a in ('gnu', 'cc'), "--license should be 'gnu' or 'cc'"
0119                generator.license = a
0120            elif o in ('-m', '--modules'):
0121                generator.modules = a.split(',')
0122            elif o in ('-i', '--documents'):
0123                generator.document_files = a.split(',')
0124            elif o in ('-t', '--templates'):
0125                generator.template_dir = a
0126            elif o in ('--theme', ):
0127                generator.theme = a
0128            elif o in ('-x', '--xhtml'):
0129                generator.xhtml = 1
0130            elif o == '--trac':
0131                generator.trac_url = a
0132        if not generator.modules and not generator.document_files:
0133            self.usage('Must specify --modules or --documents.', exit_with=99)
0134            return
0135        return generator
0136
0137    def process_command(self):
0138        try:
0139            command = self.parse_arguments()
0140        except Exception, e:
0141            raise
0142            self.usage(str(e), exit_with=2)
0143        else:
0144            command()
0145
0146__all__ = ['PudgeCommand']
0147
0148# module attributes
0149__author__ = "Ryan Tomayko <rtomayko@gmail.com>"
0150__date__ = "$Date: 2005-07-01 08:09:30 -0400 (Fri, 01 Jul 2005) $"
0151__revision__ = "$Revision: 54 $"
0152__url__ = "$URL: svn://lesscode.org/pudge/trunk/pudge/cli.py $"
0153__copyright__ = "Copyright 2005, Ryan Tomayko"
0154__license__ = "MIT <http://www.opensource.org/licenses/mit-license.php>"
0155
0156if __name__ == '__main__':
0157    main()