|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# extract_api_functions.py |
| 4 | +# |
| 5 | +# This file is part of NEST. |
| 6 | +# |
| 7 | +# Copyright (C) 2004 The NEST Initiative |
| 8 | +# |
| 9 | +# NEST is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation, either version 2 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# NEST is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with NEST. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +import os |
| 22 | +import ast |
| 23 | +import json |
| 24 | +import re |
| 25 | +import glob |
| 26 | + |
| 27 | + |
| 28 | +""" |
| 29 | +Generate a JSON dictionary that stores the module name as key and corresponding |
| 30 | +functions as values, along with the ``NestModule`` and the kernel attributes. |
| 31 | +Used in a Jinja template to generate the autosummary for each module in |
| 32 | +the API documentation (``ref_material/pynest_api/``) |
| 33 | +""" |
| 34 | + |
| 35 | + |
| 36 | +def find_all_variables(file_path): |
| 37 | + """ |
| 38 | + This function gets the names of all functions listed in ``__all__`` |
| 39 | + in each of the PyNEST API files, along with the Kernel Attributes |
| 40 | + found in ``__init__.py`` of ``pynest/nest/``. |
| 41 | + """ |
| 42 | + all_variables = None |
| 43 | + |
| 44 | + if "pynest/nest/__init__" in file_path: |
| 45 | + # Read the __init__.py file |
| 46 | + with open(file_path, "r") as init_file: |
| 47 | + file_content = init_file.read() |
| 48 | + |
| 49 | + # Find the class definition |
| 50 | + match = re.search(r"class\s+NestModule\(.*?\):", file_content, re.DOTALL) |
| 51 | + if match: |
| 52 | + # Find the variable assignments within the class |
| 53 | + all_variables = re.findall(r"(\w+)\s*=\s*KernelAttribute", file_content) |
| 54 | + |
| 55 | + with open(file_path, "r") as file: |
| 56 | + try: |
| 57 | + tree = ast.parse(file.read()) |
| 58 | + except SyntaxError: |
| 59 | + # Skip files with syntax errors |
| 60 | + return None |
| 61 | + |
| 62 | + for node in ast.iter_child_nodes(tree): |
| 63 | + if isinstance(node, ast.Assign) and len(node.targets) == 1: |
| 64 | + target = node.targets[0] |
| 65 | + if isinstance(target, ast.Name) and target.id == "__all__": |
| 66 | + value = node.value |
| 67 | + if isinstance(value, ast.List): |
| 68 | + all_variables = [elem.s for elem in value.elts if isinstance(elem, ast.Str)] |
| 69 | + break |
| 70 | + |
| 71 | + return all_variables |
| 72 | + |
| 73 | + |
| 74 | +def process_directory(directory): |
| 75 | + """ |
| 76 | + Get the PyNEST API filenames and set the keys to the base name |
| 77 | + """ |
| 78 | + api_dict = {} |
| 79 | + api_exception_list = ["raster_plot", "visualization", "voltage_trace"] |
| 80 | + files = glob.glob(directory + "**/*.py", recursive=True) |
| 81 | + |
| 82 | + for file in files: |
| 83 | + # ignoring the low level api and connection_helpers and helper modules |
| 84 | + if "helper" in file or "ll_api" in file: |
| 85 | + continue |
| 86 | + |
| 87 | + # get the NestModule for the kernel attributes |
| 88 | + if "pynest/nest/__init__" in file: |
| 89 | + api_name = "nest.NestModule" |
| 90 | + |
| 91 | + parts = file.split(os.path.sep) |
| 92 | + nest_index = parts.index("nest") |
| 93 | + module_name = os.path.splitext(parts[-1])[0] |
| 94 | + # only get high level API modules |
| 95 | + if "hl_" in file: |
| 96 | + module_path = ".".join(parts[nest_index + 1 : -1]) |
| 97 | + api_name = f"nest.{module_path}.{module_name}" |
| 98 | + for item in api_exception_list: |
| 99 | + if item in file: |
| 100 | + api_name = f"nest.{module_name}" |
| 101 | + |
| 102 | + all_variables = find_all_variables(file) |
| 103 | + if all_variables: |
| 104 | + api_dict[api_name] = all_variables |
| 105 | + |
| 106 | + return api_dict |
| 107 | + |
| 108 | + |
| 109 | +def ExtractPyNESTAPIS(): |
| 110 | + directory = "../../pynest/nest/" |
| 111 | + all_variables_dict = process_directory(directory) |
| 112 | + |
| 113 | + with open("api_function_list.json", "w") as outfile: |
| 114 | + json.dump(all_variables_dict, outfile, indent=4) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + ExtractPyNESTAPIS() |
0 commit comments