|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ***************************************************************************** |
| 3 | +# Copyright (c) 2020, Intel Corporation All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions are met: |
| 7 | +# |
| 8 | +# Redistributions of source code must retain the above copyright notice, |
| 9 | +# this list of conditions and the following disclaimer. |
| 10 | +# |
| 11 | +# Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +# this list of conditions and the following disclaimer in the documentation |
| 13 | +# and/or other materials provided with the distribution. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 17 | +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 19 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 25 | +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +# ***************************************************************************** |
| 27 | +from pathlib import Path |
| 28 | + |
| 29 | +from sdc_object_utils import get_sdc_object_by_pandas_name, init_sdc_structure |
| 30 | +from sdc_doc_utils import get_docstring, reindent, split_in_sections |
| 31 | +from apiref_generator import (APIREF_TEMPLATE_FNAMES, reformat) |
| 32 | + |
| 33 | + |
| 34 | +EXAMPLES_REL_PATH = Path('.') / '_examples' |
| 35 | + |
| 36 | + |
| 37 | +def get_obj_examples(pandas_name): |
| 38 | + """ |
| 39 | + Get list of examples for Pandas object. |
| 40 | +
|
| 41 | + :param pandas_name: Pandas object for which documentation to be generated. |
| 42 | + :return: Generated docstring. |
| 43 | + """ |
| 44 | + sdc_obj = get_sdc_object_by_pandas_name(pandas_name) |
| 45 | + sdc_doc = get_docstring(sdc_obj) |
| 46 | + sections = split_in_sections(reindent(sdc_doc, 0)) |
| 47 | + sections_as_dict = {title.strip(): text for title, text in sections} |
| 48 | + example_section = sections_as_dict.get('Examples') |
| 49 | + if not example_section: |
| 50 | + return None |
| 51 | + |
| 52 | + examples = [] |
| 53 | + section_names = ['literalinclude', 'command-output'] |
| 54 | + for subsection in example_section.strip().split('\n\n'): |
| 55 | + subsection = subsection.strip() |
| 56 | + if any(subsection.startswith(f'.. {name}') for name in section_names): |
| 57 | + # remove a directory level from path to examples |
| 58 | + examples.append(subsection.replace(' ../', ' ')) |
| 59 | + |
| 60 | + return reformat('\n\n'.join(examples)) |
| 61 | + |
| 62 | + |
| 63 | +def get_tmpl_examples(fname_templ): |
| 64 | + """Get all examples based on input template rst file""" |
| 65 | + tmpl_examples = [] |
| 66 | + with open(fname_templ, encoding='utf-8') as fin: |
| 67 | + doc = fin.readlines() |
| 68 | + |
| 69 | + while len(doc) > 0: |
| 70 | + # Parsing lines until ``.. sdc_toctree`` section is met |
| 71 | + while len(doc) > 0 and not doc[0].startswith('.. sdc_toctree'): |
| 72 | + line = doc[0] |
| 73 | + if line.startswith('.. currentmodule::'): |
| 74 | + current_module_name = line[19:].strip() |
| 75 | + doc.pop(0) |
| 76 | + |
| 77 | + if len(doc) == 0: |
| 78 | + break |
| 79 | + |
| 80 | + doc.pop(0) # Skipping ``.. sdc_toctree`` |
| 81 | + |
| 82 | + # Parsing the list of APIs |
| 83 | + while len(doc) > 0 and doc[0].strip() != '': |
| 84 | + line = doc[0] |
| 85 | + line = line.strip() |
| 86 | + full_name = current_module_name + '.' + line |
| 87 | + doc.pop(0) |
| 88 | + |
| 89 | + obj_examples = get_obj_examples(full_name) |
| 90 | + if obj_examples: |
| 91 | + tmpl_examples.append(obj_examples) |
| 92 | + |
| 93 | + if len(doc) == 0: |
| 94 | + break |
| 95 | + |
| 96 | + return tmpl_examples |
| 97 | + |
| 98 | + |
| 99 | +def generate_examples(): |
| 100 | + """ |
| 101 | + Master function for examples list generation. |
| 102 | +
|
| 103 | + This function initializes SDC data structures, and parses required templates for |
| 104 | + Final RST file generation that lists all the examples. |
| 105 | + """ |
| 106 | + init_sdc_structure() |
| 107 | + |
| 108 | + all_examples = [] |
| 109 | + for templ_fname in APIREF_TEMPLATE_FNAMES: |
| 110 | + all_examples += get_tmpl_examples(templ_fname) |
| 111 | + |
| 112 | + if not EXAMPLES_REL_PATH.exists(): |
| 113 | + EXAMPLES_REL_PATH.mkdir(parents=True, exist_ok=True) |
| 114 | + |
| 115 | + examples_rst_path = EXAMPLES_REL_PATH / 'examples.rst' |
| 116 | + with examples_rst_path.open('w', encoding='utf-8') as fd: |
| 117 | + for examples in all_examples: |
| 118 | + fd.write(examples + '\n') |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + generate_examples() |
0 commit comments