-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_authors.py
47 lines (37 loc) · 1.77 KB
/
get_authors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def get_authors(docstring: str) -> list[dict[str | None, str | None]] | None:
"""
Goes through the docstring and looks for authors annotated by the `@author` tag. You can add as many of these
tags as needed.
If no authors are found, it returns `None`. Otherwise, it returns a list of dictionaries with keys
`name` and `email`. Either author name or email may be left blank, which will return that key as `None`.
The email value is everything between a set of brackets.
For example:
- `@author John Doe`
- Returns: `{"name": "John Doe", "email": None}`
- `@author John Doe [John.Doe@myemail.com]`
- Returns: `{"name": "John Doe", "email": "john.doe@somedomain.com"}`
- `@author [john.doe@somedomain.com]`
- Returns: `{"name": None, "email": "john.doe@somedomain.com"}`
"""
parsed = docstring.splitlines()
authors = []
author_name = ""
author_email = ""
for line in parsed:
stripped_line = line.strip()
if stripped_line[0:8] == "@author ":
# We have encountered a new author, start recording the info
email_start = stripped_line.find("[", 0)
email_end = stripped_line.find("]", 0)
if email_start > 0 and email_end > 0:
author_name = stripped_line[8:email_start - 1].strip()
author_email = stripped_line[email_start + 1:email_end].strip()
else:
author_name = stripped_line[8:len(stripped_line)].strip()
author_email = None
if author_name == "":
author_name = None
authors.append({"name": author_name, "email": author_email})
if len(authors) > 0:
return authors
return None