| 1 |
import collections |
| 2 |
from docutils import nodes |
| 3 |
from pathlib import Path |
| 4 |
from typing import TYPE_CHECKING |
| 5 |
|
| 6 |
from sphinx.application import Sphinx |
| 7 |
from sphinx.util.docutils import SphinxDirective |
| 8 |
|
| 9 |
if TYPE_CHECKING: |
| 10 |
from sphinx.util.typing import ExtensionMetadata |
| 11 |
|
| 12 |
|
| 13 |
class FlashromAuthorsDirective(SphinxDirective): |
| 14 |
required_arguments = 1 |
| 15 |
has_content = True |
| 16 |
|
| 17 |
def make_table(self, name_counts): |
| 18 |
body = nodes.tbody() |
| 19 |
for name, count in name_counts: |
| 20 |
body += nodes.row( |
| 21 |
"", |
| 22 |
nodes.entry("", nodes.paragraph(text=name)), |
| 23 |
nodes.entry("", nodes.paragraph(text=str(count))), |
| 24 |
) |
| 25 |
|
| 26 |
return nodes.table( |
| 27 |
"", |
| 28 |
nodes.tgroup( |
| 29 |
"", |
| 30 |
nodes.colspec(colname="name"), |
| 31 |
nodes.colspec(colname="count"), |
| 32 |
nodes.thead( |
| 33 |
"", |
| 34 |
nodes.row( |
| 35 |
"", |
| 36 |
nodes.entry("", nodes.paragraph(text="Name")), |
| 37 |
nodes.entry("", nodes.paragraph(text="Number of changes")), |
| 38 |
), |
| 39 |
), |
| 40 |
body, |
| 41 |
cols=2, |
| 42 |
), |
| 43 |
) |
| 44 |
|
| 45 |
def make_placeholder(self, contents): |
| 46 |
return nodes.admonition( |
| 47 |
"", |
| 48 |
nodes.title("", text="List not available"), |
| 49 |
*contents, |
| 50 |
) |
| 51 |
|
| 52 |
def run(self) -> list[nodes.Node]: |
| 53 |
config = self.config.flashrom_authors_list_files |
| 54 |
|
| 55 |
source_name = self.arguments[0] |
| 56 |
list_file = (config or {}).get(source_name) |
| 57 |
if list_file is None: |
| 58 |
if config is not None: |
| 59 |
available_names = ','.join(config.keys()) |
| 60 |
raise self.error( |
| 61 |
'Undefined authors list file: "{}" (available names: {})'.format( |
| 62 |
source_name, available_names |
| 63 |
) |
| 64 |
) |
| 65 |
container = nodes.Element() |
| 66 |
self.state.nested_parse(self.content, 0, container) |
| 67 |
return [self.make_placeholder(container.children)] |
| 68 |
else: |
| 69 |
authors = self.count_authors_from_file(Path(list_file)) |
| 70 |
return [self.make_table(authors.most_common())] |
| 71 |
|
| 72 |
@classmethod |
| 73 |
def count_authors_from_file(cls, list_file: Path) -> collections.Counter: |
| 74 |
# Correcting names may influence the count, so although git shortlog |
| 75 |
# provides us a count there may be multiple entries in the file that |
| 76 |
# must be combined into a single row. |
| 77 |
counter = collections.Counter() |
| 78 |
|
| 79 |
with list_file.open("r") as f: |
| 80 |
for line in f: |
| 81 |
count, _, name = line.strip().partition("\t") |
| 82 |
fixed_name = cls._correct_known_bad_name(name) |
| 83 |
if fixed_name is not None: |
| 84 |
counter[fixed_name] += int(count) |
| 85 |
|
| 86 |
return counter |
| 87 |
|
| 88 |
_INCORRECT_NAMES = { |
| 89 |
# Commit 9ff3d4cf759161edf6c163f454f11bc9f5328ce3 is missing a '>' in a |
| 90 |
# Co-Authored-By trailer, but this person is already credited as author |
| 91 |
# of that commit so should not be double-counted. |
| 92 |
"persmule <[email protected]": None, |
| 93 |
} |
| 94 |
|
| 95 |
@classmethod |
| 96 |
def _correct_known_bad_name(cls, name: str): |
| 97 |
""" |
| 98 |
Return a corrected form of the provided name if it's known to be |
| 99 |
incorrect and should be modified, or None if the name should be |
| 100 |
ignored. Otherwise, return the provided name. |
| 101 |
""" |
| 102 |
return cls._INCORRECT_NAMES.get(name, name) |
| 103 |
|
| 104 |
|
| 105 |
def setup(app: Sphinx) -> 'ExtensionMetadata': |
| 106 |
app.add_config_value( |
| 107 |
"flashrom_authors_list_files", default=None, rebuild="html", types=[dict] |
| 108 |
) |
| 109 |
app.add_directive("flashrom-authors", FlashromAuthorsDirective) |
| 110 |
|
| 111 |
return { |
| 112 |
"version": "1", |
| 113 |
} |