[util] refactor to use argparse and add custom resolution option

This commit is contained in:
Stefan Riesenberger 2022-02-04 12:21:29 +01:00
parent ffcb750e79
commit 9e2928c234
1 changed files with 29 additions and 33 deletions

View File

@ -17,11 +17,12 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
########################################################################### ###########################################################################
import argparse
import glob import glob
import ntpath import ntpath
import os
import subprocess import subprocess
import sys import sys
import os
# Dependencies: (optional) git, inkscape, python 3.9 # Dependencies: (optional) git, inkscape, python 3.9
@ -34,10 +35,9 @@ def delete_graphics(name):
os.remove(directory) os.remove(directory)
# rasters SVGs via Inkscape # rasters SVGs via Inkscape
def raster_graphics(files): def raster_graphics(files, sizes = None):
"""Rasters each file in files via Inkscape""" """Rasters each file in files via Inkscape"""
# TODO: make this adjustable via option/parameter sizes = [72, 512, 1024] if sizes is None else sizes
sizes = [72, 512, 1024]
frogs = len(files)-1 frogs = len(files)-1
for file in files: for file in files:
name = ntpath.basename(file).replace(".svg", "") name = ntpath.basename(file).replace(".svg", "")
@ -79,32 +79,32 @@ def create_tag():
stream = os.popen(f"git tag auto-v{len(tags)}") stream = os.popen(f"git tag auto-v{len(tags)}")
print(stream.read()) print(stream.read())
# print help message
def print_help():
"""Prints gen_png's help output to console"""
print("gen_png.py [OPTION]")
print("")
print("-h, --help Shows this help message.")
print("-a, --all Regenerates all PNGs.")
print("-s, --specific [FILENAME]... " +
"Regenerate the PNGs for each FILENAME in the \"svg\" folder. " +
"The '.svg' suffix is optional.")
print("-g, --git " +
"Regenerates all PNGs and creates a git commit for them. " +
"Requires 'git' to be installed and located in PATH.")
# -------------------------------# # -------------------------------#
# main section # main section
# -------------------------------# # -------------------------------#
def main(): def main():
"""Main Method""" """Main Method"""
# parse arguments
parser = argparse.ArgumentParser(description='Automate rasterization of SVGs to PNGs.')
mutex_group = parser.add_mutually_exclusive_group(required=True)
mutex_group.add_argument("-a", "--all", action="store_true", help="Generate PNGs of all SVGs.")
mutex_group.add_argument("-s", "--specific", metavar='S', type=str, nargs="+",
help="Generate PNGs for each S in the \"svg\" folder. " +
"The '.svg' suffix is optional.")
parser.add_argument("-r", "--resolution", metavar='R', type=int, nargs="+",
help="Custom resolutions that will be generated instead of the defaults.")
mutex_group.add_argument("-g", "--git", action="store_true",
help="Generates PNGs of SVGs that changed since last auto tag and creates a git commit with them. " +
"Requires 'git' to be installed and located in PATH.")
# this will error out on invalid argument configurations
args = parser.parse_args()
sizes = args.resolution # this is None when no argument is supplied
files = [] files = []
# if there are no arguments (or help parameter was specified), print help and exit
if len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
print_help()
sys.exit(1)
# if "git" parameter was specified, regenerate since last tag + create git commit # if "git" parameter was specified, regenerate since last tag + create git commit
elif sys.argv[1] == '-g' or sys.argv[1] == '--git': if args.git:
# get modified, added, renamed, deleted SVGs since last tag # get modified, added, renamed, deleted SVGs since last tag
# call this on windows a) with PS for the $() and b) replace grep with PS' equivalent # call this on windows a) with PS for the $() and b) replace grep with PS' equivalent
git_command = 'git diff --name-status $(git describe --tags --abbrev=0 --match "auto-v*") HEAD' git_command = 'git diff --name-status $(git describe --tags --abbrev=0 --match "auto-v*") HEAD'
@ -138,21 +138,21 @@ def main():
delete_graphics(deleted) delete_graphics(deleted)
git_add_raster(deleted) git_add_raster(deleted)
print('\nAdditions:') print('\nAdditions:')
raster_graphics(additions) raster_graphics(additions, sizes)
git_add_raster(additions) git_add_raster(additions)
print('\nModifications:') print('\nModifications:')
raster_graphics(modifications) raster_graphics(modifications, sizes)
git_add_raster(modifications) git_add_raster(modifications)
# commit and tag # commit and tag
git_commit_raster() git_commit_raster()
create_tag() create_tag()
# if "all" parameter was specified, raster all svgs including subfolders # if "all" parameter was specified, raster all svgs including subfolders
elif sys.argv[1] == '-a' or sys.argv[1] == '--all': if args.all:
files = list(glob.glob('svg/**/*.svg', recursive=True)) files = list(glob.glob('svg/**/*.svg', recursive=True))
raster_graphics(files) raster_graphics(files, sizes)
# if "specific" parameter was specified, only render those # if "specific" parameter was specified, only render those
elif sys.argv[1] == '-s' or sys.argv[1] == '--specific': if args.specific:
if len(sys.argv) == 2: if len(sys.argv) == 2:
print("Please specify at least one file!") print("Please specify at least one file!")
sys.exit(1) sys.exit(1)
@ -161,11 +161,7 @@ def main():
file = file.removeprefix('svg/').removesuffix('.svg') file = file.removeprefix('svg/').removesuffix('.svg')
if os.path.exists(f"svg/{file}.svg"): if os.path.exists(f"svg/{file}.svg"):
files.append(f"svg/{file}.svg") files.append(f"svg/{file}.svg")
raster_graphics(files) raster_graphics(files, sizes)
# unknown option
else:
print("Unknown option!")
print("Try 'gen_png.py --help' for more information.")
if __name__ == "__main__": if __name__ == "__main__":
main() main()