Skip to content

Commit

Permalink
Merge pull request #73 from rimas-kudelis/cmdline
Browse files Browse the repository at this point in the history
Add command line script
  • Loading branch information
benkiel authored Sep 27, 2024
2 parents 97a82fa + e5c1540 commit 82a5a45
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Lib/extractor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,66 @@ def extractUFO(
raise ExtractorError(
"There was an error reading the %s file." % format
)


def cmdline():
"""
Extract one ore more fonts to UFO. Installed as command line script
`extractufo`.
Usage: extractufo font [font ...]
"""
import os
from sys import exit
from argparse import ArgumentParser

parser = ArgumentParser(
description="Extract data from font binaries and build UFO objects from them.",
epilog="Each resulting UFO will be saved as FONT_FILE.ufo(z) in the same directory as the original FONT_FILE. "
"If destination file or directory already exists, conversion for that source file will be skipped and the application exit code will indicate an error.",
)
parser.add_argument('FONT_FILE', help='Input font path', nargs="+")
parser.add_argument('-m', '--ufo-module', choices=['ufoLib2', 'defcon'],
help='Select the default library for writing UFOs (default: autodetect, prefer ufoLib2)')
parser.add_argument('-z', '--zip', action="store_true", help="Output UFO ZIP")

args = parser.parse_args()
if args.ufo_module is None:
try:
from ufoLib2 import Font
print("Will use ufoLib2 for UFO output.")
except ImportError:
try:
from defcon import Font
print("Will use defcon for UFO output.")
except ImportError:
print("Either ufoLib2 or, alternatively, defcon library is required to run this command.\nPlease install one of them.")
exit(1)
elif args.ufo_module == 'ufoLib2':
try:
from ufoLib2 import Font
except ImportError:
print("Can't find ufoLib2 installed. Please install it or specify a different UFO library.")
exit(1)
else:
try:
from defcon import Font
except ImportError:
print("Can't find defcon installed. Please install it or specify a different UFO library.")
exit(1)

structure = "zip" if args.zip else "package"
had_write_errors = False
for font_path in args.FONT_FILE:
ufo_path = f"{font_path}.ufo" if not args.zip else f"{font_path}.ufoz"
print(f"Extracting {ufo_path}... ", end="")
if os.path.exists(ufo_path):
print("path already exists, skipping.")
had_write_errors = True
continue
ufo = Font()
extractUFO(font_path, ufo)
ufo.save(ufo_path, structure=structure)
print("done.")

exit(had_write_errors)
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"write_to": 'Lib/extractor/_version.py',
"write_to_template": '__version__ = "{version}"',
},
entry_points={
"console_scripts": [
"extractufo = extractor:cmdline",
]
},
setup_requires=pytest_runner + wheel + ['setuptools_scm'],
tests_require=[
'pytest>=3.0.3',
Expand All @@ -39,6 +44,7 @@
],
extras_require={
"vfb": ["vfbLib>=0.7.1"],
"script": ["ufoLib2"],
},
classifiers=[
"Development Status :: 4 - Beta",
Expand Down

0 comments on commit 82a5a45

Please sign in to comment.