Skip to content

Instantly share code, notes, and snippets.

@anthrotype
Last active September 4, 2017 20:44
Show Gist options
  • Select an option

  • Save anthrotype/3f4c9b3e04c22ddcfda196b970de8549 to your computer and use it in GitHub Desktop.

Select an option

Save anthrotype/3f4c9b3e04c22ddcfda196b970de8549 to your computer and use it in GitHub Desktop.
Test parsing/writing of .glyphs files with glyphsLib master vs 'classtree' branch
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
from fontTools.misc.py23 import open, StringIO, tounicode, tostr
import difflib
from glyphsLib import loads
try:
# 'GlyphsWriter' is only present on 'classtree' branch
from glyphsLib.writer import GlyphsWriter
ON_CLASSTREE_BRANCH = True
except ImportError:
ON_CLASSTREE_BRANCH = False
def roundtrip_master(path):
from glyphsLib import dump
with open(path, 'r', encoding='utf-8') as f:
input_data = f.read()
s = StringIO()
dump(loads(input_data), s)
output_data = s.getvalue()
actual = tounicode(output_data, 'utf-8').splitlines(True)
expected = input_data.splitlines(True)
return expected, actual
def roundtrip_classtree(path):
with open(path, 'r', encoding='utf-8') as f:
input_data = f.read()
s = StringIO()
w = GlyphsWriter(fp=s)
# XXX can't use `GlyphsWriter.write` as it closes the file
w.writeDict(loads(input_data))
s.write(tostr("\n"))
output_data = s.getvalue()
actual = tounicode(output_data, 'utf-8').splitlines(True)
expected = input_data.splitlines(True)
return expected, actual
def diff_roundtrip(path):
name = os.path.basename(path)
if ON_CLASSTREE_BRANCH:
expected, actual = roundtrip_classtree(path)
else:
expected, actual = roundtrip_master(path)
if actual != expected:
for line in difflib.unified_diff(
expected, actual,
fromfile="%s <input>" % name,
tofile="%s <roundtripped>" % name):
print(line, end="")
return 1
return 0
def main():
files = sys.argv[1:]
if not files:
print("./roundtrip.py file [file ...]")
return 1
rv = 0
for i, path in enumerate(files):
if i > 0:
print("\n======\n")
rv |= diff_roundtrip(path)
return rv
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment