launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/scripts/generate-docs.py (about)

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