Skip to content

Commit ca568e3

Browse files
twsltimvink
authored andcommitted
Added type hints to library, add config
1 parent 770ad9b commit ca568e3

File tree

9 files changed

+147
-116
lines changed

9 files changed

+147
-116
lines changed

mkdocs_git_authors_plugin/ci.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
Taken from https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/blob/master/mkdocs_git_revision_date_localized_plugin/ci.py
77
"""
88

9+
import logging
910
import os
1011
from contextlib import contextmanager
11-
import logging
1212
from pathlib import Path
13-
from typing import Union
13+
from typing import Any, Generator, Union
14+
1415
from mkdocs_git_authors_plugin.git.command import GitCommand
1516

1617

1718
@contextmanager
18-
def working_directory(path: Union[str, Path]):
19+
def working_directory(path: Union[str, Path]) -> Generator[None, Any, None]:
1920
"""
2021
Temporarily change working directory.
2122
A context manager which changes the working directory to the given
@@ -27,7 +28,7 @@ def working_directory(path: Union[str, Path]):
2728
# Do something in new directory
2829
# Back to old directory
2930
```
30-
"""
31+
"""
3132
origin = Path().absolute()
3233
try:
3334
os.chdir(path)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from mkdocs.config import config_options
2+
from mkdocs.config.base import Config
3+
4+
5+
class GitAuthorsPluginConfig(Config):
6+
show_contribution = config_options.Type(bool, default=False)
7+
show_line_count = config_options.Type(bool, default=False)
8+
show_email_address = config_options.Type(bool, default=True)
9+
href = config_options.Type(str, default="mailto:{email}")
10+
count_empty_lines = config_options.Type(bool, default=True)
11+
fallback_to_empty = config_options.Type(bool, default=False)
12+
exclude = config_options.Type(list, default=[])
13+
ignore_commits = config_options.Type(str, default="")
14+
ignore_authors = config_options.Type(list, default=[])
15+
enabled = config_options.Type(bool, default=True)
16+
enabled_on_serve = config_options.Type(bool, default=True)
17+
sort_authors_by = config_options.Type(str, default="name")
18+
authorship_threshold_percent = config_options.Type(int, default=0)
19+
strict = config_options.Type(bool, default=True)
20+
# sort_authors_by_name = config_options.Type(bool, default=True)
21+
# sort_reverse = config_options.Type(bool, default=False)

mkdocs_git_authors_plugin/git/author.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
from .repo import AbstractRepoObject, Repo
2-
from .page import Page
3-
from .commit import Commit
1+
from typing import Dict, Union
42

5-
from typing import Dict
3+
from mkdocs_git_authors_plugin.git.commit import Commit
4+
from mkdocs_git_authors_plugin.git.page import Page
5+
from mkdocs_git_authors_plugin.git.repo import AbstractRepoObject, Repo
66

77

88
class Author(AbstractRepoObject):
99
"""
1010
Abstraction of an author in the Git repository.
1111
"""
1212

13-
def __init__(self, repo: Repo, name: str, email: str):
13+
def __init__(self, repo: Repo, name: str, email: str) -> None:
1414
"""
1515
Instantiate an Author.
1616
@@ -24,7 +24,7 @@ def __init__(self, repo: Repo, name: str, email: str):
2424
self._email = email
2525
self._pages: Dict[str, dict] = dict()
2626

27-
def add_lines(self, page: Page, commit: Commit, lines: int = 1):
27+
def add_lines(self, page: Page, commit: Commit, lines: int = 1) -> None:
2828
"""
2929
Add line(s) in a given page/commit to the author's data.
3030
@@ -42,7 +42,7 @@ def add_lines(self, page: Page, commit: Commit, lines: int = 1):
4242
entry["datetime"] = commit_dt
4343
entry["datetime_str"] = commit.datetime(str)
4444

45-
def contribution(self, path=None, _type=float):
45+
def contribution(self, path=None, _type=float) -> Union[float, str]:
4646
"""
4747
The author's relative contribution to a page or the repository.
4848
@@ -60,17 +60,17 @@ def contribution(self, path=None, _type=float):
6060
formatted string or floating point number
6161
"""
6262
lines = self.lines(path)
63-
total_lines = (
63+
total_lines: int = (
6464
self.page(path)["page"].total_lines() if path else self.repo().total_lines()
6565
)
6666

6767
# Some pages are empty, that case contribution is 0 by default
6868
if total_lines == 0:
69-
result = 0
69+
result = 0.0
7070
else:
7171
result = lines / total_lines
7272

73-
if _type == float:
73+
if _type is float:
7474
return result
7575
else:
7676
return str(round(result * 100, 2)) + "%"
@@ -87,10 +87,10 @@ def datetime(self, path, fmt=str):
8787
a formatted string (fmt=str)
8888
or a datetime.datetime object with tzinfo
8989
"""
90-
key = "datetime_str" if fmt == str else "datetime"
90+
key = "datetime_str" if fmt is str else "datetime"
9191
return self.page(path).get(key)
9292

93-
def email(self):
93+
def email(self) -> str:
9494
"""
9595
The author's email address
9696
@@ -101,7 +101,7 @@ def email(self):
101101
"""
102102
return self._email
103103

104-
def lines(self, path=None):
104+
def lines(self, path=None) -> int:
105105
"""
106106
The author's total number of lines on a page or in the repository.
107107
@@ -117,7 +117,7 @@ def lines(self, path=None):
117117
else:
118118
return sum([v["lines"] for v in self._pages.values()])
119119

120-
def name(self):
120+
def name(self) -> str:
121121
"""
122122
The author's full name
123123
@@ -128,7 +128,7 @@ def name(self):
128128
"""
129129
return self._name
130130

131-
def page(self, path, page=None):
131+
def page(self, path, page=None) -> dict:
132132
"""
133133
A dictionary with the author's contribution to a page.
134134
@@ -154,7 +154,7 @@ def page(self, path, page=None):
154154
if not self._pages.get(path):
155155
self._pages[path] = {
156156
"page": page or self.repo().page(path),
157-
"lines": 0
157+
"lines": 0,
158158
# datetime and datetime_str will be populated later
159159
}
160160
return self._pages[path]

mkdocs_git_authors_plugin/git/command.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import subprocess
2+
from typing import List, Union
23

34

45
class GitCommandError(Exception):
@@ -24,7 +25,7 @@ class GitCommand(object):
2425
In case of an error a verbose GitCommandError is raised.
2526
"""
2627

27-
def __init__(self, command: str, args: list = []):
28+
def __init__(self, command: str, args: List = []) -> None:
2829
"""
2930
Initialize the GitCommand.
3031
@@ -40,7 +41,7 @@ def __init__(self, command: str, args: list = []):
4041
self._stderr = None
4142
self._completed = False
4243

43-
def run(self):
44+
def run(self) -> int:
4445
"""
4546
Execute the configured Git command.
4647
@@ -82,7 +83,7 @@ def run(self):
8283
self._completed = True
8384
return int(str(p.returncode))
8485

85-
def set_args(self, args: list):
86+
def set_args(self, args: List) -> None:
8687
"""
8788
Change the command arguments.
8889
@@ -91,7 +92,7 @@ def set_args(self, args: list):
9192
"""
9293
self._args = args
9394

94-
def set_command(self, command: str):
95+
def set_command(self, command: str) -> None:
9596
"""
9697
Change the Git command.
9798
@@ -100,7 +101,7 @@ def set_command(self, command: str):
100101
"""
101102
self._command = command
102103

103-
def stderr(self):
104+
def stderr(self) -> Union[None, List[str]]:
104105
"""
105106
Return the stderr output of the command as a string list.
106107
@@ -113,7 +114,7 @@ def stderr(self):
113114
raise GitCommandError("Trying to read from uncompleted GitCommand")
114115
return self._stderr
115116

116-
def stdout(self):
117+
def stdout(self) -> Union[None, List[str]]:
117118
"""
118119
Return the stdout output of the command as a string list.
119120

mkdocs_git_authors_plugin/git/commit.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
2-
from .repo import AbstractRepoObject, Repo
3-
from .. import util
2+
from typing import Union
3+
4+
from mkdocs_git_authors_plugin import util
5+
from mkdocs_git_authors_plugin.git.repo import AbstractRepoObject, Repo
46

57

68
class Commit(AbstractRepoObject):
@@ -58,7 +60,7 @@ def author(self):
5860
"""
5961
return self._author
6062

61-
def datetime(self, _type=str):
63+
def datetime(self, _type=str) -> Union[str, util.datetime]:
6264
"""
6365
The commit's commit time.
6466
@@ -71,4 +73,4 @@ def datetime(self, _type=str):
7173
The commit's commit time, either as a formatted string (_type=str)
7274
or as a datetime.datetime expression with tzinfo
7375
"""
74-
return self._datetime_string if _type == str else self._datetime
76+
return self._datetime_string if _type is str else self._datetime

mkdocs_git_authors_plugin/git/page.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from pathlib import Path
2-
import re
31
import logging
4-
from .repo import Repo, AbstractRepoObject
5-
from .command import GitCommand, GitCommandError
6-
2+
import re
3+
from pathlib import Path
74
from typing import List
85

6+
from mkdocs_git_authors_plugin.git.command import GitCommand, GitCommandError
7+
from mkdocs_git_authors_plugin.git.repo import AbstractRepoObject, Repo
8+
99
logger = logging.getLogger("mkdocs.plugins")
1010

1111

@@ -18,7 +18,7 @@ class Page(AbstractRepoObject):
1818
modified by that commit.
1919
"""
2020

21-
def __init__(self, repo: Repo, path: Path, strict: bool):
21+
def __init__(self, repo: Repo, path: Path, strict: bool) -> None:
2222
"""
2323
Instantiate a Page object
2424
@@ -32,7 +32,7 @@ def __init__(self, repo: Repo, path: Path, strict: bool):
3232
self._total_lines = 0
3333
self._authors: List[dict] = list()
3434
self._strict = strict
35-
35+
3636
try:
3737
self._process_git_blame()
3838
except GitCommandError:
@@ -47,7 +47,7 @@ def __init__(self, repo: Repo, path: Path, strict: bool):
4747
% path
4848
)
4949

50-
def add_total_lines(self, cnt: int = 1):
50+
def add_total_lines(self, cnt: int = 1) -> None:
5151
"""
5252
Add line(s) to the count of total lines for the page.
5353
@@ -56,7 +56,7 @@ def add_total_lines(self, cnt: int = 1):
5656
"""
5757
self._total_lines += cnt
5858

59-
def get_authors(self):
59+
def get_authors(self) -> List[dict]:
6060
"""
6161
Return a sorted list of authors for the page
6262
@@ -86,7 +86,7 @@ def get_authors(self):
8686
]
8787
return self._authors
8888

89-
def _process_git_blame(self):
89+
def _process_git_blame(self) -> None:
9090
"""
9191
Execute git blame and parse the results.
9292
@@ -190,15 +190,17 @@ def _process_git_blame(self):
190190
author_tz=commit_data.get("author-tz"),
191191
summary=commit_data.get("summary"),
192192
)
193-
if commit.author().email() not in ignore_authors and (len(line) > 1 or self.repo().config("count_empty_lines")):
193+
if commit.author().email() not in ignore_authors and (
194+
len(line) > 1 or self.repo().config("count_empty_lines")
195+
):
194196
author = commit.author()
195197
if author not in self._authors:
196198
self._authors.append(author)
197199
author.add_lines(self, commit)
198200
self.add_total_lines()
199201
self.repo().add_total_lines()
200202

201-
def path(self):
203+
def path(self) -> Path:
202204
"""
203205
The path to the markdown file.
204206
@@ -209,7 +211,7 @@ def path(self):
209211
"""
210212
return self._path
211213

212-
def total_lines(self):
214+
def total_lines(self) -> int:
213215
"""
214216
Total number of lines in the markdown source file.
215217

0 commit comments

Comments
 (0)