Skip to content

Commit d0d55e3

Browse files
authored
solved issue pandas-dev#58848 by adding docstrings in announce.py and contributors.py
1 parent a564662 commit d0d55e3

File tree

2 files changed

+232
-2
lines changed

2 files changed

+232
-2
lines changed

doc/sphinxext/announce.py

+149-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@
5757

5858

5959
def get_authors(revision_range):
60+
"""
61+
Extracts a list of code authors within a specified revision range.
62+
63+
This function analyzes the git log for the given revision range and identifies
64+
all the contributors who authored or co-authored commits. It considers both
65+
regular commits and "Co-authored-by" commits used for backports.
66+
67+
Parameters
68+
----------
69+
revision_range : str
70+
A string representing the revision range for analysis. The Expected format is: "<start-revision>..<end-revision>".
71+
72+
Returns
73+
-------
74+
list
75+
A alphabetically sorted list of author names (strings) who contributed within the revision range. The new authors are marked with a '+'.
76+
77+
Examples
78+
--------
79+
>>> get_authors("v1.0.0..v1.0.1")
80+
['Author1', 'Author2', 'Author3', 'Author4']
81+
82+
>>> get_authors('v1.0.0..HEAD')
83+
['Author1', 'Author2', 'Author3', 'Author4', 'Author5 +']
84+
"""
6085
pat = "^.*\\t(.*)$"
6186
lst_release, cur_release = (r.strip() for r in revision_range.split(".."))
6287

@@ -109,6 +134,39 @@ def get_authors(revision_range):
109134

110135

111136
def get_pull_requests(repo, revision_range):
137+
"""
138+
Retrieve pull requests from a Git repository within a specified revision range.
139+
140+
This function extracts pull request numbers from various types of merge commits (regular merges, Homu auto merges, and fast-forward squash-merges) in a specified revision range of a repository. It then retrieves detailed pull request data from the GitHub repository based on these numbers.
141+
142+
Parameters
143+
----------
144+
repo : Repository object
145+
The GitHub repository object from which to retrieve pull request data.
146+
revision_range : str
147+
The range of revisions to search for pull request merges, specified in a
148+
format recognized by `git log`.
149+
150+
Returns
151+
-------
152+
list
153+
A sorted list of pull request objects corresponding to the pull request numbers found in the specified revision range.
154+
155+
See Also
156+
--------
157+
Repository.get_pull : Retrieve a pull request by number from the repository.
158+
159+
Examples
160+
--------
161+
>>> prs = get_pull_requests(repo, "v1.0.0...v2.0.0")
162+
>>> prs
163+
[<Pull Request 24>, <Pull Request 25>, <Pull Request 26>]
164+
>>> for pr in prs:
165+
... print(pr.number, pr.title)
166+
1 Fix bug in feature X
167+
2 Improve documentation
168+
3 Add new feature Y
169+
"""
112170
prnums = []
113171

114172
# From regular merges
@@ -134,6 +192,37 @@ def get_pull_requests(repo, revision_range):
134192

135193

136194
def build_components(revision_range, heading="Contributors"):
195+
"""
196+
Build the components for a contributors section based on a revision range.
197+
198+
This function extracts the list of authors who contributed within a specified revision range and constructs the components needed for a contributors section, including a heading and an author message.
199+
200+
Parameters
201+
----------
202+
revision_range : str
203+
The range of revisions to search for authors, specified in a format recognized by `git log`.
204+
heading : str, optional
205+
The heading for the contributors section, default is "Contributors".
206+
207+
Returns
208+
-------
209+
dict
210+
A dictionary containing the heading, author message, and list of authors who contributed within the specified revision range.
211+
212+
See Also
213+
--------
214+
get_authors : Retrieve a list of authors who contributed within a specified revision range.
215+
216+
Examples
217+
--------
218+
>>> components = build_components("v1.0.0...v2.0.0")
219+
>>> components["heading"]
220+
Contributors
221+
>>> components["author_message"]
222+
There are 10 contributors.
223+
>>> components["authors"]
224+
['Author1', 'Author2', 'Author3', ...]
225+
"""
137226
lst_release, cur_release = (r.strip() for r in revision_range.split(".."))
138227
authors = get_authors(revision_range)
139228

@@ -145,6 +234,40 @@ def build_components(revision_range, heading="Contributors"):
145234

146235

147236
def build_string(revision_range, heading="Contributors"):
237+
"""
238+
Build a formatted string for the contributors section based on a revision range.
239+
240+
This function creates a formatted string that includes a heading, an author message, and a list of authors who contributed within a specified revision range. The formatting is designed to be suitable for inclusion in documentation.
241+
242+
Parameters
243+
----------
244+
revision_range : str
245+
The range of revisions to search for authors, specified in a format recognized by `git log`.
246+
heading : str, optional
247+
The heading for the contributors section, default is "Contributors".
248+
249+
Returns
250+
-------
251+
str
252+
A formatted string containing the contributors section.
253+
254+
See Also
255+
--------
256+
build_components : Build the components for a contributors section.
257+
258+
Examples
259+
--------
260+
>>> contrib_string = build_string("v1.0.0...v2.0.0")
261+
>>> contrib_string
262+
Contributors
263+
============
264+
265+
There are 10 contributors.
266+
* Author1
267+
* Author2
268+
* Author3
269+
...
270+
"""
148271
components = build_components(revision_range, heading=heading)
149272
components["uline"] = "=" * len(components["heading"])
150273
components["authors"] = "* " + "\n* ".join(components["authors"])
@@ -162,6 +285,31 @@ def build_string(revision_range, heading="Contributors"):
162285

