-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
util.py
212 lines (184 loc) · 8.01 KB
/
util.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""Utility class for mkdocs plugin."""
import logging
import os
import time
from datetime import datetime
from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
from babel.dates import format_date, get_timezone
from git import (
Repo,
Git,
GitCommandError,
GitCommandNotFound,
InvalidGitRepositoryError,
NoSuchPathError,
)
from typing import Any, Dict
logger = logging.getLogger("mkdocs.plugins")
class Util:
"""Utility class.
This helps find git and calculate relevant dates.
"""
def __init__(self, config={}):
"""Initialize utility class."""
self.config = config
self.repo_cache = {}
def _get_repo(self, path: str) -> Git:
if not os.path.isdir(path):
path = os.path.dirname(path)
if path not in self.repo_cache:
self.repo_cache[path] = Repo(path, search_parent_directories=True).git
# Checks if user is running builds on CI
# and raise appropriate warnings
raise_ci_warnings(self.repo_cache[path])
return self.repo_cache[path]
@staticmethod
def _date_formats(
unix_timestamp: float, locale: str = "en", time_zone: str = "UTC"
) -> Dict[str, Any]:
"""
Calculate different date formats / types.
Args:
unix_timestamp (float): A timestamp in seconds since 1970.
locale (str): Locale code of language to use. Defaults to 'en'.
time_zone (str): Timezone database name (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
Returns:
dict: Different date formats.
"""
utc_revision_date = datetime.utcfromtimestamp(int(unix_timestamp))
loc_revision_date = utc_revision_date.replace(
tzinfo=get_timezone("UTC")
).astimezone(get_timezone(time_zone))
return {
"date": format_date(loc_revision_date, format="long", locale=locale),
"datetime": " ".join(
[
format_date(loc_revision_date, format="long", locale=locale),
loc_revision_date.strftime("%H:%M:%S"),
]
),
"iso_date": loc_revision_date.strftime("%Y-%m-%d"),
"iso_datetime": loc_revision_date.strftime("%Y-%m-%d %H:%M:%S"),
"timeago": '<span class="timeago" datetime="%s" locale="%s"></span>'
% (loc_revision_date.isoformat(), locale),
}
def get_git_commit_timestamp(
self,
path: str,
is_first_commit: bool = False
) -> int:
"""
Get a list of commit dates in unix timestamp, starts with the most recent commit.
Args:
is_first_commit (bool): if true, get the timestamp of the first commit,
else, get that of the most recent commit.
path (str): Location of a markdown file that is part of a Git repository.
is_first_commit (bool): retrieve commit timestamp when file was created.
Returns:
int: commit date in unix timestamp, starts with the most recent commit.
"""
commit_timestamp = ""
# perform git log operation
try:
# Retrieve author date in UNIX format (%at)
# https://git-scm.com/docs/git-log#Documentation/git-log.txt-ematem
# https://git-scm.com/docs/git-log#Documentation/git-log.txt---diff-filterACDMRTUXB82308203
realpath = os.path.realpath(path)
git = self._get_repo(realpath)
if is_first_commit:
# diff_filter="A" will select the commit that created the file
commit_timestamp = git.log(
realpath, date="short", format="%at", diff_filter="A"
)
# A file can be created multiple times, through a file renamed.
# Commits are ordered with most recent commit first
# Get the oldest commit only
if commit_timestamp != "":
commit_timestamp = commit_timestamp.split()[-1]
else:
# Latest commit touching a specific file
commit_timestamp = git.log(
realpath, date="short", format="%at", n=1
)
except (InvalidGitRepositoryError, NoSuchPathError) as err:
if self.config.get('fallback_to_build_date'):
logger.warning(
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
)
commit_timestamp = time.time()
else:
logger.error(
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
" To ignore this error, set option 'fallback_to_build_date: true'"
)
raise err
except GitCommandError as err:
if self.config.get('fallback_to_build_date'):
logger.warning(
"[git-revision-date-localized-plugin] Unable to read git logs of '%s'. Is git log readable?"
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
% path
)
commit_timestamp = time.time()
else:
logger.error(
"[git-revision-date-localized-plugin] Unable to read git logs of '%s'. "
" To ignore this error, set option 'fallback_to_build_date: true'"
% path
)
raise err
except GitCommandNotFound as err:
if self.config.get('fallback_to_build_date'):
logger.warning(
"[git-revision-date-localized-plugin] Unable to perform command: 'git log'. Is git installed?"
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
)
commit_timestamp = time.time()
else:
logger.error(
"[git-revision-date-localized-plugin] Unable to perform command 'git log'. Is git installed?"
" To ignore this error, set option 'fallback_to_build_date: true'"
)
raise err
# create timestamp
if commit_timestamp == "":
commit_timestamp = time.time()
logger.warning(
"[git-revision-date-localized-plugin] '%s' has no git logs, using current timestamp"
% path
)
return int(commit_timestamp)
def get_date_formats_for_timestamp(
self,
commit_timestamp: int,
add_spans: bool = True
) -> Dict[str, str]:
"""
Determine localized date variants for a given timestamp.
Args:
commit_timestamp (int): most recent commit date in unix timestamp.
locale (str, optional): Locale code of language to use. Defaults to 'en'.
time_zone (str): Timezone database name (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
Returns:
dict: Localized date variants.
"""
date_formats = self._date_formats(
unix_timestamp=commit_timestamp,
time_zone=self.config.get("time_zone"),
locale=self.config.get("locale")
)
if add_spans:
date_formats = self.add_spans(date_formats)
return date_formats
@staticmethod
def add_spans(date_formats: Dict[str, str]) -> Dict[str, str]:
"""
Wraps the date string in <span> elements with CSS identifiers.
"""
for date_type, date_string in date_formats.items():
date_formats[date_type] = (
'<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-%s">%s</span>'
% (date_type, date_string)
)
return date_formats