aboutsummaryrefslogtreecommitdiff
path: root/save-webfonts
blob: 8a7e2302428521dbdd2c80aa0f5be2a1a5b6427c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# File: /usr/bin/save-webfonts
# Location: save-webfonts package
# Author: bgstack15
# Startdate: 2021-04-02 19:34
# SPDX-License-Identifier: CC-BY-SA 4.0
# Title: Save Webfonts For a Given URL
# Purpose: whitelisting a page's webfonts by downloading them for current user
# Usage: run save-webfonts --help
# Reference:
#    https://gitlab.com/bgstack15/s2s
# Improve:
#    Make front-end handle harfiles
#    write man page in .txt format
#    write rpm
#    write dpkg
#    publish to COPR and OBS
# Dependencies:
#    /usr/libexec/savewebfonts/savewebfonts_lib.py

from sys import path
from argparse import ArgumentParser
from os.path import expanduser
path.append("/usr/libexec/savewebfonts") # for when running as a system package
path.append(".") # for when running this straight from a git-cloned directory
try:
   from savewebfonts_lib import *
except ModuleNotFoundError as e:
   raise Exception("Unable to find savewebfonts_lib.py library. Check /usr/libexec/savewebfonts/ or current directory.")

savewebfonts_version = "2021-04-02a"

parser = ArgumentParser(description="Download webfonts for a URL so browsers do not need to fetch them")
parser.add_argument("-d","--debug",nargs='?', default=0, type=int, choices=range(0,11), help="Set debug level")
parser.add_argument("-v","--version", action="version", version="%(prog)s " + savewebfonts_version)
g_dryrun = parser.add_mutually_exclusive_group()
g_dryrun.add_argument("-n","--dryrun", action="store_true", help="Make no changes (default)")
g_dryrun.add_argument("-a","--apply", action="store_true", help="Actually make changes")
parser.add_argument("-o","--outdir", help="Location of font directory. Default is ~/.local/share/fonts",default=expanduser("~/.local/share/fonts"))
parser.add_argument("url", nargs='+', help="URLs whose webfonts should be downloaded")
g_wait = parser.add_mutually_exclusive_group()
g_wait.add_argument("-w","--wait", action="store_true", help="Wait at end for Return key")
g_wait.add_argument("-N","--no-wait","--nowait", action="store_true", help="Do not wait. Default.", default=True)
g_convert = parser.add_mutually_exclusive_group()
g_convert.add_argument("-c","--convert", action="store_true", help="Convert all downloaded woff and woff2 files to ttf. Default.")
g_convert.add_argument("--nc","--no-convert","--noconvert", action="store_true", help="Do not convert all downloaded woff and woff2 files to ttf.")
parser.add_argument("--eotbin", default="eot2ttf", help="Path to eot2ttf binary. Default is \"eot2ttf\"")

# pull useful values out of the argparse entry
args = parser.parse_args()
config = swf_config()
config.debuglevel=0
if args.debug is None:
   config.debuglevel = 10 # if -d without a number, then 10
elif args.debug:
   config.debuglevel = args.debug
outdir = args.outdir
config.dryrun = args.dryrun or not args.apply # the mutually exclusive group handles this OK
config.wait = args.wait
urls = args.url
config.convert = not args.nc
config.eot2ttf_binary = args.eotbin
#eprint(args)
#eprint(f"dryrun {config.dryrun}")
#eprint(f"debuglevel {config.debuglevel}")
#eprint(f"outdir {outdir}")
#eprint(f"wait {wait}")
#eprint(f"urls {urls}")
#eprint(f"convert {config.convert}")

for url in urls:
   print(f"Saving webfonts for {url} ...")
   whitelist_page(url, outdir, config)

if config.wait:
   input("Press Enter to continue...")
bgstack15