163286

164287
def main(revision_range):
288+
"""
289+
Main function to document authors based on a revision range.
290+
291+
This function generates and prints a formatted contributors section for a specified
292+
revision range. It serves as the entry point for generating the contributors section
293+
output.
294+
295+
Parameters
296+
----------
297+
revision_range : str
298+
The range of revisions to search for authors, specified in a format recognized
299+
by `git log`.
300+
301+
Examples
302+
--------
303+
>>> main("v1.0.0...v2.0.0")
304+
Contributors
305+
============
306+
307+
There are 10 contributors.
308+
* Author1
309+
* Author2
310+
* Author3
311+
...
312+
"""
165313
# document authors
166314
text = build_string(revision_range)
167315
print(text)
@@ -173,4 +321,4 @@ def main(revision_range):
173321
parser = ArgumentParser(description="Generate author lists for release")
174322
parser.add_argument("revision_range", help="<revision>..<revision>")
175323
args = parser.parse_args()
176-
main(args.revision_range)
324+
main(args.revision_range)

doc/sphinxext/contributors.py

+83-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,67 @@
2222

2323

2424
class ContributorsDirective(Directive):
25+
"""
26+
A custom Sphinx directive to generate a contributors section for a specified revision range.
27+
28+
This directive processes a given revision range to build and display a list of contributors
29+
in a Sphinx document. If the revision range ends with 'x..HEAD', an empty paragraph and
30+
bullet list are returned. If an error occurs while fetching the contributors, a warning is
31+
issued.
32+
33+
Parameters
34+
----------
35+
required_arguments : int
36+
Number of required arguments for the directive, set to 1.
37+
name : str
38+
The name of the directive, set to "contributors".
39+
40+
Returns
41+
-------
42+
list
43+
A list of Sphinx nodes containing the contributors section.
44+
45+
Examples
46+
--------
47+
.. contributors:: v1.0.0..v2.0.0
48+
49+
This directive can be used in Sphinx documentation to automatically generate and include a
50+
contributors section based on a specified revision range.
51+
"""
2552
required_arguments = 1
2653
name = "contributors"
2754

2855
def run(self):
56+
"""
57+
Process the directive and generate the contributors section.
58+
59+
This method retrieves the specified revision range from the directive's arguments.
60+
If the range ends with 'x..HEAD', it returns an empty paragraph and bullet list.
61+
Otherwise, it attempts to build the components for the contributors section using
62+
the `build_components` function. If an error occurs during this process, it issues
63+
a warning. On success, it creates a paragraph node with the author message and a
64+
bullet list node with the list of authors.
65+
66+
Returns
67+
-------
68+
list
69+
A list of Sphinx nodes representing the contributors section. This includes
70+
a paragraph node for the author message and a bullet list node with the authors.
71+
72+
Raises
73+
------
74+
git.GitCommandError
75+
If an error occurs while retrieving the contributors, a warning node is returned
76+
with the error message.
77+
78+
Examples
79+
--------
80+
This method is called automatically by the Sphinx framework when processing the
81+
`contributors` directive. For example, in a Sphinx document:
82+
83+
.. contributors:: v1.0.0..v2.0.
84+
"""
85+
2986
range_ = self.arguments[0]
3087
if range_.endswith("x..HEAD"):
3188
return [nodes.paragraph(), nodes.bullet_list()]
@@ -53,6 +110,31 @@ def run(self):
53110

54111

55112
def setup(app):
113+
"""
114+
Setup function to register the ContributorsDirective with a Sphinx application.
115+
116+
This function adds the 'contributors' directive to the Sphinx application, enabling the
117+
use of the custom directive to generate contributors sections in the documentation.
118+
119+
Parameters
120+
----------
121+
app : Sphinx
122+
The Sphinx application object.
123+
124+
Returns
125+
-------
126+
dict
127+
A dictionary with metadata about the extension, indicating version and parallel
128+
read/write safety.
129+
130+
Examples
131+
--------
132+
In your Sphinx `conf.py` file, include the following line to register the extension:
133+
134+
>>> def setup(app):
135+
>>> app.add_directive("contributors", ContributorsDirective)
136+
137+
"""
56138
app.add_directive("contributors", ContributorsDirective)
57139

58-
return {"version": "0.1", "parallel_read_safe": True, "parallel_write_safe": True}
140+
return {"version": "0.1", "parallel_read_safe": True, "parallel_write_safe": True}

0 commit comments

Comments
 (0)