Skip to content

Commit

Permalink
improving the GitHub Markdown reporter
Browse files Browse the repository at this point in the history
(issue #1754)
  • Loading branch information
felipesanches committed May 14, 2018
1 parent 7846326 commit ea2cd06
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 76 deletions.
81 changes: 10 additions & 71 deletions Lib/fontbakery/commands/check_specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

from fontbakery.reporters.terminal import TerminalReporter
from fontbakery.reporters.serialize import SerializeReporter
from fontbakery.reporters.ghmarkdown import GHMarkdownReporter

def ArgumentParser(specification, spec_arg=True):
argument_parser = argparse.ArgumentParser(description="Check TTF files"
Expand Down Expand Up @@ -265,86 +266,24 @@ def main(specification=None, values=None):
)
reporters = [tr.receive]

if args.json or args.ghmarkdown:
if args.json:
sr = SerializeReporter(runner=runner, collect_results_by=args.gather_by)
reporters.append(sr.receive)

if args.ghmarkdown:
mdr = GHMarkdownReporter(loglevels=args.loglevels,
runner=runner,
collect_results_by=args.gather_by)
reporters.append(mdr.receive)

distribute_generator(runner.run(), reporters)

if args.json:
import json
json.dump(sr.getdoc(), args.json, sort_keys=True, indent=4)

if args.ghmarkdown:
data = sr.getdoc()
from random import randint
pass_emoticon = [":croissant:",
":cake:",
":doughnut:",
":bread:"][randint(0, 3)]
def emoticon(name):
return {'ERROR': ':broken_heart:',
'FAIL': ':fire:',
'WARN': ':warning:',
'INFO': ':information_source:',
'SKIP': ':zzz:',
'PASS': pass_emoticon,
}[name]

def html5_collapsible(summary, details):
return """
<details>
<summary>{}</summary>
{}
</details>
""".format(summary, details)

def log_md(log):
return " * **{}:** {}\n".format(log["status"], log["message"])

def check_md(check):
checkid = check["key"][1].split(":")[1].split(">")[0]

logs = "".join(map(log_md, check["logs"]))
github_search_url = ("[{}](https://github.com/googlefonts/fontbakery/"
"search?q={})").format(checkid, checkid)
return html5_collapsible("{} <b>{}:</b> {}".format(emoticon(check["result"]),
check["result"],
check["description"]),
"\n* {}\n{}".format(github_search_url,
logs))
summary_table = ("### Summary\n\n"
"| {} ERROR | {} FAIL | {} WARN | {} SKIP | {} INFO | {} PASS |\n"
"").format(*[emoticon(k) for k in ["ERROR","FAIL","WARN","SKIP","INFO","PASS"]]) + \
("|:-----:|:----:|:----:|:----:|:----:|:----:|\n"
"| {} | {} | {} | {} | {} | {} |\n"
"").format(*[data["result"][k] for k in ["ERROR","FAIL","WARN","SKIP","INFO","PASS"]])
checks = {}
family_checks = []
for section in data["sections"]:
for check in section["checks"]:
if args.loglevels and (args.loglevels[0] > Status(check["result"])):
continue

if check["key"][2] == (): # That's a family check!
family_checks.append(check)
else:
key = os.path.basename(check["filename"])
if key not in checks:
checks[key] = []
checks[key].append(check)

family_checks.sort(key=lambda c: c["result"])
md = html5_collapsible("<b>[{}] Family checks</b>".format(len(family_checks)),
"".join(map(check_md, family_checks)) + "<br>")

for filename in checks.keys():
checks[filename].sort(key=lambda c: c["result"])
md += html5_collapsible("<b>[{}] {}</b>".format(len(checks[filename]),
filename),
"".join(map(check_md, checks[filename])) + "<br>")

md += "\n" + summary_table
args.ghmarkdown.write(md)
args.ghmarkdown.write(mdr.get_markdown())

# Fail and error let the command fail
return 1 if tr.worst_check_status in (ERROR, FAIL) else 0
Expand Down
111 changes: 111 additions & 0 deletions Lib/fontbakery/reporters/ghmarkdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals, division

from future import standard_library
standard_library.install_aliases()
from builtins import map
import os
from random import randint
from fontbakery.reporters.serialize import SerializeReporter
from fontbakery.checkrunner import Status

LOGLEVELS=["ERROR","FAIL","WARN","SKIP","INFO","PASS"]

PASS_EMOJI = [
":croissant:",
":cake:",
":doughnut:",
":bread:"
]

class GHMarkdownReporter(SerializeReporter):

def __init__(self, loglevels, **kwd):
super(GHMarkdownReporter, self).__init__(**kwd)
self.pass_emoticon = PASS_EMOJI[randint(0, len(PASS_EMOJI)-1)]
self.loglevels = loglevels


def emoticon(self, name):
return {
'ERROR': ':broken_heart:',
'FAIL': ':fire:',
'WARN': ':warning:',
'INFO': ':information_source:',
'SKIP': ':zzz:',
'PASS': self.pass_emoticon,
}[name]


def html5_collapsible(self, summary, details):
return ("<details>\n"
"<summary>{}</summary>\n"
"{}\n"
"</details>\n").format(summary, details)


def log_md(self, log):
return "* **{}:** {}\n".format(log["status"], log["message"])


def check_md(self, check):
checkid = check["key"][1].split(":")[1].split(">")[0]

logs = "".join(map(self.log_md, check["logs"]))
github_search_url = ("[{}](https://github.com/googlefonts/fontbakery/"
"search?q={})").format(checkid, checkid)
return self.html5_collapsible("{} <b>{}:</b> {}".format(self.emoticon(check["result"]),
check["result"],
check["description"]),
"\n* {}\n{}".format(github_search_url, logs))

def omit_loglevel(self, msg):
return self.loglevels and (self.loglevels[0] > Status(msg))

def get_markdown(self):
checks = {}
family_checks = []
data = self.getdoc()
num_checks = 0
for section in data["sections"]:
num_checks += len(section["checks"])
for check in section["checks"]:
if self.omit_loglevel(check["result"]):
continue

if "filename" not in check.keys():
# That's a family check!
family_checks.append(check)
else:
key = os.path.basename(check["filename"])
if key not in checks:
checks[key] = []
checks[key].append(check)

family_checks.sort(key=lambda c: c["result"])
md = self.html5_collapsible("<b>[{}] Family checks</b>".format(len(family_checks)),
"".join(map(self.check_md, family_checks)) + "<br>")

for filename in checks.keys():
checks[filename].sort(key=lambda c: LOGLEVELS.index(c["result"]))
md += self.html5_collapsible("<b>[{}] {}</b>".format(len(checks[filename]),
filename),
"".join(map(self.check_md, checks[filename])) + "<br>")

if num_checks != 0:
summary_table = "### Summary\n\n" + \
("| {} " + " | {} ".join(LOGLEVELS) + " |\n").format(*[self.emoticon(k) for k in LOGLEVELS]) + \
("|:-----:|:----:|:----:|:----:|:----:|:----:|\n"
"| {} | {} | {} | {} | {} | {} |\n"
"").format(*[data["result"][k] for k in LOGLEVELS]) +\
("| {:.0f}% | {:.0f}% | {:.0f}% | {:.0f}% | {:.0f}% | {:.0f}% |\n"
"").format(*[100*data["result"][k]/num_checks for k in LOGLEVELS])
md += "\n" + summary_table

omitted = [l for l in LOGLEVELS if self.omit_loglevel(l)]
if omitted:
md += "\n" + \
"**Note:** The following loglevels were omitted in this report:\n" + \
"".join(map(lambda o: "* **{}**\n".format(o), omitted))

return md
9 changes: 4 additions & 5 deletions Lib/fontbakery/specifications/googlefonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,27 +820,26 @@ def com_google_fonts_check_054(font, ttfautohint_stats):
dehinted = ttfautohint_stats["dehinted_size"]
increase = hinted - dehinted
change = float(hinted)/dehinted - 1
change = int(change*10000)/100.0 # round to 2 decimal pts percentage

def filesize_formatting(s):
if s < 1024:
return "{} bytes".format(s)
elif s < 1024*1024:
return "{}kb".format(s/1024)
return "{:.1f}kb".format(s/1024)
else:
return "{}Mb".format(s/(1024*1024))
return "{:.1f}Mb".format(s/(1024*1024))

hinted_size = filesize_formatting(hinted)
dehinted_size = filesize_formatting(dehinted)
increase = filesize_formatting(increase)

results_table = "Hinting filesize impact:\n\n"
results_table += "| | {} |\n".format(font)
results_table += "|:--- | ---:| ---:|\n"
results_table += "|:--- | ---:|\n"
results_table += "| Dehinted Size | {} |\n".format(dehinted_size)
results_table += "| Hinted Size | {} |\n".format(hinted_size)
results_table += "| Increase | {} |\n".format(increase)
results_table += "| Change | {} % |\n".format(change)
results_table += "| Change | {:.1f} % |\n".format(change)
yield INFO, results_table


Expand Down

0 comments on commit ea2cd06

Please sign in to comment.