bmc-tools
bmc-tools copied to clipboard
Few Error handling Items for Bitmap Collage and Zero-Byte files.
Adding Exceptions for bitmap collage if not enough files are present with default -WIDTH of 64
try:
c_bmp = b"" if not self.pal else self.PALETTE
if self.btype == self.BIN_CONTAINER:
collage_builder = (lambda x, a=self, PAD=len(pad), WIDTH=list(range(w // 64)): b''.join([b''.join([a.bmps[a.STRIPE_WIDTH*(x+1)-1-k][64*PAD*j:64*PAD*(j+1)] for k in WIDTH]) for j in range(64)]))
else:
collage_builder = (lambda x, a=self, PAD=len(pad), WIDTH=list(range(w // 64)): b''.join([b''.join([a.bmps[a.STRIPE_WIDTH*x+k][64*PAD*j:64*PAD*(j+1)] for k in WIDTH]) for j in range(64)]))
c_bmp += b''.join(map(collage_builder, list(range(h//64))))
self.b_write(os.path.join(dname, "%s_collage.bmp" % (self.fname)), self.b_export_bmp(w, h, c_bmp))
self.b_log(sys.stdout, False, 0, "Successfully exported collage file.")
except IndexError as ie:
self.b_log(sys.stdout, False, 1, "Not enough files to build collage with default collage -WIDTH of 64")
try:
c_bmp = b"" if not self.pal else self.PALETTE
if self.btype == self.BIN_CONTAINER:
collage_builder = (lambda x, a=self, PAD=len(pad), WIDTH=list(range(w // 64)): b''.join([b''.join([a.bmps[a.STRIPE_WIDTH*(x+1)-1-k][64*PAD*j:64*PAD*(j+1)] for k in WIDTH]) for j in range(64)]))
else:
collage_builder = (lambda x, a=self, PAD=len(pad), WIDTH=list(range(w // 64)): b''.join([b''.join([a.bmps[a.STRIPE_WIDTH*x+k][64*PAD*j:64*PAD*(j+1)] for k in WIDTH]) for j in range(64)]))
c_bmp += b''.join(map(collage_builder, list(range(h//64))))
self.b_write(os.path.join(dname, "%s_collage.bmp" % (self.fname)), self.b_export_bmp(w, h, c_bmp))
self.b_log(sys.stdout, False, 0, "Successfully exported collage file.")
except IndexError as ie:
self.b_log(sys.stdout, False, 1, "Not enough files to build collage with default collage -WIDTH of 64")
Add in an exception and skip for zero byte files in Argparse Options:
# Process all files in a directory
elif os.path.isdir(args.src):
sys.stdout.write("[+++] Processing a directory...%s" % (os.linesep))
src_files = []
for root, dirs, files in os.walk(args.src):
for f in files:
if f.rsplit(".", 1)[-1].upper() in ["BIN", "BMC"]:
file_path = os.path.join(root, f)
file_size = os.path.getsize(file_path)
if file_size > 0:
if args.verbose:
sys.stdout.write("[---] File '%s' has been found.%s" % (file_path, os.linesep))
src_files.append(os.path.join(root, f))
else:
sys.stdout.write("[!!!] File '%s' is zero bytes, skipping file.%s" % (file_path, os.linesep))
if len(src_files) == 0:
sys.stderr.write("No suitable files were found under '%s' directory.%s" % (args.src, os.linesep))
exit(-1)
# Process a single file
elif not os.path.isfile(args.src):
sys.stderr.write("Invalid -s/--src parameter; use -h/--help for help.%s" % (os.linesep))
exit(-1)
else:
file_size = os.path.getsize(args.src)
if file_size > 0:
sys.stdout.write("[+++] Processing a single file: '%s'.%s" % (args.src, os.linesep))
src_files = [args.src]
else:
sys.stdout.write("[!!!] File '%s' is Zero-Bytes.%s" % (args.src, os.linesep))
# Import, process, export, and flush each file
print("[+++] Files for processing:", str(src_files))