@@ -92,14 +92,50 @@ def follow_relative_links(path: str) -> str:
92
92
path = os .path .join (os .path .dirname (path ), link )
93
93
94
94
95
+ def has_broken_docs (plugin_record : t .Mapping [str , t .Any ], plugin_type : str ) -> bool :
96
+ """
97
+ Determine whether the plugin record is completely broken or not.
98
+ """
99
+ expected_fields = ('entry_points' ,) if plugin_type == 'role' else ('doc' , 'examples' , 'return' )
100
+ return not plugin_record or not all (field in plugin_record for field in expected_fields )
101
+
102
+
103
+ def guess_relative_filename (plugin_record : t .Mapping [str , t .Any ],
104
+ plugin_short_name : str ,
105
+ plugin_type : str ,
106
+ collection_name : str ,
107
+ collection_meta : AnsibleCollectionMetadata ) -> str :
108
+ """
109
+ Make an educated guess on the documentation source file.
110
+ """
111
+ if plugin_record and plugin_record .get ('doc' ) and plugin_record ['doc' ].get ('filename' ):
112
+ filename = follow_relative_links (plugin_record ['doc' ]['filename' ])
113
+ return os .path .relpath (filename , collection_meta .path )
114
+ if plugin_type == 'role' :
115
+ return f"roles/{ plugin_short_name } /meta/argument_specs.yml"
116
+ plugin_dir = (
117
+ # Modules in ansible-core:
118
+ 'modules' if plugin_type == 'module' and collection_name == 'ansible.builtin' else
119
+ # Modules in collections:
120
+ 'plugins/modules' if plugin_type == 'module' else
121
+ # Plugins in ansible-core or collections:
122
+ 'plugins/' + plugin_type
123
+ )
124
+ # Guess path inside collection tree
125
+ return f"{ plugin_dir } /{ plugin_short_name } .py"
126
+
127
+
95
128
def create_plugin_rst (collection_name : str ,
96
129
collection_meta : AnsibleCollectionMetadata ,
97
130
collection_links : CollectionLinks ,
98
- plugin_short_name : str , plugin_type : str ,
99
- plugin_record : t .Dict [str , t .Any ], nonfatal_errors : t .Sequence [str ],
131
+ plugin_short_name : str ,
132
+ plugin_type : str ,
133
+ plugin_record : t .Dict [str , t .Any ],
134
+ nonfatal_errors : t .Sequence [str ],
100
135
plugin_tmpl : Template , error_tmpl : Template ,
101
136
use_html_blobs : bool = False ,
102
- for_official_docsite : bool = False ) -> str :
137
+ for_official_docsite : bool = False ,
138
+ log_errors : bool = True ) -> str :
103
139
"""
104
140
Create the rst page for one plugin.
105
141
@@ -118,6 +154,7 @@ def create_plugin_rst(collection_name: str,
118
154
tables instead of using RST tables.
119
155
:kwarg for_official_docsite: Default False. Set to True to use wording specific for the
120
156
official docsite on docs.ansible.com.
157
+ :kwarg log_errors: Default True. Set to False to avoid errors to be logged.
121
158
"""
122
159
flog = mlog .fields (func = 'create_plugin_rst' )
123
160
flog .debug ('Enter' )
@@ -126,38 +163,21 @@ def create_plugin_rst(collection_name: str,
126
163
127
164
edit_on_github_url = None
128
165
eog = collection_links .edit_on_github
129
- if eog and plugin_type != 'role' :
130
- gh_plugin_dir = (
131
- # Modules in ansible-core:
132
- 'modules' if plugin_type == 'module' and collection_name == 'ansible.builtin' else
133
- # Modules in collections:
134
- 'plugins/modules' if plugin_type == 'module' else
135
- # Plugins in ansible-core or collections:
136
- 'plugins/' + plugin_type
137
- )
138
- # Guess path inside collection tree
139
- gh_path = f"{ gh_plugin_dir } /{ plugin_short_name } .py"
140
- # If we have more precise information, use that!
141
- if plugin_record and plugin_record .get ('doc' ) and plugin_record ['doc' ].get ('filename' ):
142
- filename = follow_relative_links (plugin_record ['doc' ]['filename' ])
143
- gh_path = os .path .relpath (filename , collection_meta .path )
144
- # Compose path
166
+ if eog :
167
+ # Compose Edit on GitHub URL
168
+ gh_path = guess_relative_filename (
169
+ plugin_record , plugin_short_name , plugin_type , collection_name , collection_meta )
145
170
edit_on_github_url = (
146
171
f"https://github.com/{ eog .repository } /edit/{ eog .branch } /{ eog .path_prefix } { gh_path } "
147
172
)
148
- if eog and plugin_type == 'role' :
149
- edit_on_github_url = (
150
- f"https://github.com/{ eog .repository } /edit/{ eog .branch } /{ eog .path_prefix } "
151
- f"roles/{ plugin_short_name } /meta/argument_specs.yml"
152
- )
153
173
154
- expected_fields = ( 'entry_points' ,) if plugin_type == 'role' else ( 'doc' , 'examples' , 'return' )
155
- if not plugin_record or not all ( field in plugin_record for field in expected_fields ) :
156
- flog .fields (plugin_type = plugin_type ,
157
- plugin_name = plugin_name ,
158
- nonfatal_errors = nonfatal_errors
159
- ).error ('{plugin_name} did not return correct DOCUMENTATION. An error page '
160
- ' will be generated.' , plugin_name = plugin_name )
174
+ if has_broken_docs ( plugin_record , plugin_type ):
175
+ if log_errors :
176
+ flog .fields (plugin_type = plugin_type ,
177
+ plugin_name = plugin_name ,
178
+ nonfatal_errors = nonfatal_errors
179
+ ).error ('{plugin_name} did not return correct DOCUMENTATION. An error'
180
+ ' page will be generated.' , plugin_name = plugin_name )
161
181
plugin_contents = _render_template (
162
182
error_tmpl ,
163
183
plugin_name + '_' + plugin_type ,
@@ -172,7 +192,7 @@ def create_plugin_rst(collection_name: str,
172
192
for_official_docsite = for_official_docsite ,
173
193
)
174
194
else :
175
- if nonfatal_errors :
195
+ if log_errors and nonfatal_errors :
176
196
flog .fields (plugin_type = plugin_type ,
177
197
plugin_name = plugin_name ,
178
198
nonfatal_errors = nonfatal_errors
0 commit comments