..
Viewing
utils.py
100 lines (85 loc) • 2.7 KB
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 | from datetime import datetime
from pygments import highlight
from pygments.lexers import get_lexer_for_filename
from pygments.lexers.special import TextLexer
from pygments.formatters import HtmlFormatter
def text_lines(text):
# return the number of lines in a text
return len(text.split("\n")) - 1
def text_loc(text):
text = text.strip()
# return the number of lines of code in a text
return len([line for line in text.split("\n") if line.strip()])
def size_format(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(size_bytes // 1024)
for size in size_name:
if i == 0:
return "{:.1f} {}".format(size_bytes, size)
size_bytes /= 1024
i = int(size_bytes // 1024)
def relative_date(entry):
committedDate = datetime.strptime(
entry["commit"]["committedDate"], "%Y-%m-%dT%H:%M:%SZ"
)
now = datetime.now()
diff = now - committedDate
if diff.days > 365:
entry["commit"]["committedDate"] = (
str(diff.days // 365)
+ " year"
+ ("s" if diff.days // 365 > 1 else "")
+ " ago"
)
elif diff.days > 30:
entry["commit"]["committedDate"] = (
str(diff.days // 30)
+ " month"
+ ("s" if diff.days // 30 > 1 else "")
+ " ago"
)
elif diff.days > 7:
entry["commit"]["committedDate"] = (
str(diff.days // 7) + " week" + ("s" if diff.days // 7 > 1 else "") + " ago"
)
elif diff.days > 0:
entry["commit"]["committedDate"] = (
str(diff.days) + " day" + ("s" if diff.days > 1 else "") + " ago"
)
elif diff.seconds > 3600:
entry["commit"]["committedDate"] = (
str(diff.seconds // 3600)
+ " hour"
+ ("s" if diff.seconds // 3600 > 1 else "")
+ " ago"
)
elif diff.seconds > 60:
entry["commit"]["committedDate"] = (
str(diff.seconds // 60)
+ " minute"
+ ("s" if diff.seconds // 60 > 1 else "")
+ " ago"
)
else:
entry["commit"]["committedDate"] = "just now"
return entry
def highlight_code(text, filename):
print(filename)
print(text)
try:
lexer = get_lexer_for_filename(filename, stripall=True)
except:
lexer = None
formatter = HtmlFormatter(
noclasses=True,
style="native",
wrapcode=True,
linenos="table",
nobackground=True,
)
if lexer:
return highlight(text, lexer, formatter)
else:
return highlight(text, TextLexer(), formatter)
|
|