1
0
mirror of https://github.com/s00500/ESPUI.git synced 2025-07-06 09:40:20 +00:00

'Refactored by Sourcery' (#2)

Co-authored-by: Sourcery AI <>
This commit is contained in:
sourcery-ai[bot]
2023-09-13 17:46:02 +02:00
committed by GitHub
parent c7a9438149
commit f41f80e880

View File

@ -47,21 +47,25 @@ def get_context(infile, outfile):
indir = os.path.dirname(infile) indir = os.path.dirname(infile)
if os.path.isdir(outfile): if os.path.isdir(outfile):
outdir = os.path.realpath(outfile) outdir = os.path.realpath(outfile)
outfilename = "%s%s%s.h" % (dir, name.capitalize(), type.upper()) outfilename = f"{dir}{name.capitalize()}{type.upper()}.h"
outfile = os.path.realpath(os.path.sep.join([outdir, outfilename])) outfile = os.path.realpath(os.path.sep.join([outdir, outfilename]))
else: else:
outfile = os.path.realpath(outfile) outfile = os.path.realpath(outfile)
outdir = os.path.dirname(outfile) outdir = os.path.dirname(outfile)
outfilename = os.path.basename(outfile) outfilename = os.path.basename(outfile)
minifile = re.sub('\.([^.]+)$', '.min.\\1', infile) if not ".min." in infile else infile minifile = (
constant = '%s_%s' % (type.upper(), name.upper()) re.sub('\.([^.]+)$', '.min.\\1', infile)
if ".min." not in infile
else infile
)
constant = f'{type.upper()}_{name.upper()}'
return locals() return locals()
def perform_gzip(c): def perform_gzip(c):
compressed = gzip.compress(bytes(c['minidata'], 'utf-8')) compressed = gzip.compress(bytes(c['minidata'], 'utf-8'))
c['gzipdata'] = ','.join([ str(b) for b in compressed ]) c['gzipdata'] = ','.join([ str(b) for b in compressed ])
c['gziplen'] = len(compressed) c['gziplen'] = len(compressed)
print(" GZIP data length: %s" % c['gziplen']) print(f" GZIP data length: {c['gziplen']}")
return c return c
def perform_minify(c): def perform_minify(c):
@ -71,24 +75,24 @@ def perform_minify(c):
'js': jsminify, 'js': jsminify,
'html': htmlminify 'html': htmlminify
}.get(c['type']) or htmlminify }.get(c['type']) or htmlminify
print(" Using %s minifier" % c['type']) print(f" Using {c['type']} minifier")
c['minidata'] = minifier(infile.read()) c['minidata'] = minifier(infile.read())
return perform_gzip(c) return perform_gzip(c)
def process_file(infile, outdir, storemini=True): def process_file(infile, outdir, storemini=True):
print("Processing file %s" % infile) print(f"Processing file {infile}")
c = get_context(infile, outdir) c = get_context(infile, outdir)
c = perform_minify(c) c = perform_minify(c)
if storemini: if storemini:
if c['infile'] == c['minifile']: if c['infile'] == c['minifile']:
print(" Original file is already minified, refusing to overwrite it") print(" Original file is already minified, refusing to overwrite it")
else: else:
print(" Writing minified file %s" % c['minifile']) print(f" Writing minified file {c['minifile']}")
with open(c['minifile'], 'w+') as minifile: with open(c['minifile'], 'w+') as minifile:
minifile.write(c['minidata']) minifile.write(c['minidata'])
with open(c['outfile'], 'w+') as outfile: with open(c['outfile'], 'w+') as outfile:
print(" Using C constant names %s and %s_GZIP" % (c['constant'], c['constant'])) print(f" Using C constant names {c['constant']} and {c['constant']}_GZIP")
print(" Writing C header file %s" % c['outfile']) print(f" Writing C header file {c['outfile']}")
outfile.write(TARGET_TEMPLATE.format(**c)) outfile.write(TARGET_TEMPLATE.format(**c))
def filenamefilter(pattern, strings): def filenamefilter(pattern, strings):
@ -96,10 +100,14 @@ def filenamefilter(pattern, strings):
def process_dir(sourcedir, outdir, recursive=True, storemini=True): def process_dir(sourcedir, outdir, recursive=True, storemini=True):
pattern = r'/*\.(css|js|htm|html)$' pattern = r'/*\.(css|js|htm|html)$'
files = glob(sourcedir + "/**/*", recursive=True)+glob(sourcedir + "/*") if recursive else glob(sourcedir + "/*") files = (
glob(f"{sourcedir}/**/*", recursive=True) + glob(f"{sourcedir}/*")
if recursive
else glob(f"{sourcedir}/*")
)
files = filenamefilter(pattern, files) files = filenamefilter(pattern, files)
for f in set(files): for f in set(files):
if not '.min.' in f: if '.min.' not in f:
process_file(f, outdir, storemini) process_file(f, outdir, storemini)
elif not os.path.isfile(f.replace(".min.", ".")): elif not os.path.isfile(f.replace(".min.", ".")):
process_file(f, outdir, storemini) process_file(f, outdir, storemini)
@ -107,13 +115,15 @@ def process_dir(sourcedir, outdir, recursive=True, storemini=True):
def check_args(args): def check_args(args):
abort = 0 abort = 0
if not os.path.exists(args.sources): if not os.path.exists(args.sources):
print("ERROR: Source %s does not exist" % args.sources) print(f"ERROR: Source {args.sources} does not exist")
abort += 2 abort += 2
if not os.path.isdir(os.path.dirname(args.target)): if not os.path.isdir(os.path.dirname(args.target)):
print("ERROR: Parent directory of target %s does not exist" % args.target) print(f"ERROR: Parent directory of target {args.target} does not exist")
abort += 4 abort += 4
if os.path.isdir(args.sources) and not os.path.isdir(args.target): if os.path.isdir(args.sources) and not os.path.isdir(args.target):
print("ERROR: Source %s is a directory, target %s is not" % (args.sources, args.target)) print(
f"ERROR: Source {args.sources} is a directory, target {args.target} is not"
)
abort += 8 abort += 8
if abort > 0: if abort > 0:
print("Aborting.") print("Aborting.")
@ -124,10 +134,12 @@ def main(args):
args.target = os.path.realpath(args.target or os.sep.join((os.path.dirname(os.path.realpath(__file__)), "..", "src"))) args.target = os.path.realpath(args.target or os.sep.join((os.path.dirname(os.path.realpath(__file__)), "..", "src")))
check_args(args) check_args(args)
if os.path.isfile(args.sources): if os.path.isfile(args.sources):
print("Source %s is a file, will process one file only." % args.sources) print(f"Source {args.sources} is a file, will process one file only.")
process_file(args.sources, args.target, storemini = args.storemini) process_file(args.sources, args.target, storemini = args.storemini)
elif os.path.isdir(args.sources): elif os.path.isdir(args.sources):
print("Source %s is a directory, searching for files recursively..." % args.sources) print(
f"Source {args.sources} is a directory, searching for files recursively..."
)
process_dir(args.sources, args.target, recursive = True, storemini = args.storemini) process_dir(args.sources, args.target, recursive = True, storemini = args.storemini)
if __name__ == "__main__" and "get_ipython" not in dir(): if __name__ == "__main__" and "get_ipython" not in dir():