doc.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """
  2. doc.py
  3. This module allows documentation for Python classes and modules to be viewed
  4. in-game via the 'doc' command.
  5. """
  6. import pydoc, os, mudsys, display
  7. ################################################################################
  8. # local variables
  9. ################################################################################
  10. # where do we store documentation?
  11. HTML_DOC_DIR = "../html/pydocs"
  12. shortcuts = { "ch" : "char",
  13. "sock" : "mudsock",
  14. }
  15. # just a list of all our builtin modules
  16. builtins = [
  17. "char",
  18. "room",
  19. "obj",
  20. "exit",
  21. "account",
  22. "mudsock",
  23. "mud",
  24. "mudsys",
  25. "hooks",
  26. "event",
  27. "auxiliary",
  28. "storage",
  29. "olc",
  30. ]
  31. # append all of our builtins to suggested reading list
  32. suggested_reading = [mod for mod in builtins]
  33. def register_module_doc(modname, package = None, root = "pymodules"):
  34. """Add a new module name to suggested_reading. If modname is a package,
  35. recursively add its packages as well
  36. """
  37. fname = root + "/" + modname.replace(".", "/")
  38. suggested_reading.append(modname)
  39. if os.path.isdir(fname):
  40. for file in os.listdir(fname):
  41. if (file.endswith(".py") or not "." in file) and not file[0] in "._":
  42. module = modname + "." + file.split(".", 1)[0]
  43. register_module_doc(module, root)
  44. # now, append all of our Python packages and modules
  45. for fname in os.listdir("pymodules/"):
  46. # look for modules and packages
  47. if (fname.endswith(".py") or not "." in fname) and not fname[0] in "._":
  48. modname = fname.split(".", 1)[0]
  49. register_module_doc(modname)
  50. ################################################################################
  51. # player commands
  52. ################################################################################
  53. def cmd_htmldoc(ch, cmd, arg):
  54. """Creates html documentation for all registered modules. html files will
  55. be saved to html/pydocs/
  56. """
  57. try:
  58. os.makedirs(HTML_DOC_DIR)
  59. except: pass
  60. doc = pydoc.HTMLDoc()
  61. for modname in suggested_reading:
  62. todoc = pydoc.locate(modname)
  63. if todoc != None:
  64. fname = HTML_DOC_DIR + "/" + modname + ".html"
  65. fl = file(fname, "w+")
  66. fl.write(doc.page(modname, doc.document(todoc)))
  67. fl.close()
  68. builtin_index = doc.multicolumn([doc.modulelink(pydoc.locate(modname)) for modname in builtins], lambda x: x)
  69. # build our index page. That includes things in pymodules/ and builtins
  70. index_contents ="".join([doc.section("<big><strong>builtins</big></strong>",
  71. 'white', '#ee77aa', builtin_index),
  72. doc.index("../lib/pymodules/")])
  73. # go over all of our builtins and add them to the index
  74. index = file(HTML_DOC_DIR + "/index.html", "w+")
  75. index.write(doc.page("index", index_contents))
  76. index.close()
  77. ch.send("html documentation generated for all known modules.")
  78. def cmd_doc(ch, cmd, arg):
  79. """Return Python documentation for the specified module, class, function,
  80. etc... for example:
  81. > doc char.Char
  82. Will return all available documentation for the Char class.
  83. """
  84. if arg == "":
  85. ch.page("\r\n".join(display.pagedlist({ "Topics" : suggested_reading },
  86. header = "Suggested doc readings include:")))
  87. else:
  88. # just because sometimes I forget periods
  89. arg = arg.replace(" ", ".")
  90. # are we looking for a shortcut value?
  91. if arg in shortcuts:
  92. arg = shortcuts[arg]
  93. # try to find what we're documenting
  94. todoc = pydoc.locate(arg)
  95. if todoc == None:
  96. ch.send("Could not find Python documentation on: '%s'" % arg)
  97. else:
  98. doc = pydoc.TextDoc()
  99. ch.page(doc.document(todoc).replace("{", "{{"))
  100. ################################################################################
  101. # initialization
  102. ################################################################################
  103. mudsys.add_cmd("doc", None, cmd_doc, "wizard", False)
  104. mudsys.add_cmd("htmldoc", None, cmd_htmldoc, "admin", False)