Skip to content

Commit

Permalink
Merge pull request #41 from unified-font-object/no-mod-times
Browse files Browse the repository at this point in the history
Add option to not write normalization time stamps
  • Loading branch information
anthrotype committed Oct 30, 2017
2 parents 1ed0111 + d4ca3ed commit b66318e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
46 changes: 26 additions & 20 deletions normalization/ufonormalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from collections import OrderedDict
from io import open
import logging
import sys


"""
Expand All @@ -22,7 +21,7 @@
- things that need to be improved are marked with "# TO DO"
"""

__version__ = "0a2"
__version__ = "0.3"
description = """
UFO Normalizer (version %s):
Expand All @@ -46,6 +45,7 @@ def main(args=None):
parser.add_argument("-v", "--verbose", help="Print more info to console.", action="store_true")
parser.add_argument("-q", "--quiet", help="Suppress all non-error messages.", action="store_true")
parser.add_argument("--float-precision", type=int, default=DEFAULT_FLOAT_PRECISION, help="Round floats to the specified number of decimal places (default is %d). The value -1 means no rounding (i.e. use built-in repr()." % DEFAULT_FLOAT_PRECISION)
parser.add_argument("-m", "--no-mod-times", help="Do not write normalization time stamps.", action="store_true")
args = parser.parse_args(args)

if args.test:
Expand Down Expand Up @@ -73,13 +73,15 @@ def main(args=None):
else:
parser.error("float precision must be >= 0 or -1 (no round).")

writeModTimes = not args.no_mod_times

message = 'Normalizing "%s".'
if not onlyModified:
message += " Processing all files."
log.info(message, os.path.basename(inputPath))
start = time.time()
normalizeUFO(inputPath, outputPath=outputPath, onlyModified=onlyModified,
floatPrecision=floatPrecision)
floatPrecision=floatPrecision, writeModTimes=writeModTimes)
runtime = time.time() - start
log.info("Normalization complete (%.4f seconds).", runtime)

Expand Down Expand Up @@ -153,14 +155,16 @@ def tounicode(s, encoding='ascii', errors='strict'):
tostr = tounicode


class UFONormalizerError(Exception): pass
class UFONormalizerError(Exception):
pass


DEFAULT_FLOAT_PRECISION = 10
FLOAT_FORMAT = "%%.%df" % DEFAULT_FLOAT_PRECISION


def normalizeUFO(ufoPath, outputPath=None, onlyModified=True, floatPrecision=DEFAULT_FLOAT_PRECISION):
def normalizeUFO(ufoPath, outputPath=None, onlyModified=True,
floatPrecision=DEFAULT_FLOAT_PRECISION, writeModTimes=True):
global FLOAT_FORMAT
if floatPrecision is None:
# use repr() and don't round floats
Expand Down Expand Up @@ -225,10 +229,11 @@ def normalizeUFO(ufoPath, outputPath=None, onlyModified=True, floatPrecision=DEF
if subpathExists(ufoPath, "layercontents.plist"):
normalizeLayerContentsPlist(ufoPath, modTimes)
# update the mod time storage, write, normalize
storeModTimes(fontLib, modTimes)
subpathWritePlist(fontLib, ufoPath, "lib.plist")
if subpathExists(ufoPath, "lib.plist"):
normalizeLibPlist(ufoPath)
if writeModTimes:
storeModTimes(fontLib, modTimes)
subpathWritePlist(fontLib, ufoPath, "lib.plist")
if subpathExists(ufoPath, "lib.plist"):
normalizeLibPlist(ufoPath)

# ------
# Layers
Expand Down Expand Up @@ -760,7 +765,7 @@ def _normalizeGlifNote(element, writer):
if not value:
return
if not value.strip():
return
return
writer.beginElement("note")
writer.text(value)
writer.endElement("note")
Expand Down Expand Up @@ -1045,7 +1050,7 @@ def _normalizeColorString(value):
except ValueError:
return
if any(x < 0 or x > 1 for x in (r, g, b, a)):
return
return
color = (xmlConvertFloat(i) for i in (r, g, b, a))
return ",".join(color)

Expand Down Expand Up @@ -1167,7 +1172,8 @@ def text(self, text):
if not paragraph:
paragraphs.append("")
else:
paragraph = textwrap.wrap(paragraph.rstrip(),
paragraph = textwrap.wrap(
paragraph.rstrip(),
width=xmlTextMaxLineLength,
expand_tabs=False,
replace_whitespace=False,
Expand Down Expand Up @@ -1225,12 +1231,12 @@ def propertyListObject(self, data):
elif isinstance(data, (int, long)):
self._plistInt(data)
elif isinstance(data, float):
dataStr = xmlConvertFloat(data)
try:
data = int(dataStr)
self._plistInt(data)
except ValueError:
self._plistFloat(data)
dataStr = xmlConvertFloat(data)
try:
data = int(dataStr)
self._plistInt(data)
except ValueError:
self._plistFloat(data)
elif isinstance(data, plistlib.Data):
self._plistData(data)
elif isinstance(data, datetime.datetime):
Expand Down Expand Up @@ -1633,8 +1639,8 @@ def handleClash1(userName, existing=[], prefix="", suffix=""):
prefixLength = len(prefix)
suffixLength = len(suffix)
if prefixLength + len(userName) + suffixLength + 15 > maxFileNameLength:
l = (prefixLength + len(userName) + suffixLength + 15)
sliceLength = maxFileNameLength - l
length = (prefixLength + len(userName) + suffixLength + 15)
sliceLength = maxFileNameLength - length
userName = userName[:sliceLength]
finalName = None
# try to add numbers to create a unique name
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from setuptools import setup
from normalization.ufonormalizer import __version__

setup(name="ufonormalizer",
version="0.1",
version=__version__,
description="Example implementation of a UFO normalizer",
author="Tal Leming",
email="tal@typesupply.com",
Expand Down

0 comments on commit b66318e

Please sign in to comment.