| skipped 25 lines |
26 | 26 | | image.save(output, format='png') |
27 | 27 | | png_data = output.getvalue() |
28 | 28 | | |
29 | | - | if len(png_data) > byte_size: |
| 29 | + | if (byte_size == None) or (len(png_data) > byte_size): |
30 | 30 | | with open(file_path, 'wb') as f: |
31 | 31 | | f.write(png_data) |
32 | 32 | | return False |
| skipped 30 lines |
63 | 63 | | c.showPage() |
64 | 64 | | c.save() |
65 | 65 | | |
| 66 | + | def parse_bytes(byte_str): |
| 67 | + | if byte_str == None: |
| 68 | + | return None |
| 69 | + | |
| 70 | + | if byte_str.endswith(('B', 'b')): |
| 71 | + | return int(byte_str[:-1]) |
| 72 | + | elif byte_str.endswith(('KB', 'Kb', 'kB', 'kb')): |
| 73 | + | return int(byte_str[:-2]) * 1024 |
| 74 | + | elif byte_str.endswith(('MB', 'Mb', 'mB', 'mb')): |
| 75 | + | return int(byte_str[:-2]) * 1024 * 1024 |
| 76 | + | elif byte_str.endswith(('GB', 'Gb', 'gB', 'gb')): |
| 77 | + | return int(byte_str[:-2]) * 1024 * 1024 * 1024 |
| 78 | + | else: |
| 79 | + | return int(byte_str) |
| 80 | + | |
66 | 81 | | def parse_args(): |
67 | 82 | | colorama.init(autoreset=True) |
68 | | - | parser = argparse.ArgumentParser(description='Create a dummy file of the specified size.') |
| 83 | + | parser = argparse.ArgumentParser(description='Create a dummy file for testing.') |
69 | 84 | | parser.add_argument('-n', '--name', help='File name(.jpeg, .png, .pdf)', required=True) |
70 | | - | parser.add_argument('-t', '--text', help='Text to be written in the file') |
71 | | - | parser.add_argument('-s', '--size', help='Bytes of file(.png only)') |
| 85 | + | parser.add_argument('-t', '--text', help='Text to be written in the file', default='dummy file') |
| 86 | + | parser.add_argument('-b', '--bytes', help='Bytes of file(.png only)') |
| 87 | + | |
72 | 88 | | args = parser.parse_args() |
73 | | - | # if args.name.endswith('.png'): |
74 | | - | |
75 | | - | if __name__ == '__main__': |
76 | | - | colorama.init(autoreset=True) |
77 | | - | make_jpeg('dummy.jpeg', 'sample text') |
| 89 | + | if args.name.endswith('.jpeg'): |
| 90 | + | make_jpeg(args.name, args.text) |
| 91 | + | elif args.name.endswith('.png'): |
| 92 | + | make_png(args.name, args.text, parse_bytes(args.bytes)) |
| 93 | + | elif args.name.endswith('.pdf'): |
| 94 | + | make_pdf(args.name, args.text) |
| 95 | + | else: |
| 96 | + | print(Fore.RED + 'Error: Invalid file extension.') |
| 97 | + | return |
78 | 98 | | |