documentation.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2015 Red Hat, Inc.
  4. #
  5. # Author: Martin Magr <mmagr@redhat.com>
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. from docutils import core
  20. # ------------------ helpers to locate option list ------------------ #
  21. def _iter_by_titles(tree):
  22. for i in tree.children:
  23. i = i.asdom()
  24. for child in i.childNodes:
  25. if child.nodeName != 'title':
  26. continue
  27. if child.childNodes and child.childNodes[0].nodeValue:
  28. title = child.childNodes[0].nodeValue
  29. yield title, i
  30. def _get_options(tree, section):
  31. for title, node in _iter_by_titles(tree):
  32. if title == section:
  33. return node
  34. # --------------------- helper to locate options -------------------- #
  35. def _iter_options(section):
  36. for subsection in section.childNodes:
  37. for subsub in subsection.childNodes:
  38. if subsub.nodeName != 'definition_list':
  39. # TO-DO: log parsing warning
  40. continue
  41. for defitem in subsub.childNodes:
  42. key_node = defitem.getElementsByTagName('strong')
  43. val_node = defitem.getElementsByTagName('paragraph')
  44. if not key_node or not val_node:
  45. # TO-DO: log parsing warning
  46. continue
  47. key_node = key_node[0].childNodes[0]
  48. val_node = val_node[0].childNodes[0]
  49. yield key_node.nodeValue, val_node.nodeValue
  50. # ----------------------------- interface --------------------------- #
  51. _rst_cache = {}
  52. def update_params_usage(path, params, opt_title='OPTIONS', sectioned=True):
  53. """Updates params dict with USAGE texts parsed from given rst file."""
  54. def _update(section, rst):
  55. for param in section:
  56. if param['CONF_NAME'] not in rst:
  57. # TO-DO: log warning
  58. continue
  59. param['USAGE'] = rst[param['CONF_NAME']]
  60. if not _rst_cache:
  61. tree = core.publish_doctree(
  62. source=open(path).read().decode('utf-8'), source_path=path
  63. )
  64. for key, value in _iter_options(_get_options(tree, opt_title)):
  65. _rst_cache.setdefault(key, value)
  66. if sectioned:
  67. for section in params.values():
  68. _update(section, _rst_cache)
  69. else:
  70. _update(params, _rst_cache)