whitespace

This commit is contained in:
Denis Moyogo Jacquerye 2016-11-15 20:15:04 +00:00
parent 41f5cd7e5d
commit 6770d6a02f

View File

@ -15,17 +15,20 @@ import xml.etree.ElementTree as ET
__all__ = [
'DesignSpaceDocumentError', 'BaseDocReader', 'DesignSpaceDocument',
'DesignSpaceDocumentError', 'BaseDocReader', 'DesignSpaceDocument',
'SourceDescriptor', 'InstanceDescriptor',
'AxisDescriptor', 'BaseDocReader', 'BaseDocWriter']
class DesignSpaceDocumentError(Exception):
def __init__(self, msg, obj=None):
self.msg = msg
self.obj = obj
def __str__(self):
return repr(self.msg) + repr(self.obj)
def _indent(elem, whitespace=" ", level=0):
# taken from http://effbot.org/zone/element-lib.htm#prettyprint
i = "\n" + level * whitespace
@ -42,26 +45,29 @@ def _indent(elem, whitespace=" ", level=0):
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
class SimpleDescriptor(object):
""" Containers for a bunch of attributes"""
def compare(self, other):
# test if this object contains the same data as the other
for attr in self._attrs:
try:
#print getattr(self, attr), getattr(other, attr)
assert(getattr(self,attr) == getattr(other,attr))
# print getattr(self, attr), getattr(other, attr)
assert(getattr(self, attr) == getattr(other, attr))
except AssertionError:
print "failed attribute", attr, getattr(self, attr), "!=", getattr(other, attr)
class SourceDescriptor(SimpleDescriptor):
"""Simple container for data related to the source"""
flavor="source"
_attrs = [ 'path', 'name',
'location', 'copyLib',
'copyGroups', 'copyFeatures',
'muteKerning', 'muteInfo',
'mutedGlyphNames',
'familyName', 'styleName']
flavor = "source"
_attrs = ['path', 'name',
'location', 'copyLib',
'copyGroups', 'copyFeatures',
'muteKerning', 'muteInfo',
'mutedGlyphNames',
'familyName', 'styleName']
def __init__(self):
self.path = None
self.name = None
@ -79,13 +85,14 @@ class SourceDescriptor(SimpleDescriptor):
class InstanceDescriptor(SimpleDescriptor):
"""Simple container for data related to the instance"""
flavor="instance"
_attrs = [ 'path', 'name',
'location', 'familyName',
'styleName', 'postScriptFontName',
'styleMapFamilyName',
'styleMapStyleName',
'kerning', 'info']
flavor = "instance"
_attrs = ['path', 'name',
'location', 'familyName',
'styleName', 'postScriptFontName',
'styleMapFamilyName',
'styleMapStyleName',
'kerning', 'info']
def __init__(self):
self.path = None
self.name = None
@ -102,12 +109,13 @@ class InstanceDescriptor(SimpleDescriptor):
class AxisDescriptor(SimpleDescriptor):
"""Simple container for the axis data"""
flavor="axis"
flavor = "axis"
_attrs = ['tag', 'name', 'maximum', 'minimum', 'default', 'map']
def __init__(self):
self.tag = None # opentype tag for this axis
self.name = None # name of the axis used in locations
self.labelNames = {} # names for UI purposes, if this is not a standard axis,
self.tag = None # opentype tag for this axis
self.name = None # name of the axis used in locations
self.labelNames = {} # names for UI purposes, if this is not a standard axis,
self.minimum = None
self.maximum = None
self.default = None
@ -119,12 +127,13 @@ class BaseDocWriter(object):
axisDescriptorClass = AxisDescriptor
sourceDescriptorClass = SourceDescriptor
instanceDescriptorClass = InstanceDescriptor
def __init__(self, documentPath, documentObject):
self.path = documentPath
self.documentObject = documentObject
self.toolVersion = 3
self.root = ET.Element("designspace")
self.root.attrib['format'] = "%d"%self.toolVersion
self.root.attrib['format'] = "%d" % self.toolVersion
self.root.append(ET.Element("axes"))
self.root.append(ET.Element("sources"))
self.root.append(ET.Element("instances"))
@ -152,7 +161,7 @@ class BaseDocWriter(object):
""" Convert Location dict to a locationElement."""
locElement = ET.Element("location")
if name is not None:
locElement.attrib['name'] = name
locElement.attrib['name'] = name
defaultLoc = self.newDefaultLocation()
validatedLocation = {}
for axisName, axisValue in defaultLoc.items():
@ -162,20 +171,20 @@ class BaseDocWriter(object):
else:
validatedLocation[axisName] = locationObject[axisName]
for dimensionName, dimensionValue in validatedLocation.items():
dimElement = ET.Element('dimension')
dimElement.attrib['name'] = dimensionName
if type(dimensionValue)==tuple:
dimElement.attrib['xvalue'] = self.intOrFloat(dimensionValue[0])
dimElement.attrib['yvalue'] = self.intOrFloat(dimensionValue[1])
else:
dimElement.attrib['xvalue'] = self.intOrFloat(dimensionValue)
locElement.append(dimElement)
dimElement = ET.Element('dimension')
dimElement.attrib['name'] = dimensionName
if type(dimensionValue) == tuple:
dimElement.attrib['xvalue'] = self.intOrFloat(dimensionValue[0])
dimElement.attrib['yvalue'] = self.intOrFloat(dimensionValue[1])
else:
dimElement.attrib['xvalue'] = self.intOrFloat(dimensionValue)
locElement.append(dimElement)
return locElement, validatedLocation
def intOrFloat(self, num):
if int(num) == num:
return "%d"%num
return "%f"%num
return "%d" % num
return "%f" % num
def _addAxis(self, axisObject):
self.axes.append(axisObject)
@ -307,10 +316,12 @@ class BaseDocWriter(object):
glyphElement.append(mastersElement)
return glyphElement
class BaseDocReader(object):
axisDescriptorClass = AxisDescriptor
sourceDescriptorClass = SourceDescriptor
instanceDescriptorClass = InstanceDescriptor
def __init__(self, documentPath, documentObject):
self.path = documentPath
self.documentObject = documentObject
@ -333,15 +344,15 @@ class BaseDocReader(object):
for name in self.documentObject.sources.keys():
paths.append(self.documentObject.sources[name][0].path)
return paths
def newDefaultLocation(self):
loc = {}
for axisDescriptor in self.axes:
loc[axisDescriptor.name] = axisDescriptor.default
return loc
def readAxes(self):
# read the axes elements, including the warp map.
# read the axes elements, including the warp map.
axes = []
for axisElement in self.root.findall(".axes/axis"):
axisObject = self.axisDescriptorClass()
@ -360,7 +371,7 @@ class BaseDocReader(object):
for mapElement in axisElement.findall('map'):
a = float(mapElement.attrib['input'])
b = float(mapElement.attrib['output'])
axisObject.map.append((a,b))
axisObject.map.append((a, b))
self.documentObject.axes.append(axisObject)
self.axisDefaults[axisObject.name] = axisObject.default
@ -483,7 +494,7 @@ class BaseDocReader(object):
::
<info/>
Let's drop support for a different location for the info. Never needed it.
"""
@ -548,9 +559,9 @@ class BaseDocReader(object):
if masterGlyphName is None:
# if we don't read a glyphname, use the one we have
masterGlyphName = glyphName
d = dict( font=fontSourceName,
location=sourceLocation,
glyphName=masterGlyphName)
d = dict(font=fontSourceName,
location=sourceLocation,
glyphName=masterGlyphName)
if glyphSources is None:
glyphSources = []
glyphSources.append(d)
@ -602,10 +613,6 @@ class DesignSpaceDocument(object):
return loc
if __name__ == "__main__":
def test():
@ -661,7 +668,7 @@ if __name__ == "__main__":
>>> i2.familyName = "InstanceFamilyName"
>>> i2.styleName = "InstanceStyleName"
>>> i2.name = "instance.ufo2"
>>> # anisotropic location
>>> # anisotropic location
>>> i2.location = dict(weight=500, width=(400,300))
>>> i2.postScriptFontName = "InstancePostscriptName"
>>> i2.styleMapFamilyName = "InstanceStyleMapFamilyName"
@ -715,10 +722,10 @@ if __name__ == "__main__":
>>> [n.mutedGlyphNames for n in new.sources]
[['A', 'Z'], []]
"""
def _test():
import doctest
doctest.testmod()
print "done"
_test()