github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/scripts/generate-docs.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2013 Canonical Ltd.
     4  # Licensed under the AGPLv3, see LICENCE file for details.
     5  
     6  import os
     7  import sys
     8  from optparse import OptionParser
     9  
    10  from jujuman import JujuMan
    11  
    12  
    13  GENERATORS = {
    14      'man': JujuMan
    15  }
    16  
    17  # Insert the directory that this module is in into the python path.
    18  sys.path.insert(0, (os.path.dirname(__file__)))
    19  
    20  def main(argv):
    21      parser = OptionParser(usage="""%prog [options] OUTPUT_FORMAT
    22  
    23  Available OUTPUT_FORMAT:
    24  
    25      man              man page
    26  
    27  And that is all for now.""")
    28  
    29      parser.add_option("-s", "--show-filename",
    30                        action="store_true", dest="show_filename", default=False,
    31                        help="print default filename on stdout")
    32  
    33      parser.add_option("-o", "--output", dest="filename", metavar="FILE",
    34                        help="write output to FILE")
    35  
    36      (options, args) = parser.parse_args(argv)
    37  
    38      if len(args) != 2:
    39          parser.print_help()
    40          sys.exit(1)
    41  
    42      try:
    43          doc_generator = GENERATORS[args[1]]()
    44      except KeyError as e:
    45          sys.stderr.write("Unknown documentation generator %r\n" % e.message)
    46          sys.exit(1)
    47  
    48      if options.filename:
    49          outfilename = options.filename
    50      else:
    51          outfilename = doc_generator.get_filename(options)
    52  
    53      if outfilename == "-":
    54          outfile = sys.stdout
    55      else:
    56          outfile = open(outfilename, "w")
    57      if options.show_filename and (outfilename != "-"):
    58          sys.stdout.write(outfilename)
    59          sys.stdout.write('\n')
    60  
    61      doc_generator.write_documentation(options, outfile)
    62  
    63  
    64  if __name__ == "__main__":
    65      main(sys.argv)