Andrew M. Kuchling | b68ef50 | 2001-02-28 20:55:10 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # -------------------------------------------------- command-line interface |
| 4 | |
| 5 | import sys, os, pydoc |
| 6 | from string import lower |
| 7 | |
| 8 | if __name__ == '__main__': |
| 9 | import getopt |
| 10 | class BadUsage: pass |
| 11 | |
| 12 | try: |
| 13 | opts, args = getopt.getopt(sys.argv[1:], 'k:p:w') |
| 14 | writing = 0 |
| 15 | |
| 16 | for opt, val in opts: |
| 17 | if opt == '-k': |
| 18 | pydoc.apropos(lower(val)) |
| 19 | break |
| 20 | if opt == '-p': |
| 21 | try: |
| 22 | port = int(val) |
| 23 | except ValueError: |
| 24 | raise BadUsage |
| 25 | def ready(port=port): |
| 26 | print 'server ready at http://127.0.0.1:%d/' % port |
| 27 | pydoc.serve(('127.0.0.1', port), ready) |
| 28 | break |
| 29 | if opt == '-w': |
| 30 | if not args: raise BadUsage |
| 31 | writing = 1 |
| 32 | else: |
| 33 | if args: |
| 34 | for arg in args: |
| 35 | try: |
| 36 | if os.path.isfile(arg): |
| 37 | arg = pydoc.importfile(arg) |
| 38 | if writing: |
| 39 | if os.path.isdir(arg): pydoc.writedocs(arg) |
| 40 | else: pydoc.writedoc(arg) |
| 41 | else: pydoc.man(arg) |
| 42 | except pydoc.DocImportError, value: |
| 43 | print 'problem in %s - %s' % ( |
| 44 | value.filename, value.args) |
| 45 | else: |
| 46 | if sys.platform in ['mac', 'win', 'win32', 'nt']: |
| 47 | # GUI platforms with threading |
| 48 | import threading |
| 49 | ready = threading.Event() |
| 50 | address = ('127.0.0.1', 12346) |
| 51 | threading.Thread( |
| 52 | target=pydoc.serve, args=(address, ready.set)).start() |
| 53 | ready.wait() |
| 54 | import webbrowser |
| 55 | webbrowser.open('http://127.0.0.1:12346/') |
| 56 | else: |
| 57 | raise BadUsage |
| 58 | |
| 59 | except (getopt.error, BadUsage): |
| 60 | print """%s <name> ... |
| 61 | Show documentation on something. |
| 62 | <name> may be the name of a Python function, module, or package, |
| 63 | or a dotted reference to a class or function within a module or |
| 64 | module in a package, or the filename of a Python module to import. |
| 65 | |
| 66 | %s -k <keyword> |
| 67 | Search for a keyword in the synopsis lines of all modules. |
| 68 | |
| 69 | %s -p <port> |
| 70 | Start an HTTP server on the given port on the local machine. |
| 71 | |
| 72 | %s -w <module> ... |
| 73 | Write out the HTML documentation for a module to a file. |
| 74 | |
| 75 | %s -w <moduledir> |
| 76 | Write out the HTML documentation for all modules in the tree |
| 77 | under a given directory to files in the current directory. |
| 78 | """ % ((sys.argv[0],) * 5) |