Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
extract_docstrings.py
Go to the documentation of this file.
1#-------------------------------------------------------------------------------
2# Author: Enric Tejedor <enric.tejedor.saavedra@cern.ch> CERN
3#-------------------------------------------------------------------------------
4
5################################################################################
6# Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. #
7# All rights reserved. #
8# #
9# For the licensing terms see $ROOTSYS/LICENSE. #
10# For the list of contributors see $ROOTSYS/README/CREDITS. #
11################################################################################
12
13# Code that extracts the docstrings from pythonization files and stores them
14# in .pyzdoc files, so that doxygen can process them later to merge the
15# documentation they contain with that of C++ files
16
17import ast
18import sys
19from os import path
20
21# Inspired from https://pypi.org/project/scandir/:
22# Use the built-in version of scandir/walk if possible, otherwise
23# use the scandir module
24try:
25 from os import scandir
26except ImportError:
27 from scandir import scandir
28
29if len(sys.argv) < 3:
30 print("Please provide the directory where documented .py files are, and the output folder for the .pyzdoc files.")
31 exit(1)
32
33pyz_dir = sys.argv[1]
34pyz_dir_out = sys.argv[2]
35
36def run_fast_scandir(dir, ext): # dir: str, ext: list
37 subfolders, files = [], []
38
39 for f in scandir(dir):
40 if f.is_dir():
41 subfolders.append(f.path)
42 if f.is_file():
43 if path.splitext(f.name)[1].lower() in ext:
44 files.append(f.path)
45
46
47 for dir in list(subfolders):
48 sf, f = run_fast_scandir(dir, ext)
49 subfolders.extend(sf)
50 files.extend(f)
51 return subfolders, files
52
53
54_, filenames = run_fast_scandir(pyz_dir, [".py"])
55
56
57# Iterate over pythonization files
58for pyz_file_path in filenames:
59
60 with open(pyz_file_path) as fd:
61 file_contents = fd.read()
62
63 # Docs for pythonizations are provided as a module-level docstring
64 module = ast.parse(file_contents)
65 ds = ast.get_docstring(module)
66 if ds is not None:
67 # The output file will sit in the $(DOXYGEN_PYZDOC_PATH)/pyzdoc folder. The output
68 # file name is build from the input file name which is extracted from pyz_file_path.
69 # The extracted file name has the extension ".py". We want the ouput file name has the
70 # extension ".pyzdoc". Therefore it is enough to concatenate the string "zdoc" at the
71 # end of the input file name to get the desired extension for the output file name.
72 pyz_filename_out = pyz_dir_out + '/' + pyz_file_path[pyz_file_path.rfind("_"):] + 'zdoc'
73 with open(pyz_filename_out, 'w') as pyz_doc_file:
74 pyz_doc_file.write(ds)
75