# Conflicts:
#	Lib/designSpaceDocument/__init__.py
This commit is contained in:
Erik 2016-11-20 10:14:25 +01:00
commit 079771dd76
2 changed files with 62 additions and 52 deletions

4
.gitignore vendored
View File

@ -1,2 +1,6 @@
test.designspace test.designspace
Lib/DesignSpaceDocument.egg-info Lib/DesignSpaceDocument.egg-info
.cache
__pycache__
*.py[co]
.DS_Store

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import os import os
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
@ -19,13 +20,16 @@ __all__ = [
'SourceDescriptor', 'InstanceDescriptor', 'SourceDescriptor', 'InstanceDescriptor',
'AxisDescriptor', 'BaseDocReader', 'BaseDocWriter'] 'AxisDescriptor', 'BaseDocReader', 'BaseDocWriter']
class DesignSpaceDocumentError(Exception): class DesignSpaceDocumentError(Exception):
def __init__(self, msg, obj=None): def __init__(self, msg, obj=None):
self.msg = msg self.msg = msg
self.obj = obj self.obj = obj
def __str__(self): def __str__(self):
return repr(self.msg) + repr(self.obj) return repr(self.msg) + repr(self.obj)
def _indent(elem, whitespace=" ", level=0): def _indent(elem, whitespace=" ", level=0):
# taken from http://effbot.org/zone/element-lib.htm#prettyprint # taken from http://effbot.org/zone/element-lib.htm#prettyprint
i = "\n" + level * whitespace i = "\n" + level * whitespace
@ -42,16 +46,17 @@ def _indent(elem, whitespace=" ", level=0):
if level and (not elem.tail or not elem.tail.strip()): if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i elem.tail = i
class SimpleDescriptor(object): class SimpleDescriptor(object):
""" Containers for a bunch of attributes""" """ Containers for a bunch of attributes"""
def compare(self, other): def compare(self, other):
# test if this object contains the same data as the other # test if this object contains the same data as the other
for attr in self._attrs: for attr in self._attrs:
try: try:
#print getattr(self, attr), getattr(other, attr)
assert(getattr(self, attr) == getattr(other, attr)) assert(getattr(self, attr) == getattr(other, attr))
except AssertionError: except AssertionError:
print "failed attribute", attr, getattr(self, attr), "!=", getattr(other, attr) print("failed attribute", attr, getattr(self, attr), "!=", getattr(other, attr))
class SourceDescriptor(SimpleDescriptor): class SourceDescriptor(SimpleDescriptor):
"""Simple container for data related to the source""" """Simple container for data related to the source"""
@ -62,6 +67,7 @@ class SourceDescriptor(SimpleDescriptor):
'muteKerning', 'muteInfo', 'muteKerning', 'muteInfo',
'mutedGlyphNames', 'mutedGlyphNames',
'familyName', 'styleName'] 'familyName', 'styleName']
def __init__(self): def __init__(self):
self.path = None self.path = None
self.name = None self.name = None
@ -86,6 +92,7 @@ class InstanceDescriptor(SimpleDescriptor):
'styleMapFamilyName', 'styleMapFamilyName',
'styleMapStyleName', 'styleMapStyleName',
'kerning', 'info'] 'kerning', 'info']
def __init__(self): def __init__(self):
self.path = None self.path = None
self.name = None self.name = None
@ -104,6 +111,7 @@ class AxisDescriptor(SimpleDescriptor):
"""Simple container for the axis data""" """Simple container for the axis data"""
flavor = "axis" flavor = "axis"
_attrs = ['tag', 'name', 'maximum', 'minimum', 'default', 'map'] _attrs = ['tag', 'name', 'maximum', 'minimum', 'default', 'map']
def __init__(self): def __init__(self):
self.tag = None # opentype tag for this axis self.tag = None # opentype tag for this axis
self.name = None # name of the axis used in locations self.name = None # name of the axis used in locations
@ -114,7 +122,7 @@ class AxisDescriptor(SimpleDescriptor):
self.map = [] self.map = []
def serialize(self): def serialize(self):
# output to a dict # output to a dict, used in testing
d = dict(tag = self.tag, d = dict(tag = self.tag,
name = self.name, name = self.name,
labelNames = self.labelNames, labelNames = self.labelNames,
@ -131,6 +139,7 @@ class BaseDocWriter(object):
axisDescriptorClass = AxisDescriptor axisDescriptorClass = AxisDescriptor
sourceDescriptorClass = SourceDescriptor sourceDescriptorClass = SourceDescriptor
instanceDescriptorClass = InstanceDescriptor instanceDescriptorClass = InstanceDescriptor
def __init__(self, documentPath, documentObject): def __init__(self, documentPath, documentObject):
self.path = documentPath self.path = documentPath
self.documentObject = documentObject self.documentObject = documentObject
@ -158,7 +167,7 @@ class BaseDocWriter(object):
if pretty: if pretty:
_indent(self.root, whitespace=self._whiteSpace) _indent(self.root, whitespace=self._whiteSpace)
tree = ET.ElementTree(self.root) tree = ET.ElementTree(self.root)
tree.write(self.path, encoding=u"utf-8", method='xml', xml_declaration=True) tree.write(self.path, encoding="utf-8", method='xml', xml_declaration=True)
def _makeLocationElement(self, locationObject, name=None): def _makeLocationElement(self, locationObject, name=None):
""" Convert Location dict to a locationElement.""" """ Convert Location dict to a locationElement."""
@ -319,10 +328,12 @@ class BaseDocWriter(object):
glyphElement.append(mastersElement) glyphElement.append(mastersElement)
return glyphElement return glyphElement
class BaseDocReader(object): class BaseDocReader(object):
axisDescriptorClass = AxisDescriptor axisDescriptorClass = AxisDescriptor
sourceDescriptorClass = SourceDescriptor sourceDescriptorClass = SourceDescriptor
instanceDescriptorClass = InstanceDescriptor instanceDescriptorClass = InstanceDescriptor
def __init__(self, documentPath, documentObject): def __init__(self, documentPath, documentObject):
self.path = documentPath self.path = documentPath
self.documentObject = documentObject self.documentObject = documentObject
@ -620,14 +631,10 @@ class DesignSpaceDocument(object):
return loc return loc
if __name__ == "__main__": if __name__ == "__main__":
def test(): def test():
u""" """
>>> import os >>> import os
>>> testDocPath = os.path.join(os.getcwd(), "test.designspace") >>> testDocPath = os.path.join(os.getcwd(), "test.designspace")
>>> masterPath1 = os.path.join(os.getcwd(), "masters", "masterTest1.ufo") >>> masterPath1 = os.path.join(os.getcwd(), "masters", "masterTest1.ufo")
@ -753,5 +760,4 @@ if __name__ == "__main__":
def _test(): def _test():
import doctest import doctest
doctest.testmod() doctest.testmod()
_test() _test()