[designspace] refactor
- remove unused import - use snake_case instead of camelCase - add two empty lines between module-level definitions - rename 'elt' variable to 'element' - don't make extra dict copy in _load_axis
This commit is contained in:
parent
558e5e172d
commit
66c6662572
@ -1,6 +1,5 @@
|
|||||||
"""Rudimentary support for loading MutatorMath .designspace files."""
|
"""Rudimentary support for loading MutatorMath .designspace files."""
|
||||||
from __future__ import print_function, division, absolute_import
|
from __future__ import print_function, division, absolute_import
|
||||||
import collections
|
|
||||||
from fontTools.misc.py23 import *
|
from fontTools.misc.py23 import *
|
||||||
try:
|
try:
|
||||||
import xml.etree.cElementTree as ET
|
import xml.etree.cElementTree as ET
|
||||||
@ -11,7 +10,8 @@ __all__ = ['load', 'loads']
|
|||||||
|
|
||||||
namespaces = {'xml': '{http://www.w3.org/XML/1998/namespace}'}
|
namespaces = {'xml': '{http://www.w3.org/XML/1998/namespace}'}
|
||||||
|
|
||||||
def _xmlParseLocation(et):
|
|
||||||
|
def _xml_parse_location(et):
|
||||||
loc = {}
|
loc = {}
|
||||||
for dim in et.find('location'):
|
for dim in et.find('location'):
|
||||||
assert dim.tag == 'dimension'
|
assert dim.tag == 'dimension'
|
||||||
@ -21,45 +21,49 @@ def _xmlParseLocation(et):
|
|||||||
loc[name] = value
|
loc[name] = value
|
||||||
return loc
|
return loc
|
||||||
|
|
||||||
def _loadItem(et):
|
|
||||||
|
def _load_item(et):
|
||||||
item = dict(et.attrib)
|
item = dict(et.attrib)
|
||||||
for elt in et:
|
for element in et:
|
||||||
if elt.tag == 'location':
|
if element.tag == 'location':
|
||||||
value = _xmlParseLocation(et)
|
value = _xml_parse_location(et)
|
||||||
else:
|
else:
|
||||||
value = {}
|
value = {}
|
||||||
if 'copy' in elt.attrib:
|
if 'copy' in element.attrib:
|
||||||
value['copy'] = bool(int(elt.attrib['copy']))
|
value['copy'] = bool(int(element.attrib['copy']))
|
||||||
# TODO load more?!
|
# TODO load more?!
|
||||||
item[elt.tag] = value
|
item[element.tag] = value
|
||||||
return item
|
return item
|
||||||
|
|
||||||
def _xmlParseAxisOrMap(elt):
|
|
||||||
|
def _xml_parse_axis_or_map(element):
|
||||||
dic = {}
|
dic = {}
|
||||||
for name in elt.attrib:
|
for name in element.attrib:
|
||||||
if name in ['name', 'tag']:
|
if name in ['name', 'tag']:
|
||||||
dic[name] = elt.attrib[name]
|
dic[name] = element.attrib[name]
|
||||||
else:
|
else:
|
||||||
dic[name] = float(elt.attrib[name])
|
dic[name] = float(element.attrib[name])
|
||||||
return dic
|
return dic
|
||||||
|
|
||||||
def _loadAxis(et):
|
|
||||||
item = dict(_xmlParseAxisOrMap(et))
|
def _load_axis(et):
|
||||||
|
item = _xml_parse_axis_or_map(et)
|
||||||
maps = []
|
maps = []
|
||||||
labelnames = {}
|
labelnames = {}
|
||||||
for elt in et:
|
for element in et:
|
||||||
assert elt.tag in ['labelname', 'map']
|
assert element.tag in ['labelname', 'map']
|
||||||
if elt.tag == 'labelname':
|
if element.tag == 'labelname':
|
||||||
lang = elt.attrib["{0}lang".format(namespaces['xml'])]
|
lang = element.attrib["{0}lang".format(namespaces['xml'])]
|
||||||
labelnames[lang] = elt.text
|
labelnames[lang] = element.text
|
||||||
elif elt.tag == 'map':
|
elif element.tag == 'map':
|
||||||
maps.append(_xmlParseAxisOrMap(elt))
|
maps.append(_xml_parse_axis_or_map(element))
|
||||||
if labelnames:
|
if labelnames:
|
||||||
item['labelname'] = labelnames
|
item['labelname'] = labelnames
|
||||||
if maps:
|
if maps:
|
||||||
item['map'] = maps
|
item['map'] = maps
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
def _load(et):
|
def _load(et):
|
||||||
designspace = {}
|
designspace = {}
|
||||||
ds = et.getroot()
|
ds = et.getroot()
|
||||||
@ -68,25 +72,26 @@ def _load(et):
|
|||||||
if axes_element is not None:
|
if axes_element is not None:
|
||||||
axes = []
|
axes = []
|
||||||
for et in axes_element:
|
for et in axes_element:
|
||||||
axes.append(_loadAxis(et))
|
axes.append(_load_axis(et))
|
||||||
designspace['axes'] = axes
|
designspace['axes'] = axes
|
||||||
|
|
||||||
sources_element = ds.find('sources')
|
sources_element = ds.find('sources')
|
||||||
if sources_element is not None:
|
if sources_element is not None:
|
||||||
sources = []
|
sources = []
|
||||||
for et in sources_element:
|
for et in sources_element:
|
||||||
sources.append(_loadItem(et))
|
sources.append(_load_item(et))
|
||||||
designspace['sources'] = sources
|
designspace['sources'] = sources
|
||||||
|
|
||||||
instances_element = ds.find('instances')
|
instances_element = ds.find('instances')
|
||||||
if instances_element is not None:
|
if instances_element is not None:
|
||||||
instances = []
|
instances = []
|
||||||
for et in instances_element:
|
for et in instances_element:
|
||||||
instances.append(_loadItem(et))
|
instances.append(_load_item(et))
|
||||||
designspace['instances'] = instances
|
designspace['instances'] = instances
|
||||||
|
|
||||||
return designspace
|
return designspace
|
||||||
|
|
||||||
|
|
||||||
def load(filename):
|
def load(filename):
|
||||||
"""Load designspace from a file name or object.
|
"""Load designspace from a file name or object.
|
||||||
Returns a dictionary containing three items:
|
Returns a dictionary containing three items:
|
||||||
@ -96,6 +101,7 @@ def load(filename):
|
|||||||
"""
|
"""
|
||||||
return _load(ET.parse(filename))
|
return _load(ET.parse(filename))
|
||||||
|
|
||||||
|
|
||||||
def loads(string):
|
def loads(string):
|
||||||
"""Load designspace from a string."""
|
"""Load designspace from a string."""
|
||||||
return _load(ET.fromstring(string))
|
return _load(ET.fromstring(string))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user