Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few Error handling Items for Bitmap Collage and Zero-Byte files. #14

Open
ghost opened this issue Mar 13, 2023 · 0 comments
Open

Few Error handling Items for Bitmap Collage and Zero-Byte files. #14

ghost opened this issue Mar 13, 2023 · 0 comments

Comments

@ghost
Copy link

ghost commented Mar 13, 2023

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants