1
- import os
2
- import sys
1
+ import json
3
2
import logging
4
- from pprint import pprint
3
+ import os
4
+ import re
5
+ import time
6
+ from datetime import datetime
5
7
from timeit import default_timer as timer
6
- from datetime import datetime , timedelta
7
8
8
- from mkdocs import utils as mkdocs_utils
9
- from mkdocs .config import config_options , Config
9
+ import requests
10
+ from bs4 import BeautifulSoup as bs
11
+ from git import Commit , Repo
12
+ from mkdocs .config import config_options
10
13
from mkdocs .plugins import BasePlugin
11
-
12
- from git import Repo , Commit
13
- import requests , json
14
14
from requests .exceptions import HTTPError
15
- import time
16
- import hashlib
17
- import re
18
- from bs4 import BeautifulSoup as bs
19
15
20
16
from mkdocs_git_committers_plugin_2 .exclude import exclude
21
17
22
18
LOG = logging .getLogger ("mkdocs.plugins." + __name__ )
23
19
24
- class GitCommittersPlugin (BasePlugin ):
25
20
21
+ class GitCommittersPlugin (BasePlugin ):
26
22
config_scheme = (
27
- (' enterprise_hostname' , config_options .Type (str , default = '' )),
28
- (' repository' , config_options .Type (str , default = '' )),
29
- (' branch' , config_options .Type (str , default = ' master' )),
30
- (' docs_path' , config_options .Type (str , default = ' docs/' )),
31
- (' enabled' , config_options .Type (bool , default = True )),
32
- (' cache_dir' , config_options .Type (str , default = ' .cache/plugin/git-committers' )),
23
+ (" enterprise_hostname" , config_options .Type (str , default = "" )),
24
+ (" repository" , config_options .Type (str , default = "" )),
25
+ (" branch" , config_options .Type (str , default = " master" )),
26
+ (" docs_path" , config_options .Type (str , default = " docs/" )),
27
+ (" enabled" , config_options .Type (bool , default = True )),
28
+ (" cache_dir" , config_options .Type (str , default = " .cache/plugin/git-committers" )),
33
29
("exclude" , config_options .Type (list , default = [])),
34
30
)
35
31
36
32
def __init__ (self ):
37
33
self .total_time = 0
38
- self .branch = ' master'
34
+ self .branch = " master"
39
35
self .enabled = True
40
36
self .authors = dict ()
41
37
self .cache_page_authors = dict ()
42
38
self .exclude = list ()
43
- self .cache_date = ''
39
+ self .cache_date = ""
44
40
45
41
def on_config (self , config ):
46
- self .enabled = self .config [' enabled' ]
42
+ self .enabled = self .config [" enabled" ]
47
43
if not self .enabled :
48
44
LOG .info ("git-committers plugin DISABLED" )
49
45
return config
50
46
51
47
LOG .info ("git-committers plugin ENABLED" )
52
48
53
- if not self .config [' repository' ]:
49
+ if not self .config [" repository" ]:
54
50
LOG .error ("git-committers plugin: repository not specified" )
55
51
return config
56
- if self .config ['enterprise_hostname' ] and self .config ['enterprise_hostname' ] != '' :
57
- self .githuburl = "https://" + self .config ['enterprise_hostname' ] + "/"
52
+ if (
53
+ self .config ["enterprise_hostname" ]
54
+ and self .config ["enterprise_hostname" ] != ""
55
+ ):
56
+ self .githuburl = "https://" + self .config ["enterprise_hostname" ] + "/"
58
57
else :
59
58
self .githuburl = "https://github.com/"
60
59
self .localrepo = Repo ("." )
61
- self .branch = self .config [' branch' ]
62
- self .excluded_pages = self .config [' exclude' ]
60
+ self .branch = self .config [" branch" ]
61
+ self .excluded_pages = self .config [" exclude" ]
63
62
return config
64
63
65
64
def list_contributors (self , path ):
66
- if exclude (path .lstrip (self .config [' docs_path' ]), self .excluded_pages ):
65
+ if exclude (path .lstrip (self .config [" docs_path" ]), self .excluded_pages ):
67
66
return None , None
68
-
67
+
69
68
last_commit_date = ""
70
69
path = path .replace ("\\ " , "/" )
71
70
for c in Commit .iter_items (self .localrepo , self .localrepo .head , path ):
72
71
if not last_commit_date :
73
72
# Use the last commit and get the date
74
- last_commit_date = time .strftime ("%Y-%m-%d" , time .gmtime (c .authored_date ))
73
+ last_commit_date = time .strftime (
74
+ "%Y-%m-%d" , time .gmtime (c .authored_date )
75
+ )
75
76
76
77
# File not committed yet
77
78
if last_commit_date == "" :
@@ -80,68 +81,92 @@ def list_contributors(self, path):
80
81
81
82
# Try to leverage the cache
82
83
if path in self .cache_page_authors :
83
- if self .cache_date and time .strptime (last_commit_date , "%Y-%m-%d" ) < time .strptime (self .cache_date , "%Y-%m-%d" ):
84
- return self .cache_page_authors [path ]['authors' ], self .cache_page_authors [path ]['last_commit_date' ]
85
-
86
- url_contribs = self .githuburl + self .config ['repository' ] + "/contributors-list/" + self .config ['branch' ] + "/" + path
84
+ if self .cache_date and time .strptime (
85
+ last_commit_date , "%Y-%m-%d"
86
+ ) < time .strptime (self .cache_date , "%Y-%m-%d" ):
87
+ return (
88
+ self .cache_page_authors [path ]["authors" ],
89
+ self .cache_page_authors [path ]["last_commit_date" ],
90
+ )
91
+
92
+ url_contribs = (
93
+ self .githuburl
94
+ + self .config ["repository" ]
95
+ + "/contributors-list/"
96
+ + self .config ["branch" ]
97
+ + "/"
98
+ + path
99
+ )
87
100
LOG .info ("git-committers: fetching contributors for " + path )
88
101
LOG .debug (" from " + url_contribs )
89
- authors = []
102
+ authors = []
90
103
try :
91
104
response = requests .get (url_contribs )
92
105
response .raise_for_status ()
93
106
except HTTPError as http_err :
94
- LOG .error (f'git-committers: HTTP error occurred: { http_err } \n (404 is normal if file is not on GitHub yet or Git submodule)' )
107
+ LOG .error (
108
+ f"git-committers: HTTP error occurred: { http_err } \n (404 is normal if file is not on GitHub yet or Git submodule)"
109
+ )
95
110
except Exception as err :
96
- LOG .error (f' git-committers: Other error occurred: { err } ' )
111
+ LOG .error (f" git-committers: Other error occurred: { err } " )
97
112
else :
98
113
html = response .text
99
114
# Parse the HTML
100
115
soup = bs (html , "lxml" )
101
- lis = soup .find_all ('li' )
116
+ lis = soup .find_all ("li" )
102
117
for li in lis :
103
- a_tags = li .find_all ('a' )
104
- login = a_tags [0 ][' href' ].replace ("/" , "" )
118
+ a_tags = li .find_all ("a" )
119
+ login = a_tags [0 ][" href" ].replace ("/" , "" )
105
120
url = self .githuburl + login
106
121
name = login
107
- img_tags = li .find_all ('img' )
108
- avatar = img_tags [0 ]['src' ]
109
- avatar = re .sub (r'\?.*$' , '' , avatar )
110
- authors .append ({'login' :login , 'name' : name , 'url' : url , 'avatar' : avatar })
122
+ img_tags = li .find_all ("img" )
123
+ avatar = img_tags [0 ]["src" ]
124
+ avatar = re .sub (r"\?.*$" , "" , avatar )
125
+ authors .append (
126
+ {"login" : login , "name" : name , "url" : url , "avatar" : avatar }
127
+ )
111
128
# Update global cache_page_authors
112
- self .cache_page_authors [path ] = {'last_commit_date' : last_commit_date , 'authors' : authors }
129
+ self .cache_page_authors [path ] = {
130
+ "last_commit_date" : last_commit_date ,
131
+ "authors" : authors ,
132
+ }
113
133
114
134
return authors , last_commit_date
115
135
116
136
def on_page_context (self , context , page , config , nav ):
117
- context [' committers' ] = []
137
+ context [" committers" ] = []
118
138
if not self .enabled :
119
139
return context
120
140
start = timer ()
121
- git_path = self .config [' docs_path' ] + page .file .src_path
141
+ git_path = self .config [" docs_path" ] + page .file .src_path
122
142
authors , last_commit_date = self .list_contributors (git_path )
123
143
if authors :
124
- context [' committers' ] = authors
144
+ context [" committers" ] = authors
125
145
if last_commit_date :
126
- context [' last_commit_date' ] = last_commit_date
146
+ context [" last_commit_date" ] = last_commit_date
127
147
end = timer ()
128
- self .total_time += ( end - start )
148
+ self .total_time += end - start
129
149
130
150
return context
131
151
132
152
def on_post_build (self , config ):
133
153
LOG .info ("git-committers: saving page authors cache file" )
134
- json_data = json .dumps ({'cache_date' : datetime .now ().strftime ("%Y-%m-%d" ), 'page_authors' : self .cache_page_authors })
135
- os .makedirs (self .config ['cache_dir' ], exist_ok = True )
136
- f = open (self .config ['cache_dir' ] + "/page-authors.json" , "w" )
154
+ json_data = json .dumps (
155
+ {
156
+ "cache_date" : datetime .now ().strftime ("%Y-%m-%d" ),
157
+ "page_authors" : self .cache_page_authors ,
158
+ }
159
+ )
160
+ os .makedirs (self .config ["cache_dir" ], exist_ok = True )
161
+ f = open (self .config ["cache_dir" ] + "/page-authors.json" , "w" )
137
162
f .write (json_data )
138
163
f .close ()
139
164
140
165
def on_pre_build (self , config ):
141
- if os .path .exists (self .config [' cache_dir' ] + "/page-authors.json" ):
166
+ if os .path .exists (self .config [" cache_dir" ] + "/page-authors.json" ):
142
167
LOG .info ("git-committers: found page authors cache file - loading it" )
143
- f = open (self .config [' cache_dir' ] + "/page-authors.json" , "r " )
168
+ f = open (self .config [" cache_dir" ] + "/page-authors.json" )
144
169
cache = json .loads (f .read ())
145
- self .cache_date = cache [' cache_date' ]
146
- self .cache_page_authors = cache [' page_authors' ]
170
+ self .cache_date = cache [" cache_date" ]
171
+ self .cache_page_authors = cache [" page_authors" ]
147
172
f .close ()
0 commit comments