Improve pylint warnings

Mostly fixes #7
This commit is contained in:
Miepee 2022-02-02 20:58:38 +01:00 committed by Riesi
parent 0d169ba5a0
commit ffcb750e79
1 changed files with 108 additions and 93 deletions

View File

@ -22,100 +22,113 @@ import ntpath
import subprocess import subprocess
import sys import sys
import os import os
import platform
# Dependencies: (optional) git, inkscape, python 3.9 # Dependencies: (optional) git, inkscape, python 3.9
# deletes raster graphics with given name # deletes raster graphics with given name
def delete_graphics(name): def delete_graphics(name):
for f in name: """Delete each PNG file in name"""
for d in glob.glob("./png/**/"+str(f)+".png", recursive=True): for file in name:
print('deleting: ' +str(d)) for directory in glob.glob(f"./png/**/{file}.png", recursive=True):
os.remove(d) print(f"deleting: {directory}")
os.remove(directory)
# rasters SVGs via Inkscape # rasters SVGs via Inkscape
def raster_graphics(files): def raster_graphics(files):
"""Rasters each file in files via Inkscape"""
# TODO: make this adjustable via option/parameter # TODO: make this adjustable via option/parameter
sizes = [72, 512, 1024] sizes = [72, 512, 1024]
frogs = len(files)-1 frogs = len(files)-1
for f in files: for file in files:
name = ntpath.basename(f).replace(".svg", "") name = ntpath.basename(file).replace(".svg", "")
print('\n------------------------------\n'+ str(frogs) + ' remaining...\nRastering ' + str(name)+'\n') print('\n------------------------------')
print(f"{frogs} remaining...\nRastering {name}\n")
frogs = frogs - 1 frogs = frogs - 1
for s in sizes: for size in sizes:
# make sure the subdirectories exist # make sure the subdirectories exist
raster_path = "./png/"+str(s)+"/"+str(os.path.dirname(f.removeprefix('svg/').removesuffix('.svg'))) raster_path = f"./png/{size}/{os.path.dirname(file.removeprefix('svg/').removesuffix('.svg'))}"
if not(os.path.exists(raster_path) ): if not os.path.exists(raster_path) :
os.makedirs(raster_path ,exist_ok=True) os.makedirs(raster_path ,exist_ok=True)
# invoke Inkscape to raster the given vector graphics # invoke Inkscape to raster the given vector graphics
subprocess.run(["inkscape", f, "-C", "-h", str(s), "--export-filename="+str(raster_path)+"/"+name+".png"],timeout=30) subprocess.run(["inkscape", file, "-C", "-h", str(size),
f"--export-filename={raster_path}/{name}.png"],timeout=30, check=True)
# git add given files # git add given files
def git_add_raster(files): def git_add_raster(files):
for f in files: """Git add's each file in files"""
f = f.removeprefix('svg/').removesuffix('.svg') for file in files:
stream = os.popen('git add ./png/**/'+str(f)+'.png') file = file.removeprefix('svg/').removesuffix('.svg')
stream = os.popen(f"git add ./png/**/{file}.png")
print(stream.read()) print(stream.read())
# git commit given files # git commit given files
def git_commit_raster(): def git_commit_raster():
"""Creates a new autogenerated git commit with the PNGs"""
stream = os.popen('git commit -m "[png] generate PNGs"') stream = os.popen('git commit -m "[png] generate PNGs"')
print(stream.read()) print(stream.read())
# create a new tag # create a new tag
def create_tag(): def create_tag():
"""Creates a new git tag and names it appropriately"""
# get tags # get tags
stream = os.popen('git tag -l "auto-v*"') stream = os.popen('git tag -l "auto-v*"')
output = stream.read() output = stream.read()
tags = output.split('\n') tags = output.split('\n')
tags.remove('') tags.remove('')
# create new tag # create new tag
stream = os.popen('git tag auto-v'+str(len(tags))) stream = os.popen(f"git tag auto-v{len(tags)}")
print(stream.read()) print(stream.read())
# print help message # print help message
def print_help(): def print_help():
"""Prints gen_png's help output to console"""
print("gen_png.py [OPTION]") print("gen_png.py [OPTION]")
print("") print("")
print("-h, --help Shows this help message.") print("-h, --help Shows this help message.")
print("-a, --all Regenerates all PNGs.") 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("-s, --specific [FILENAME]... " +
print("-g, --git Regenerates all PNGs and creates a git commit for them. Requires 'git' to be installed and located in PATH.") "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
# # -------------------------------#
files = list() def main():
"""Main Method"""
files = []
# if there are no arguments (or help parameter was specified), print help and exit # 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': if len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
print_help() print_help()
exit(1) 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': elif sys.argv[1] == '-g' or sys.argv[1] == '--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
gitCommand = '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'
if sys.platform == "win32": if sys.platform == "win32":
os.environ["COMSPEC"] = 'powershell' os.environ["COMSPEC"] = 'powershell'
stream = os.popen(gitCommand + ' | Select-String svg/') stream = os.popen(f"{git_command} | Select-String svg/")
else: else:
stream = os.popen(gitCommand + ' | grep svg/') stream = os.popen(f"{git_command} | grep svg/")
output = stream.read() output = stream.read()
files = output.split('\n') files = output.split('\n')
files.remove('') files.remove('')
if len(files) == 0: if len(files) == 0:
print('No SVGs to regenerate!') print('No SVGs to regenerate!')
exit() sys.exit(1)
additions = [s.removeprefix('A\t') for s in files if 'A\t' in s] additions = [s.removeprefix('A\t') for s in files if 'A\t' in s]
modifications = [s.removeprefix('M\t') for s in files if 'M\t' in s] modifications = [s.removeprefix('M\t') for s in files if 'M\t' in s]
deleted = [s.removeprefix('D\tsvg/').removesuffix('.svg') for s in files if 'D\t' in s] deleted = [s.removeprefix('D\tsvg/').removesuffix('.svg') for s in files if 'D\t' in s]
renamed = [s.removeprefix('R100\t').split('\t') for s in files if 'R100\t' in s] renamed = [s.removeprefix('R100\t').split('\t') for s in files if 'R100\t' in s]
# also delete the renamed files and regenerate with the new name # also delete the renamed files and regenerate with the new name
for r in renamed: for ren in renamed:
deleted.append(r[0].removeprefix('svg/').removesuffix('.svg')) deleted.append(ren[0].removeprefix('svg/').removesuffix('.svg'))
modifications.append(r[1]) modifications.append(ren[1])
#print('A:' + str(additions)) #print('A:' + str(additions))
#print('M:' + str(modifications)) #print('M:' + str(modifications))
@ -136,21 +149,23 @@ elif sys.argv[1] == '-g' or sys.argv[1] == '--git':
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': elif sys.argv[1] == '-a' or sys.argv[1] == '--all':
files = [f for f in glob.glob("svg/**/*.svg", recursive=True)] files = list(glob.glob('svg/**/*.svg', recursive=True))
raster_graphics(files) raster_graphics(files)
# 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': elif sys.argv[1] == '-s' or sys.argv[1] == '--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!")
exit(1) sys.exit(1)
for f in sys.argv: for file in sys.argv:
f = f.removeprefix('svg/').removesuffix('.svg') file = file.removeprefix('svg/').removesuffix('.svg')
if os.path.exists("svg/"+f+".svg"): if os.path.exists(f"svg/{file}.svg"):
files.append("svg/"+f+".svg") files.append(f"svg/{file}.svg")
raster_graphics(files) raster_graphics(files)
# unknown option # unknown option
else: else:
print("Unknown option!") print("Unknown option!")
print("Try 'gen_png.py --help' for more information.") print("Try 'gen_png.py --help' for more information.")
if __name__ == "__main__":
main()