summaryrefslogtreecommitdiff
path: root/cal-heatmap.py
blob: 0c07b98f8e02a61c477f4f8d7301792c861518aa (plain)
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
#
# CALENDAR HEATMAP
#
# DESCRIPTION
# Creates a calendar heatmap from a git commit frequency list.
# The frequency list must be in JSON and formatted as 'YYYY-MM-DD: FREQ'.
#
# ARGS
# 1nd arg: path to JSON frequency list
# 2st arg (optional): output file path
#
# REQUIREMENTS
# matplotlib july
#
# NOTE
# If receiving Matplotlib warning, install patch:
# pip install -U git+https://github.com/thoellrich/july.git@fix-mpl.cbook.MatplotlibDepreciationWarning
#

import os
import sys
import json
import july
import matplotlib.pyplot as plt

from datetime import datetime as dtime
from july.utils import date_range as drange

if len(sys.argv) == 1:
    print("Error: commits file not specified")
    quit()

freqlist = sys.argv[1]

if len(sys.argv) == 2:
    print("Output file not specified; defaulting to current directory")
    outfile = "activity.png"
else:
    outfile = sys.argv[2]

if not os.path.exists(freqlist):
    print("Error: file '{}' does not exists".format(freqlist))
    quit()

commits = json.load(open(freqlist,'r'))

# Create this years calendar
start='{}-01-01'.format(dtime.now().year)
end='{}-12-31'.format(dtime.now().year)
calendar = [ d.strftime("%F") for d in drange(start,end) ]

# Populate calendar with commits
cal = {}
for date in calendar:
    cal[date] = commits[date] if date in commits else 0

# Create heatmap
july.heatmap(cal.keys(),
             cal.values(),
             month_grid=True,
             weekday_label=True,
             title="Activity",
             cmap="github")

# Save image
plt.savefig(outfile, bbox_inches='tight', pad_inches=0.0)