1- from os import listdir
2-
3- # This script is used to create a RST file for all Python examples
4- # Content of the file 'PythonExamples.rst' will be overwritten
5- # The script will create a title from the file name, and add a reference to the source code using literalinclude
6-
7- path_examples = "../rips/PythonExamples"
8- file_names = sorted (listdir (path_examples ))
9-
10- txt = ""
11- txt += ".. \n This file was created using the script in docs/source/create_python_examples.py\n \n "
12- txt += "Python Examples\n "
13- txt += "---------------\n \n "
14- txt += "This pages is created based on the content in the **PythonExamples** folder located inside the **rips** module, made available online for convenience.\n \n "
15-
16- for file_name in file_names :
17- reference = file_name .replace (".py" , "" )
18- heading = reference .replace ("_" , " " ).title ()
19- txt += ".. _" + reference + ":\n \n "
20- txt += heading
21- txt += "\n "
22- txt += "=============================================\n "
23- txt += ".. literalinclude:: ../rips/PythonExamples/"
24- txt += file_name
25- txt += "\n \n "
26-
27- #print (txt)
28- f = open ("PythonExamples.rst" , "w" )
29- f .write (txt )
30-
31- print ("Completed export of PythonExamples.rst" )
1+ import os
2+ from pathlib import Path
3+
4+ # This script is used to create RST files for all Python examples
5+ # It will create individual RST files for each subfolder in PythonExamples
6+ # and a main index file that references all subfolders
7+
8+ path_examples = Path ("../rips/PythonExamples" )
9+
10+ def create_rst_for_folder (folder_path , relative_to_examples ):
11+ """Create an RST file for a specific folder containing Python examples."""
12+
13+ # Get all Python files in this folder
14+ python_files = sorted ([f for f in folder_path .iterdir ()
15+ if f .is_file () and f .suffix == '.py' ])
16+
17+ if not python_files :
18+ return None
19+
20+ folder_name = folder_path .name
21+ rst_filename = f"PythonExamples_{ folder_name } .rst"
22+
23+ # Create heading from folder name
24+ heading = folder_name .replace ("_" , " " ).title ()
25+
26+ txt = ""
27+ txt += ".. \n This file was created using the script in docs/source/create_python_examples.py\n \n "
28+ txt += f"{ heading } \n "
29+ txt += "=" * len (heading ) + "\n \n "
30+ txt += f"This page shows Python examples from the **{ folder_name } ** folder.\n \n "
31+
32+ for py_file in python_files :
33+ reference = py_file .stem # filename without extension
34+ example_heading = reference .replace ("_" , " " ).title ()
35+
36+ txt += f".. _{ folder_name } _{ reference } :\n \n "
37+ txt += example_heading + "\n "
38+ txt += "-" * len (example_heading ) + "\n \n "
39+
40+ # Build path relative to the source directory
41+ relative_path = path_examples / relative_to_examples / py_file .name
42+ txt += f".. literalinclude:: { relative_path } \n "
43+ txt += " :language: python\n "
44+ txt += " :linenos:\n "
45+ txt += " :caption: " + py_file .name + "\n \n "
46+
47+ # Add individual download link
48+ txt += f"📄 `Download { py_file .name } <https://raw.githubusercontent.com/OPM/ResInsight-UserDocumentation/main/docs/rips/PythonExamples/{ folder_name } /{ py_file .name } >`_\n \n "
49+
50+ # Write the RST file
51+ with open (rst_filename , "w" ) as f :
52+ f .write (txt )
53+
54+ print (f"Created { rst_filename } " )
55+ return rst_filename , heading
56+
57+ def create_general_examples_page ():
58+ """Create a separate page for root-level examples."""
59+
60+ root_files = sorted ([f for f in path_examples .iterdir ()
61+ if f .is_file () and f .suffix == '.py' ])
62+
63+ if not root_files :
64+ return None
65+
66+ txt = ""
67+ txt += ".. \n This file was created using the script in docs/source/create_python_examples.py\n \n "
68+ txt += "General Examples\n "
69+ txt += "================\n \n "
70+ txt += "This page shows Python examples from the root directory.\n \n "
71+
72+ for py_file in root_files :
73+ reference = py_file .stem
74+ example_heading = reference .replace ("_" , " " ).title ()
75+
76+ txt += f".. _general_{ reference } :\n \n "
77+ txt += example_heading + "\n "
78+ txt += "-" * len (example_heading ) + "\n \n "
79+
80+ relative_path = path_examples / py_file .name
81+ txt += f".. literalinclude:: { relative_path } \n "
82+ txt += " :language: python\n "
83+ txt += " :linenos:\n "
84+ txt += " :caption: " + py_file .name + "\n \n "
85+
86+ # Write the general examples file
87+ with open ("PythonExamples_General.rst" , "w" ) as f :
88+ f .write (txt )
89+
90+ print ("Created PythonExamples_General.rst" )
91+ return "PythonExamples_General.rst" , "General Examples"
92+
93+ def create_main_index ():
94+ """Create the main PythonExamples.rst index file."""
95+
96+ # Get all subdirectories
97+ subdirs = sorted ([d for d in path_examples .iterdir ()
98+ if d .is_dir () and not d .name .startswith ('.' )])
99+
100+ # Also handle files in the root directory
101+ root_files = sorted ([f for f in path_examples .iterdir ()
102+ if f .is_file () and f .suffix == '.py' ])
103+
104+ txt = ""
105+ txt += ".. \n This file was created using the script in docs/source/create_python_examples.py\n \n "
106+ txt += "Python Examples\n "
107+ txt += "===============\n \n "
108+ txt += "This page provides access to all Python examples organized by category.\n \n "
109+
110+
111+ # Create toctree for subdirectories and root files
112+ has_content = bool (subdirs ) or bool (root_files )
113+
114+ if has_content :
115+ txt += ".. toctree::\n "
116+ txt += " :maxdepth: 2\n "
117+ txt += " :caption: Example Categories:\n \n "
118+
119+ # Add subdirectories to toctree
120+ for subdir in subdirs :
121+ rst_file = f"PythonExamples_{ subdir .name } "
122+ txt += f" { rst_file } \n "
123+
124+ # If there are root files, add general examples page
125+ if root_files :
126+ txt += " PythonExamples_General\n "
127+
128+ txt += "\n "
129+
130+ # Write the main index file
131+ with open ("PythonExamples.rst" , "w" ) as f :
132+ f .write (txt )
133+
134+ print ("Created PythonExamples.rst (main index)" )
135+
136+ def main ():
137+ """Main function to process all folders and create RST files."""
138+
139+ if not path_examples .exists ():
140+ print (f"Error: Examples directory not found: { path_examples } " )
141+ return
142+
143+ print (f"Processing examples from: { path_examples } " )
144+
145+ # Get all subdirectories
146+ subdirs = [d for d in path_examples .iterdir ()
147+ if d .is_dir () and not d .name .startswith ('.' )]
148+
149+ created_files = []
150+
151+ # Process each subdirectory
152+ for subdir in subdirs :
153+ relative_path = subdir .name
154+ result = create_rst_for_folder (subdir , relative_path )
155+ if result :
156+ created_files .append (result )
157+
158+ # Create a separate page for root-level files
159+ general_result = create_general_examples_page ()
160+ if general_result :
161+ created_files .append (general_result )
162+
163+ # Create the main index file
164+ create_main_index ()
165+
166+ print (f"\n Completed! Created { len (created_files )} category files plus main index." )
167+ print ("Created files:" )
168+ print ("- PythonExamples.rst (main index)" )
169+ for filename , heading in created_files :
170+ print (f"- { filename } ({ heading } )" )
171+
172+ print ("\n Download links added:" )
173+ print ("- Individual file downloads (raw GitHub links)" )
174+ print ("- Folder browsing links (GitHub tree view)" )
175+ print ("- Bulk download (ZIP archive)" )
176+
177+ if __name__ == "__main__" :
178+ main ()
0 commit comments