Skip to content

Commit 82a5a45

Browse files
authored
Merge pull request #73 from rimas-kudelis/cmdline
Add command line script
2 parents 97a82fa + e5c1540 commit 82a5a45

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Lib/extractor/__init__.py

+63
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,66 @@ def extractUFO(
7575
raise ExtractorError(
7676
"There was an error reading the %s file." % format
7777
)
78+
79+
80+
def cmdline():
81+
"""
82+
Extract one ore more fonts to UFO. Installed as command line script
83+
`extractufo`.
84+
85+
Usage: extractufo font [font ...]
86+
"""
87+
import os
88+
from sys import exit
89+
from argparse import ArgumentParser
90+
91+
parser = ArgumentParser(
92+
description="Extract data from font binaries and build UFO objects from them.",
93+
epilog="Each resulting UFO will be saved as FONT_FILE.ufo(z) in the same directory as the original FONT_FILE. "
94+
"If destination file or directory already exists, conversion for that source file will be skipped and the application exit code will indicate an error.",
95+
)
96+
parser.add_argument('FONT_FILE', help='Input font path', nargs="+")
97+
parser.add_argument('-m', '--ufo-module', choices=['ufoLib2', 'defcon'],
98+
help='Select the default library for writing UFOs (default: autodetect, prefer ufoLib2)')
99+
parser.add_argument('-z', '--zip', action="store_true", help="Output UFO ZIP")
100+
101+
args = parser.parse_args()
102+
if args.ufo_module is None:
103+
try:
104+
from ufoLib2 import Font
105+
print("Will use ufoLib2 for UFO output.")
106+
except ImportError:
107+
try:
108+
from defcon import Font
109+
print("Will use defcon for UFO output.")
110+
except ImportError:
111+
print("Either ufoLib2 or, alternatively, defcon library is required to run this command.\nPlease install one of them.")
112+
exit(1)
113+
elif args.ufo_module == 'ufoLib2':
114+
try:
115+
from ufoLib2 import Font
116+
except ImportError:
117+
print("Can't find ufoLib2 installed. Please install it or specify a different UFO library.")
118+
exit(1)
119+
else:
120+
try:
121+
from defcon import Font
122+
except ImportError:
123+
print("Can't find defcon installed. Please install it or specify a different UFO library.")
124+
exit(1)
125+
126+
structure = "zip" if args.zip else "package"
127+
had_write_errors = False
128+
for font_path in args.FONT_FILE:
129+
ufo_path = f"{font_path}.ufo" if not args.zip else f"{font_path}.ufoz"
130+
print(f"Extracting {ufo_path}... ", end="")
131+
if os.path.exists(ufo_path):
132+
print("path already exists, skipping.")
133+
had_write_errors = True
134+
continue
135+
ufo = Font()
136+
extractUFO(font_path, ufo)
137+
ufo.save(ufo_path, structure=structure)
138+
print("done.")
139+
140+
exit(had_write_errors)

setup.py

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
"write_to": 'Lib/extractor/_version.py',
3030
"write_to_template": '__version__ = "{version}"',
3131
},
32+
entry_points={
33+
"console_scripts": [
34+
"extractufo = extractor:cmdline",
35+
]
36+
},
3237
setup_requires=pytest_runner + wheel + ['setuptools_scm'],
3338
tests_require=[
3439
'pytest>=3.0.3',
@@ -39,6 +44,7 @@
3944
],
4045
extras_require={
4146
"vfb": ["vfbLib>=0.7.1"],
47+
"script": ["ufoLib2"],
4248
},
4349
classifiers=[
4450
"Development Status :: 4 - Beta",

0 commit comments

Comments
 (0)