Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command line script #73

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ...]
"""
Comment on lines +81 to +86
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, not sure if it makes sense to keep this comment now that we're using the argument parser.

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

help="Output UFOZ"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, "UFO ZIP" is actually how the specification calls it. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I can't remember, and I am used to thinking UFOZ, but fine with UFO ZIP

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From https://unifiedfontobject.org/versions/ufo3/#storage-formats:

UFO has two possible storage formats. The first, “UFO Package”, is a multi-file, file system directory structure. The second, “UFO ZIP”, is a ZIP archive containing the same directory structure as defined for UFO Package.

I'll leave this as-is then.


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
Loading