Ejemplo n.º 1
0
def create_graph(filepaths):
    """
    Prepare the `index.html` template.
    """
    graphs = []
    if isinstance(filepaths, StringTypes):
        filepaths = [filepaths]

    for path in filepaths:
        contributions = parser.parse_file(path)

        graph = {
            "data":
            gridify_contributions(contributions),
            "cell_class":
            _cell_class(contributions.values()),
            "longest_streak":
            statistics.longest_streak(
                [key for key, val in contributions.iteritems() if val > 0]),
            "current_streak":
            statistics.current_streak(
                [key for key, val in contributions.iteritems() if val > 0]),
            "sum":
            sum(contributions.itervalues()),
        }

        graph["last_date"] = (
            [""] + sorted([key
                           for key, v in contributions.iteritems() if v]))[-1]

        graphs.append(graph)

    env = Environment(loader=PackageLoader('contributions', 'templates'))

    env.filters['tooltip'] = tooltip_text
    env.filters['display_date'] = dateutils.display_date
    env.filters['elapsed_time'] = dateutils.elapsed_time

    template = env.get_template("index.html")

    weekdays = dateutils.weekday_initials()
    for idx in [0, 2, 4, 6]:
        weekdays[idx] = ""

    months = [
        cell.date.strftime("%b")
        for cell in gridify_contributions(contributions)[0]
    ]
    months = filter_months(months)

    return template.render(graphs=graphs,
                           today=dateutils.today(),
                           start=dateutils.start(),
                           weekdays=weekdays,
                           months=months)
Ejemplo n.º 2
0
def create_graph(filepaths):
    """
    Prepare the `index.html` template.
    """
    graphs = []
    if isinstance(filepaths, StringTypes):
        filepaths = [filepaths]

    for path in filepaths:
        contributions = parser.parse_file(path)

        graph = {
            "data": gridify_contributions(contributions),
            "cell_class": _cell_class(contributions.values()),
            "longest_streak": statistics.longest_streak(
                [key for key, val in contributions.iteritems() if val > 0]
            ),
            "current_streak": statistics.current_streak(
                [key for key, val in contributions.iteritems() if val > 0]
            ),
            "sum": sum(contributions.itervalues()),
            "repo_name": ntpath.basename(path)
        }

        graph["last_date"] = (
            [""] + sorted([key for key, v in contributions.iteritems() if v])
        )[-1]

        graphs.append(graph)

    env = Environment(loader=PackageLoader('contributions', 'templates'))

    env.filters['tooltip'] = tooltip_text
    env.filters['display_date'] = dateutils.display_date
    env.filters['elapsed_time'] = dateutils.elapsed_time

    template = env.get_template("index.html")

    weekdays = dateutils.weekday_initials()
    for idx in [0, 2, 4, 6]:
        weekdays[idx] = ""

    months = [
        cell.date.strftime("%b")
        for cell in gridify_contributions(contributions)[0]
    ]
    months = filter_months(months)

    return template.render(graphs=graphs,
                           today=dateutils.today(),
                           start=dateutils.start(),
                           weekdays=weekdays,
                           months=months)
Ejemplo n.º 3
0
 def class_label(cell):
     if cell.date > dateutils.today() or cell.date < dateutils.start():
         return "empty"
     elif cell.contributions == 0:
         return "grad0"
     elif cell.contributions <= quartiles[1]:
         return "grad1"
     elif cell.contributions <= quartiles[2]:
         return "grad2"
     elif cell.contributions <= quartiles[3]:
         return "grad3"
     else:
         return "grad4"
Ejemplo n.º 4
0
 def class_label(cell):
     if cell.date > dateutils.today() or cell.date < dateutils.start():
         return "empty"
     elif cell.contributions == 0:
         return "grad0"
     elif cell.contributions <= quartiles[1]:
         return "grad1"
     elif cell.contributions <= quartiles[2]:
         return "grad2"
     elif cell.contributions <= quartiles[3]:
         return "grad3"
     else:
         return "grad4"
def gridify_contributions(contributions):
    """
    The contributions graph has seven rows (one for each day of the week).
    It spans a year. Given a dict of date/value pairs, rearrange these results
    into seven rows of "cells", where each cell records a date and a value.
    """
    start = dateutils.start()
    today = dateutils.today()

    graph_entries = []

    # The first row is a Sunday, so go back to the last Sunday before the start
    if start.weekday() == 6:
        first_date = start
    else:
        first_date = start - datetime.timedelta(start.weekday() + 1 % 7)
    next_date = first_date

    first_row_dates = [first_date]
    while (next_date <= today) and (next_date + datetime.timedelta(7) <= today):
        next_date += datetime.timedelta(7)
        first_row_dates.append(next_date)

    # Now get contribution counts for each of these dates, and save the row
    first_row = [
        GridCell(date, contributions[date]) for date in first_row_dates
    ]
    graph_entries.append(first_row)

    # For each subsequent day of the week, use the first row as a model: add
    # the appropriate number of days and count the contributions
    for i in range(1, 7):
        row_dates = [day + datetime.timedelta(i) for day in first_row_dates]
        next_row = [
            GridCell(date, contributions[date]) for date in row_dates
        ]
        graph_entries.append(next_row)

    return graph_entries