Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

schema2html.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2003 Zope Corporation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. from __future__ import print_function
  15. import argparse
  16. from contextlib import contextmanager
  17. try:
  18. import html
  19. except ImportError:
  20. # Py2
  21. import cgi as html
  22. import sys
  23. from ZConfig._schema_utils import AbstractSchemaPrinter
  24. from ZConfig._schema_utils import AbstractSchemaFormatter
  25. from ZConfig._schema_utils import MARKER
  26. from ZConfig._schema_utils import load_schema
  27. from ZConfig.sphinx import RstSchemaPrinter
  28. class HtmlSchemaFormatter(AbstractSchemaFormatter):
  29. def esc(self, x):
  30. return html.escape(str(x))
  31. @contextmanager
  32. def _simple_tag(self, tag):
  33. self.write("<%s>" % tag)
  34. yield
  35. self.write("</%s>" % tag)
  36. def item_list(self):
  37. return self._simple_tag("dl")
  38. @contextmanager
  39. def describing(self, description=MARKER, after=None):
  40. with self._simple_tag("dt"):
  41. yield
  42. self._describing(description, after)
  43. def described_as(self):
  44. return self._simple_tag("dd")
  45. def abstract_name(self, name):
  46. self.write("<b><i>", name, "</b></i>")
  47. def concrete_name(self, *name):
  48. self.write("<b>", *name)
  49. self.write("</b>")
  50. def concrete_section_name(self, *name):
  51. name = ' '.join(name)
  52. self.write("<b>", self.esc("<%s>" % name), "</b>")
  53. def datatype(self, datatype):
  54. self.write("(%s)" % self._dt(datatype))
  55. def example(self, text):
  56. if not text:
  57. return
  58. with self._simple_tag("p"):
  59. with self._simple_tag("i"):
  60. self.write("Example:")
  61. with self._simple_tag("pre"):
  62. self.write(self.esc(self._dedent(text)))
  63. @contextmanager
  64. def body(self):
  65. self.write('''<html><body>
  66. <style>
  67. dl {margin: 0 0 1em 0;}
  68. </style>
  69. ''')
  70. yield
  71. self.write('</body></html>')
  72. class HtmlSchemaPrinter(AbstractSchemaPrinter):
  73. _schema_formatter = HtmlSchemaFormatter
  74. def main(argv=None):
  75. argv = argv if argv is not None else sys.argv[1:]
  76. argparser = argparse.ArgumentParser(
  77. description="Print an HTML version of a schema")
  78. argparser.add_argument(
  79. "schema",
  80. metavar='[SCHEMA-OR-PACKAGE]',
  81. help="The schema to print. By default, a file. Optionally, a Python package."
  82. " If not given, defaults to reading a schema file from stdin",
  83. default="-"
  84. )
  85. argparser.add_argument(
  86. "--out", "-o",
  87. help="Write the schema to this file; if not given, write to stdout",
  88. type=argparse.FileType('w'))
  89. argparser.add_argument(
  90. "--package",
  91. action='store_true',
  92. default=False,
  93. help="The SCHEMA-OR-PACKAGE argument indicates a Python package instead of a file."
  94. " The component.xml (by default) from the package will be read.")
  95. argparser.add_argument(
  96. "--package-file",
  97. action="store",
  98. default="component.xml",
  99. help="When PACKAGE is given, this can specify the file inside it to load.")
  100. argparser.add_argument(
  101. "--members",
  102. action="store",
  103. nargs="*",
  104. help="Only output sections and types in this list (and reachable from it)")
  105. if RstSchemaPrinter:
  106. argparser.add_argument(
  107. "--format",
  108. action="store",
  109. choices=('html', 'xml'), # XXX Can we get actual valid RST out?
  110. default="HTML",
  111. help="What output format to produce"
  112. )
  113. args = argparser.parse_args(argv)
  114. out = args.out or sys.stdout
  115. schema = load_schema(args.schema, args.package, args.package_file)
  116. printer_factory = HtmlSchemaPrinter
  117. if hasattr(args, 'format') and args.format == 'xml':
  118. printer_factory = RstSchemaPrinter
  119. printer_factory(schema, out, allowed_names=args.members).printSchema()
  120. return 0
  121. if __name__ == '__main__':
  122. main()