Skip to content

Commit

Permalink
Split subcommands, add --group and --csv options
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Feb 29, 2024
1 parent e6b7dd3 commit 9c72e91
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 190 deletions.
190 changes: 0 additions & 190 deletions Lib/shaperglot/cli.py

This file was deleted.

51 changes: 51 additions & 0 deletions Lib/shaperglot/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import argparse
import sys

from shaperglot.cli.check import check
from shaperglot.cli.report import report
from shaperglot.cli.describe import describe


def main(args=None):
if args is None:
args = sys.argv[1:]
parser = argparse.ArgumentParser(
description="Check a font file's language coverage"
)
subparsers = parser.add_subparsers(help='sub-commands')

parser_describe = subparsers.add_parser('describe', help=describe.__doc__)
parser_describe.add_argument(
'lang', metavar='LANG', help='an ISO639-3 language code'
)
parser_describe.set_defaults(func=describe)

parser_check = subparsers.add_parser('check', help=check.__doc__)
parser_check.add_argument('--verbose', '-v', action='count')
parser_check.add_argument('font', metavar='FONT', help='the font file')
parser_check.add_argument(
'lang', metavar='LANG', help='one or more ISO639-3 language codes', nargs="+"
)
parser_check.set_defaults(func=check)

parser_report = subparsers.add_parser('report', help=report.__doc__)
parser_report.add_argument('font', metavar='FONT', help='the font file')
parser_report.add_argument('--verbose', '-v', action='count')
parser_report.add_argument('--csv', action='store_true', help="Output as CSV")
parser_report.add_argument(
'--group', action='store_true', help="Group by success/failure"
)
parser_report.add_argument(
'--filter', type=str, help="Regular expression to filter languages"
)
parser_report.set_defaults(func=report)

options = parser.parse_args(args)
if not hasattr(options, "func"):
parser.print_help()
sys.exit(1)
options.func(options)


if __name__ == '__main__': # pragma: no cover
main()
28 changes: 28 additions & 0 deletions Lib/shaperglot/cli/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from shaperglot.checker import Checker
from shaperglot.languages import Languages


def check(options):
"""Check a particular language or languages are supported"""
checker = Checker(options.font)
langs = Languages()
for lang in options.lang:
if lang not in langs:
print(f"Language '{options.lang}' not known")
continue

results = checker.check(langs[lang])

if results.is_unknown:
print(f"Cannot determine whether font supports language '{lang}'")
elif results.is_success:
print(f"Font supports language '{lang}'")
else:
print(f"Font does not fully support language '{lang}'")

if options.verbose and options.verbose > 1:
for message in results:
print(f" * {message}")
elif options.verbose or not results.is_success:
for message in results.fails:
print(f" * {message}")
34 changes: 34 additions & 0 deletions Lib/shaperglot/cli/describe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
from textwrap import fill

from shaperglot.languages import Languages

def describe(options):
"""Describe the checks shaperglot will perform to determine support for a given language"""
langs = Languages()
if options.lang not in langs:
maybe = langs.disambiguate(options.lang)
if len(maybe) == 1:
lang = langs[maybe[0]]
print(f"Assuming you meant {maybe[0]} ({lang['full_name']}).")
elif len(maybe) > 1:
print(f"Language '{options.lang}' not known", end="")
print("; try one of: " + ", ".join(maybe))
return
else:
print(f"Language '{options.lang}' not known", end="")
print("")
return
else:
lang = langs[options.lang]
print(f"To test for {lang['name']} support, shaperglot will:")
for shaperglot_check in lang.get("shaperglot_checks", []):
print(
fill(
"ensure " + shaperglot_check.describe(),
initial_indent=" * ",
subsequent_indent=" ",
width=os.get_terminal_size()[0] - 2,
)
)

Loading

0 comments on commit 9c72e91

Please sign in to comment.