parse_src.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. # This script will parse source files for docstring.
  3. import os, codecs
  4. path = os.path.realpath(__file__)
  5. script = os.path.basename(path)
  6. d_root = os.path.dirname(os.path.dirname(path))
  7. d_ldoc = os.path.join(d_root, ".ldoc")
  8. armor_types = {
  9. "armor": {"topic": "Armors", "values": []},
  10. "helmet": {"topic": "Helmets", "values": []},
  11. "chestplate": {"topic": "Chestplates", "values": []},
  12. "leggings": {"topic": "Leggings", "values": []},
  13. "boots": {"topic": "Boots", "values": []},
  14. #"shield": {"topic": "Shields", "values": []},
  15. }
  16. def parse_file(f):
  17. buffer = codecs.open(f, "r", "utf-8")
  18. if not buffer:
  19. print("ERROR: could not open file for reading: {}".format(f))
  20. return
  21. data_in = buffer.read()
  22. buffer.close()
  23. # format to LF (Unix)
  24. data_in = data_in.replace("\r\n", "\n").replace("\r", "\n")
  25. current_item = []
  26. item_type = None
  27. new_item = False
  28. for li in data_in.split("\n"):
  29. li = li.strip()
  30. if li.startswith("---"):
  31. new_item = True
  32. elif not li.startswith("--"):
  33. new_item = False
  34. if new_item:
  35. current_item.append(li)
  36. if not item_type:
  37. for a_type in armor_types:
  38. if "@{} ".format(a_type) in li:
  39. item_type = a_type
  40. break
  41. elif item_type and len(current_item):
  42. armor_types[item_type]["values"].append("\n".join(current_item))
  43. item_type = None
  44. current_item = []
  45. else:
  46. current_item = []
  47. to_parse = []
  48. for obj in os.listdir(d_root):
  49. fullpath = os.path.join(d_root, obj)
  50. if not obj.startswith(".") and os.path.isdir(fullpath):
  51. for root, dirs, files in os.walk(fullpath):
  52. for f in files:
  53. if f.endswith(".lua"):
  54. to_parse.append(os.path.join(root, f))
  55. for p in to_parse:
  56. if not os.path.isfile(p):
  57. print("ERROR: {} is not a file".format(p))
  58. else:
  59. parse_file(p)
  60. for t in armor_types:
  61. topic = armor_types[t]["topic"]
  62. items = armor_types[t]["values"]
  63. if len(items):
  64. outfile = os.path.join(d_ldoc, "{}.luadoc".format(topic.lower()))
  65. buffer = codecs.open(outfile, "w", "utf-8")
  66. if not buffer:
  67. print("ERROR: could not open file for writing: {}".format(outfile))
  68. continue
  69. buffer.write("\n--- 3D Armor {}\n--\n-- @topic {}\n\n\n{}\n".format(topic, topic.lower(), "\n\n".join(items)))
  70. buffer.close()
  71. print("{} exported to\t{}".format(topic.lower(), outfile))