|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import re |
| 5 | +import os |
| 6 | +from bs4 import BeautifulSoup |
| 7 | + |
| 8 | +# walk the combined output. |
| 9 | +# for each function: |
| 10 | +# check if it is in the output for one chip, or both |
| 11 | +# if for only one chip, add a role to that section accordingly. |
| 12 | + |
| 13 | +# instead of searching every xml every time, make a list of available functions in each xml |
| 14 | +def compile_id_list(xml_content): |
| 15 | + # get any element that has an id |
| 16 | + els = xml_content.find_all(id=True) |
| 17 | + id_list = [x["id"] for x in els] |
| 18 | + return id_list |
| 19 | + |
| 20 | +def walk_and_tag_xml_tree(el, output_contexts, all_contexts): |
| 21 | + """ |
| 22 | + Process an individual xml file, adding context-specific tags as needed. |
| 23 | +
|
| 24 | + For performance purposes (to avoid traversing multiple dicts for every element), |
| 25 | + we use element IDs as the key, and the contexts it belongs to as the value. |
| 26 | + Thus, output_contexts will look something like this: |
| 27 | + { |
| 28 | + "group__hardware__gpio_1gaecd01f57f1cac060abe836793f7bea18": [ |
| 29 | + "PICO_RP2040", |
| 30 | + "FOO" |
| 31 | + ], |
| 32 | + "group__hardware__gpio_1ga7becbc8db22ff0a54707029a2c0010e6": [ |
| 33 | + "PICO_RP2040" |
| 34 | + ], |
| 35 | + "group__hardware__gpio_1ga192335a098d40e08b23cc6d4e0513786": [ |
| 36 | + "PICO_RP2040" |
| 37 | + ], |
| 38 | + "group__hardware__gpio_1ga8510fa7c1bf1c6e355631b0a2861b22b": [ |
| 39 | + "FOO", |
| 40 | + "BAR" |
| 41 | + ], |
| 42 | + "group__hardware__gpio_1ga5d7dbadb2233e2e6627e9101411beb27": [ |
| 43 | + "FOO" |
| 44 | + ] |
| 45 | + } |
| 46 | + """ |
| 47 | + targets = [] |
| 48 | + if el.get('id') is not None: |
| 49 | + myid = el["id"] |
| 50 | + if myid in output_contexts: |
| 51 | + targets = output_contexts[myid] |
| 52 | + # if this content is in all contexts, no label is required |
| 53 | + if len(targets) > 0 and len(targets) < len(all_contexts): |
| 54 | + el["role"] = "contextspecific" |
| 55 | + el["tag"] = ', '.join(targets) |
| 56 | + if len(targets) > 1: |
| 57 | + el["type"] = "multi" |
| 58 | + else: |
| 59 | + el["type"] = targets[0] |
| 60 | + # only check nested children if the parent has NOT been tagged as context-specific |
| 61 | + else: |
| 62 | + # for child in el.iterchildren(): |
| 63 | + for child in el.find_all(True, recursive=False): |
| 64 | + walk_and_tag_xml_tree(child, output_contexts, all_contexts) |
| 65 | + else: |
| 66 | + for child in el.find_all(True, recursive=False): |
| 67 | + walk_and_tag_xml_tree(child, output_contexts, all_contexts) |
| 68 | + return |
| 69 | + |
| 70 | +def postprocess_doxygen_xml_file(combined_xmlfile, xmlfiles, output_context_paths): |
| 71 | + """ |
| 72 | + Process an individual xml file, adding context-specific tags as needed. |
| 73 | +
|
| 74 | + xmlfiles will look something like this: |
| 75 | + { |
| 76 | + "PICO_RP2040": "/path/to/PICO_RP2040/myfilename.xml", |
| 77 | + "FOO": "/path/to/FOO/myfilename.xml" |
| 78 | + } |
| 79 | + """ |
| 80 | + output_contexts = {} |
| 81 | + for item in xmlfiles: |
| 82 | + label = item |
| 83 | + # parse the xml file |
| 84 | + with open(xmlfiles[item], encoding="utf-8") as f: |
| 85 | + xml_content = BeautifulSoup(f, 'xml') |
| 86 | + # compile a list of all element ids within the file |
| 87 | + id_list = compile_id_list(xml_content.doxygen) |
| 88 | + # create the map of ids and their contexts (see example above) |
| 89 | + for myid in id_list: |
| 90 | + if myid in output_contexts: |
| 91 | + output_contexts[myid].append(label) |
| 92 | + else: |
| 93 | + output_contexts[myid] = [label] |
| 94 | + with open(combined_xmlfile, encoding="utf-8") as f: |
| 95 | + combined_content = BeautifulSoup(f, 'xml') |
| 96 | + # start with top-level children, and then walk the tree as appropriate |
| 97 | + els = combined_content.doxygen.find_all(True, recursive=False) |
| 98 | + for el in els: |
| 99 | + walk_and_tag_xml_tree(el, output_contexts, list(output_context_paths.keys())) |
| 100 | + return str(combined_content) |
| 101 | + |
| 102 | +def postprocess_doxygen_xml(xml_path): |
| 103 | + """ |
| 104 | + Expectation is that xml for each context will be generated |
| 105 | + within a subfolder titled with the context name, e.g.: |
| 106 | + - doxygen_build/ |
| 107 | + - combined/ |
| 108 | + - PICO_RP2040/ |
| 109 | + - FOO/ |
| 110 | + """ |
| 111 | + # collect a list of all context-specific subdirs |
| 112 | + skip = ["index.xml", "Doxyfile.xml"] |
| 113 | + output_context_paths = {} |
| 114 | + combined_output_path = None |
| 115 | + for item in list(filter(lambda x: os.path.isdir(os.path.join(xml_path, x)), os.listdir(xml_path))): |
| 116 | + if item == "combined": |
| 117 | + # if doxygen ever changes the output path for the xml, this will need to be updated |
| 118 | + combined_output_path = os.path.join(xml_path, item, "docs", "doxygen", "xml") |
| 119 | + else: |
| 120 | + # same as above |
| 121 | + output_context_paths[item] = os.path.join(xml_path, item, "docs", "doxygen", "xml") |
| 122 | + # we need to process all generated xml files |
| 123 | + for combined_xmlfile in list(filter(lambda x: re.search(r'\.xml$', x) is not None, os.listdir(combined_output_path))): |
| 124 | + # skip the index -- it's just a listing |
| 125 | + if combined_xmlfile not in skip: |
| 126 | + xmlfiles = {} |
| 127 | + # get all context-specific versions of this file |
| 128 | + for context in output_context_paths: |
| 129 | + if os.path.isfile(os.path.join(output_context_paths[context], combined_xmlfile)): |
| 130 | + xmlfiles[context] = os.path.join(output_context_paths[context], combined_xmlfile) |
| 131 | + combined_content = postprocess_doxygen_xml_file(os.path.join(combined_output_path, combined_xmlfile), xmlfiles, output_context_paths) |
| 132 | + # write the output |
| 133 | + with open(os.path.join(combined_output_path, combined_xmlfile), 'w') as f: |
| 134 | + f.write(combined_content) |
| 135 | + return |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + xml_path = sys.argv[1] |
| 139 | + print(xml_path) |
| 140 | + file_path = os.path.realpath(__file__) |
| 141 | + # splitting thse subs into two parts to make testing easier |
| 142 | + # xml_path = re.sub(r'/documentation-toolchain/.*?$', "/"+xml_path, re.sub(r'/lib/', "/", file_path)) |
| 143 | + postprocess_doxygen_xml(xml_path) |
0 commit comments