Skip to content

Commit

Permalink
1st version where the webapp (based on name switcher) works
Browse files Browse the repository at this point in the history
triggers a server-side fontbakery-check-ttf run and displays the markdown report.
It surely still needs to have the UI polished tough.
(issue fonttools#1121)
  • Loading branch information
felipesanches committed Oct 27, 2016
1 parent 45e1f81 commit 8c0d938
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 36 deletions.
69 changes: 45 additions & 24 deletions fontbakery-check-ttf.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,27 +301,38 @@ def output_report(self, font_file):
self.all_checks = filtered

if self.config['json']:
json_path = font_file + ".fontbakery.json"
self.output_json_report(json_path)
self.output_json_report(font_file)

if self.config['ghm']:
md_path = font_file + ".fontbakery.md"
self.output_github_markdown_report(md_path)
self.output_github_markdown_report(font_file)

def output_json_report(self, filename):
def output_json_report(self, font_file):
import json
json_data = json.dumps(self.all_checks,
sort_keys=True,
indent=4,
separators=(',', ': '))
open(filename, 'w').write(json_data)
json_report_files.append(filename)

def output_github_markdown_report(self, filename):
if self.config['inmem']:
json_output = BytesIO()
json_output.write(json_data)
json_report_files.append((self.target[0]["filename"], json_output))
else:
json_output = open(font_file + ".fontbakery.json", 'w')
json_output.write(json_data)
json_report_files.append(json_output)


def output_github_markdown_report(self, font_file):

markdown_data = "# Fontbakery check results\n"
all_checks = {}
for check in self.all_checks:
target = check["target"]

if type(target) is tuple: # In-memory file-like font-object
target = target[0]["filename"]

if target in all_checks.keys():
all_checks[target].append(check)
else:
Expand All @@ -335,8 +346,13 @@ def output_github_markdown_report(self, filename):
markdown_data += ("### {}\n"
"* {}\n\n").format(check['description'], msgs)

open(filename, 'w').write(markdown_data)
ghm_report_files.append(filename)

if self.config['inmem']:
ghm_report_files.append((font_file[0], markdown_data))
else:
ghm_output = open(font_file + ".fontbakery.md", 'w')
ghm_output.write(markdown_data)
ghm_report_files.append(font_file + ".fontbakery.md")

def update_progressbar(self):
tick = {
Expand Down Expand Up @@ -371,6 +387,9 @@ def set_target(self, value):
'''sets target of the current check.
This can be a folder, or a specific TTF file
or a METADATA.pb file'''
# if type(value) is tuple:
# value = value[0]["filename"] # "In-memory font object"

if self.current_check:
self.current_check["target"] = value

Expand Down Expand Up @@ -3745,11 +3764,9 @@ def is_italic():
else:
fb.ok("Font em size is equal to 1000.")

fb.output_report(font_file)

if webapp:
# TODO: do something here to output the reports to the web!
pass
else:
if not webapp:
# ----------------------------------------------------
# https://github.com/googlefonts/fontbakery/issues/971
# DC: Each fix line should set a fix flag, and
Expand All @@ -3763,7 +3780,6 @@ def is_italic():
logging.info("{} saved\n".format(font_file_output))
font.close()

fb.output_report(font_file)
fb.reset_report()

# -------------------------------------------------------
Expand All @@ -3782,20 +3798,25 @@ def is_italic():
" --ghm \tSave results to a file in GitHub Markdown format.\n"
" --error\tPrint only the error messages (outputs to stderr).\n")

if len(json_report_files) > 0:
print(("Saved check results in "
"JSON format to:\n\t{}"
"").format('\n\t'.join(json_report_files)))
if len(ghm_report_files) > 0:
print(("Saved check results in "
"GitHub Markdown format to:\n\t{}"
"").format('\n\t'.join(ghm_report_files)))


if webapp:
return ghm_report_files
else:
if len(json_report_files) > 0:
print(("Saved check results in "
"JSON format to:\n\t{}"
"").format('\n\t'.join(json_report_files)))
if len(ghm_report_files) > 0:
print(("Saved check results in "
"GitHub Markdown format to:\n\t{}"
"").format('\n\t'.join(ghm_report_files)))

__author__ = "The Font Bakery Authors"
if __name__ == '__main__':
args = parser.parse_args()
config = {
'filepaths': args.arg_filepaths,
'files': args.arg_filepaths,
'autofix': args.autofix,
'verbose': args.verbose,
'json': args.json,
Expand Down
17 changes: 10 additions & 7 deletions webapp/app/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# [START app]
from __future__ import print_function
import logging
from flask import Flask, make_response, request
from flask import Flask, make_response, request, Response
from fontTools.ttLib import TTFont
import struct
from io import BytesIO
Expand Down Expand Up @@ -36,11 +36,10 @@ def run_fontbakery():
'error': False,
'autofix': False,
'inmem': True,
'filepaths': None,
'files': _unpack(request.stream)
}

check_ttf(config)
reports = check_ttf(config)

# TODO:
# if hotfixing:
Expand All @@ -53,14 +52,18 @@ def run_fontbakery():
if hotfixing:
zipf.close()
data = result.getvalue()
response = make_response(data)
response = app.make_response(data)
response.headers['Content-Type'] = 'application/octet-stream'
response.headers['Content-Disposition'] = 'attachment; filename=fonts-with-changed-names.zip'
return response
else:
response = make_response("Fontbakery check results will show up here!")
response.headers['Content-Type'] = 'text/html'
from markdown import markdown
report_data = ""
for desc, report_file in reports:
if desc["filename"] is not None:
report_data += "<h3>{}</h3>{}".format(desc["filename"], markdown(report_file))

return response
return "<h2>Fontbakery check results</h2><div>{}</div>".format(report_data)

@app.errorhandler(500)
def server_error(e):
Expand Down
1 change: 1 addition & 0 deletions webapp/app/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ <h1>FontBakery</h1>
<ol class="files-controls"></ol>
<div class="general-controls"><em>2.</em><p>Do you want hotfixing?</p></div>
</div>
<div id="results"></div>
</article>
</body>
</html>
15 changes: 10 additions & 5 deletions webapp/app/static/js/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,25 @@ define([

xhr = new XMLHttpRequest();
xhr.open('POST', 'runchecks');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
// xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.send(data);
xhr.responseType = 'arraybuffer';
// xhr.responseType = 'arraybuffer';
xhr.responseType = 'html';

xhr.onreadystatechange = function () {
if(xhr.readyState !== XMLHttpRequest.DONE)
return;
if(xhr.status !== 200)
console.warn(xhr.status, xhr.statusText );
else {
//alert(xhr.response);
document.getElementById("results").innerHTML = xhr.response;
console.info('Received:', xhr.responseType, xhr.response.byteLength, 'Bytes');
blob = new Blob([xhr.response], {type: "application/zip"});
window.open(URL.createObjectURL(blob));
//blob = new Blob([xhr.response], {type: "application/zip"});
//window.open(URL.createObjectURL(blob));
}
};
};

};

_p._updateAmendment = function(e) {
Expand Down
1 change: 1 addition & 0 deletions webapp/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
flask
fonttools
gcloud
markdown

0 comments on commit 8c0d938

Please sign in to comment.