Skip to content

Instantly share code, notes, and snippets.

@ties
Last active March 6, 2019 09:46
Show Gist options
  • Select an option

  • Save ties/35986e025ec75c53b5fe40cc3e66cfda to your computer and use it in GitHub Desktop.

Select an option

Save ties/35986e025ec75c53b5fe40cc3e66cfda to your computer and use it in GitHub Desktop.
"""
A utility script to copy the windows truetype fonts as recommended in [0].
[0]: https://pandasauce.org/post/linux-fonts/
"""
import argparse
import logging
import os
import shutil
import sys
from typing.io import TextIO
LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)
FONTS = [
'arialbd.ttf', 'ARIALNB.TTF', 'ariblk.ttf', 'consolai.ttf', 'courbi.ttf',
'georgiai.ttf', 'segoeprb.ttf', 'segoeuib.ttf', 'segoeuiz.ttf', 'timesbi.ttf',
'trebucbi.ttf', 'verdanai.ttf', 'arialbi.ttf', 'ARIALNI.TTF', 'comicbd.ttf',
'consola.ttf', 'couri.ttf', 'georgia.ttf', 'segoepr.ttf', 'segoeuii.ttf',
'tahomabd.ttf', 'timesi.ttf', 'trebucit.ttf', 'verdana.ttf', 'ariali.ttf',
'ARIALN.TTF', 'comic.ttf', 'consolaz.ttf', 'cour.ttf', 'georgiaz.ttf',
'segoescb.ttf', 'segoeuil.ttf', 'tahoma.ttf', 'times.ttf', 'trebuc.ttf',
'verdanaz.ttf', 'ARIALNBI.TTF', 'arial.ttf', 'consolab.ttf', 'courbd.ttf',
'georgiab.ttf', 'impact.ttf', 'segoesc.ttf', 'segoeui.ttf', 'timesbd.ttf',
'trebucbd.ttf', 'verdanab.ttf', 'webdings.ttf']
"""
Utilities: Directories as argparse arguments
https://stackoverflow.com/questions/11415570/directory-path-types-with-argparse
"""
class readable_dir(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
prospective_dir=values
if not os.path.isdir(prospective_dir):
raise argparse.ArgumentTypeError(
"{0} is not a valid path".format(prospective_dir))
if os.access(prospective_dir, os.R_OK):
setattr(namespace,self.dest,prospective_dir)
else:
raise argparse.ArgumentTypeError(
"{0} is not a readable directory".format(prospective_dir))
class writable_dir(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
prospective_dir=values
if not os.path.isdir(prospective_dir):
raise argparse.ArgumentTypeError(
"{0} is not a valid path".format(prospective_dir))
if os.access(prospective_dir, os.R_OK):
setattr(namespace,self.dest,prospective_dir)
else:
raise argparse.ArgumentTypeError(
"{0} is not a writable directory".format(prospective_dir))
def copy_fonts(root_dir: str, target_dir: str):
# Validate that all files are in source directory:
missing = set(f for f in FONTS
if not os.path.isfile(os.path.join(root_dir, f)))
if missing:
for font in missing:
LOG.info('%s is missing in source dir', font)
# Experimental threshold on Windows 18.09
if len(missing) > 4:
LOG.error("Exiting - missing font.")
sys.exit(1)
for font in FONTS:
if font not in missing:
shutil.copy(os.path.join(root_dir, font), target_dir)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('indir', action=readable_dir)
parser.add_argument('outdir', action=writable_dir,
default=os.path.expanduser('~/.local/share/fonts/'))
args = parser.parse_args()
logging.basicConfig()
copy_fonts(args.indir, args.outdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment