-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshavis.py
539 lines (459 loc) · 19.5 KB
/
shavis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import os
import sys
import string
import hashlib
import pathlib
import configparser
from git import Repo
from PIL import Image
def printHelpPage(theme: str, size_select: int, color: bool, git: bool):
helpPage = f"""Usage: shavis [OPTIONS]...[FILE]
Visualize SHA256 or SHA1 hash sum, either directly or of a file.
With no flags, print this help page and exit.
--config
Change configuration for resolution and theme with persistence
[--config theme NAME] [--config size N]
-f, --file Hash file and visualize hash
[-f filename.ext] [--file filename.ext]
-s, --hash Input hash directly as argument
[-s HASH] [--hash HASH] (HASH has to be SHA256)
-r, --resolution
Pick resolution size, options:
[N] SHA256 SHA1 (Git)
1 - 8x8 - 8x5
2 - 16x16 - 16x10
3 - 32x32 - 32x20
4 - 64x64 - 64x40
5 - 128x128 - 128x80
6 - 256x256 - 256x160
7 - 512x512 - 512x320
8 - 1024x1024 - 1024x640
9 - 2048x2048 - 2048x1280
10 - 4096x4096 - 4096x2560
[-r N] [--resolution N]
-t, --theme Change theme, currently available themes:
blue, red, gold, natur, dim, dark, cyan, soft-fall.
[-t red] [--theme gold]
-o, --output Output to file and (optionally) choose file name
[-o def] [--output def] or [-o "output.png"] [--output "output.png"]
-g, --git Use a git commit hash to generate 8x5 image
[-g HASH] [--git HASH] (HASH has to be SHA-1)
-gl --git-latest Use latest git commit hash from current directory to generate 8x5 image
[-gl] [--git-latest]
-m, --mono Black and white output. No arguments.
[-m] [--mono]
-h, --help Display this help and exit
[-h] [--help]
-v, --version Display version and exit
[-v] [--version]
If output filename is "def", the file name will be the first 7 hex digits
of the hash in a .png format.
Current config:
theme: {theme}
size: {size_select}
color: {color}
git: {git}
Examples:
shavis filename.ext
shavis -t cyan -r 4 -o def -s HASH
shavis -m -o commit.png -g GIT-HASH
Piping accessibility examples:
cat file.ext | shavis
git rev-parse HEAD | shavis -g
Check out the project at: https://github.com/kernel137/shavis
"""
print(helpPage)
def nextargument(argv: list[str], opt: str) -> list:
'''
Return next member of specified option flag
'''
return argv[(argv.index(opt) + 1):(argv.index(opt) + 2)]
def updateconf(config: configparser.ConfigParser, config_path: str) -> None:
'''
Update `config.ini` file with given `ConfigParser` object
'''
with open(config_path, "w") as conf:
config.write(conf)
# [=================[Hash functions]=================]
def hashfile(filename: str) -> str:
'''
Return file's sha256 hash sum in a form of hex string
'''
sha256hash = hashlib.sha256()
with open(filename, 'rb') as file:
while True:
stack = file.read(2**16) # 64kb
if not stack: break
sha256hash.update(stack)
return sha256hash.hexdigest()
def hashtext(text: str) -> str:
'''
Return input string's sha256 hash sum in a form of hex string
'''
return hashlib.sha256(bytes(str(text), "UTF-8")).hexdigest()
def start():
p = pathlib.PurePath(__file__)
config_dir = pathlib.Path(*list(p.parent.parts) + ["conf"] + ["config.ini"])
theme_dir = pathlib.Path(*list(p.parent.parts) + ["conf"] + ["themes"])
config = configparser.ConfigParser()
config.read(config_dir)
image = Image.new("RGB", (8, 8))
sha256 = ""
# [===================[Parameters]===================]
output_to_file_flag = False
output_filename = "output.txt"
#-----------------------------
filename = ""
#------------
theme = str(config["options"]["theme"])
size_select = int(config["options"]["size"])
color = config.getboolean("options", "color")
git = config.getboolean("options", "git")
#----------------------------------------
allowed_themes = ["blue", "red", "gold", "natur", "dim", "dark", "cyan", "soft-fall"]
allowed_sizes = [*range(1, 11)]
allowed_formats = ["png", "PNG", "jpg", "JPG", "jpeg", "JPEG", "bmp", "BMP", "ppm", "PPM"]
# [======================[Help]======================]
if(os.isatty(0) and (len(sys.argv) == 1 or sys.argv[1] == "-h" or sys.argv[1] == "--help")):
printHelpPage(theme, size_select, color, git)
exit() # expected exit
# [===============[Options Processing]===============]
if("--config" in sys.argv):
if nextargument(sys.argv, "--config") == []: # filter for missing option (--config option) -> exit
print(f"Missing option: --config OPTION")
print("Valid options: theme, size, color, git, list")
exit(1) # error exit
# option - exists
option = sys.argv[sys.argv.index("--config")+1]
# taking option
if option not in ["theme", "size", "color", "git", "list"]: # filter for wrong option (--config option) -> exit
print(f"Invalid option: {option}")
print("Valid options: theme, size, color, git, list")
exit(1) # error exit
# option = theme or size or list
if option == "list": # list current config (--config list) -> exit
print("[current config]")
print("theme: " + str(config["options"]["theme"]))
print("size: " + str(config["options"]["size"]))
print("color: " + str(config.getboolean("options", "color")))
print("git: " + str(config.getboolean("options", "git")))
exit() # expected exit
if nextargument(sys.argv, option) == []: # filter for missing argument (--config option argument) -> exit
print(f"Missing setting argument: --config {option} arg")
if option == "theme":
print("Available themes: ", end="")
for name in allowed_themes: print(name, end=" ")
if option == "size":
print("Available sizes: ", end="")
for num in allowed_sizes: print(num, end=" ")
if option == "color":
print("Available values: True, False", end="")
if option == "git":
print("Available values: True, False", end="")
print()
exit(1) # error exit
# argument - exists
if option == "size": # filter and update size config (--config size N) -> exit
size_select = sys.argv[sys.argv.index("size")+1]
if not size_select.isdigit(): # check if size is an integer (--config size N) -> exit
print(f"Size invalid: {size_select}")
print("Size needs to be an integer.")
exit(1) # error exit
if int(size_select) not in allowed_sizes: # check if size is valid (--config size N) -> exit
print(f"Size invalid: {size_select}")
print("Available sizes: ", end="")
for num in allowed_sizes[:-1]: print(num, end=", ")
print(allowed_sizes[len(allowed_sizes)-1])
exit(1) # error exit
size_select = int(size_select)
config.set("options", "size", str(size_select))
updateconf(config, config_dir)
exit() # expected exit
if option == "theme": # filter and update theme config (--config theme name) -> exit
theme = sys.argv[sys.argv.index("theme")+1]
if theme not in allowed_themes: # check if theme name is valid
print(f"Theme name invalid: {theme}")
print("Available themes: ", end="")
for name in allowed_themes[:-1]: print(name, end=", ")
print(allowed_themes[len(allowed_themes)-1])
exit(1) # error exit
config.set("options", "theme", str(theme))
updateconf(config, config_dir)
exit() # expected exit
if option == "color": # filter and update color config (--config color bool) -> exit
color = sys.argv[sys.argv.index("color")+1]
if color.lower() in ["true", "t", "y", "yes", "yeah", "mhm", "yup"]:
config.set("options", "color", "True")
elif color.lower() in ["false", "f", "n", "no", "nah", "nuhuh", "nope"]:
config.set("options", "color", "False")
else:
print("Invalid boolean value: --config color VALUE")
print("Use a boolean as value for color.")
exit(1) # error exit
updateconf(config, config_dir)
exit() # expected exit
if option == "git": # filter and update git config (--config git bool) -> exit
git = sys.argv[sys.argv.index("git")+1]
if git.lower() in ["true", "t", "y", "yes", "yeah", "mhm", "yup"]:
config.set("options", "git", "True")
elif git.lower() in ["false", "f", "n", "no", "nah", "nuhuh", "nope"]:
config.set("options", "git", "False")
else:
print("Invalid boolean value: --config git VALUE")
print("Use a boolean as value for git.")
exit(1) # error exit
updateconf(config, config_dir)
exit() # expected exit
if("-f" in sys.argv or "--file" in sys.argv):
if "-f" in sys.argv: # filter for missing filename (-f filename.ext) -> exit
if nextargument(sys.argv, "-f") == []: # filter for missing filename
print(f"Missing file name: -f filename.ext")
exit(1) # error exit
# file name - exists
if "--file" in sys.argv: # filter for missing filename (--file filename.ext) -> exit
if nextargument(sys.argv, "--file") == []: # filter for missing filename
print(f"Missing file name: --file filename.ext")
exit(1) # error exit
# file name - exists
# file name - exists
filename = sys.argv[sys.argv.index("-f")+1] if "-f" in sys.argv else sys.argv[sys.argv.index("--file")+1]
# taking file name
if not os.path.exists(filename):
print(f"Invalid file name: --file {filename}")
print("File does not exist.")
exit(1) # error exit
sha256 = hashfile(str(filename))
if("-t" in sys.argv or "--theme" in sys.argv):
if "-t" in sys.argv: # filter for missing option (-t option) -> exit
if nextargument(sys.argv, "-t") == []: # filter for missing setting
print(f"Missing setting: -t theme_name")
print("Available themes: ", end="")
for name in allowed_themes[:-1]: print(name, end=", ")
print(allowed_themes[len(allowed_themes)-1])
exit(1) # error exit
# setting - exists
if "--theme" in sys.argv: # filter for missing option (--theme option) -> exit
if nextargument(sys.argv, "--theme") == []: # filter for missing setting
print(f"Missing setting: --theme theme_name")
print("Available themes: ", end="")
for name in allowed_themes[:-1]: print(name, end=", ")
print(allowed_themes[len(allowed_themes)-1])
exit(1) # error exit
# setting - exists
# setting - exists
theme = sys.argv[sys.argv.index("-t")+1] if "-t" in sys.argv else sys.argv[sys.argv.index("--theme")+1]
# taking setting | setting -> theme
if theme not in allowed_themes: # filter for wrong option (--theme option) -> exit
print(f"Theme name invalid: -t/--theme theme_name")
print("Available themes: blue, red, gold, natur, dim, dark, cyan, soft-fall")
exit(1) # error exit
# setting theme changed for this call
if("-s" in sys.argv or "--hash" in sys.argv):
if "-s" in sys.argv: # filter for missing hash (-s HASH) -> exit
if nextargument(sys.argv, "-s") == []: # filter for missing hash
print(f"Missing hash: -s HASH")
exit(1) # error exit
# hash - exists
if "--hash" in sys.argv: # filter for missing hash (--hash HASH) -> exit
if nextargument(sys.argv, "--hash") == []: # filter for missing hash
print(f"Missing hash: --hash HASH")
exit(1) # error exit
# hash - exists
# hash input - exists
sha256 = str(sys.argv[sys.argv.index("-s")+1]) if "-s" in sys.argv else str(sys.argv[sys.argv.index("--hash")+1])
# sha256 updated - checked for invalid input in code below
if("-r" in sys.argv or "--resolution" in sys.argv):
if "-r" in sys.argv: # filter for missing hash (-r HASH) -> exit
if nextargument(sys.argv, "-r") == []: # filter for missing hash
print(f"Missing number: -r N")
print("Available resolutions: ", end="")
for num in allowed_sizes[:-1]: print(num, end=", ")
print(allowed_sizes[len(allowed_sizes)-1])
print("Syntax: -r N")
print(" N SHA256 SHA1 (Git)")
print(" 1 - 8x8 - 8x5")
print(" 2 - 16x16 - 16x10")
print(" 3 - 32x32 - 32x20")
print(" 4 - 64x64 - 64x40")
print(" 5 - 128x128 - 128x80")
print(" 6 - 256x256 - 256x160")
print(" 7 - 512x512 - 512x320")
print(" 8 - 1024x1024 - 1024x640")
print(" 9 - 2048x2048 - 2048x1280")
print("10 - 4096x4096 - 4096x2560")
exit(1) # error exit
# hash - exists
if "--resolution" in sys.argv: # filter for missing hash (--hash HASH) -> exit
if nextargument(sys.argv, "--resolution") == []: # filter for missing hash
print(f"Missing number: --resolution N")
print("Available resolutions: ", end="")
for num in allowed_sizes[:-1]: print(num, end=", ")
print(allowed_sizes[len(allowed_sizes)-1])
print("Syntax: --resolution N")
print(" N SHA256 SHA1 (Git)")
print(" 1 - 8x8 - 8x5")
print(" 2 - 16x16 - 16x10")
print(" 3 - 32x32 - 32x20")
print(" 4 - 64x64 - 64x40")
print(" 5 - 128x128 - 128x80")
print(" 6 - 256x256 - 256x160")
print(" 7 - 512x512 - 512x320")
print(" 8 - 1024x1024 - 1024x640")
print(" 9 - 2048x2048 - 2048x1280")
print("10 - 4096x4096 - 4096x2560")
exit(1) # error exit
# hash - exists
# hash input - exists
size_select = str(sys.argv[sys.argv.index("-r")+1]) if "-r" in sys.argv else str(sys.argv[sys.argv.index("--resolution")+1])
if not size_select.isdigit(): # check if size is an integer (--config size N) -> exit
print(f"Size invalid: {size_select}")
print("Size needs to be an integer.")
exit(1) # error exit
if int(size_select) not in allowed_sizes: # check if size is valid (--config size N) -> exit
print(f"Size invalid: {size_select}")
print("Available sizes: ", end="")
for num in allowed_sizes[:-1]: print(num, end=", ")
print(allowed_sizes[len(allowed_sizes)-1])
exit(1) # error exit
size_select = int(size_select)
# size_select updated
if("-g" in sys.argv or "--git" in sys.argv):
if not os.isatty(0): pass
elif "-g" in sys.argv: # filter for missing hash (-g GIT-HASH) -> exit
if nextargument(sys.argv, "-g") == []: # filter for missing hash
print(f"Missing hash: -g GIT-HASH")
print("Where GIT-HASH is a git commit SHA-1 hash of length 40")
exit(1) # error exit
# hash - exists
elif "--git" in sys.argv: # filter for missing hash (--git GIT-HASH) -> exit
if nextargument(sys.argv, "--git") == []: # filter for missing has
print(f"Missing hash: --git GIT-HASH")
print("Where GIT-HASH is a git commit SHA-1 hash of length 40")
exit(1) # error exit
# hash - exists
# hash - exists
git = True
if os.isatty(0):
sha256 = sys.argv[sys.argv.index("-g")+1] if "-g" in sys.argv else sys.argv[sys.argv.index("--git")+1]
# output filename inserted
if("-gl" in sys.argv or "--git-latest" in sys.argv):
git = True
repository = Repo(os.path.abspath(os.getcwd()))
sha256 = repository.head.commit.hexsha
if("-o" in sys.argv or "--output" in sys.argv):
if "-o" in sys.argv: # filter for missing filename (-o filename.ext) -> exit
if nextargument(sys.argv, "-o") == []: # filter for missing filename
print(f"Missing filename argument: -o filename.ext")
print("Make sure to include the extension: .png, .jpg etc etc.")
print(f"Alternative option: --output def")
print("This sets the name of the file to the first 7 hex values")
print("of the hash")
exit(1) # error exit
# output - exists
if "--output" in sys.argv: # filter for missing filename (--output filename.ext) -> exit
if nextargument(sys.argv, "--output") == []: # filter for missing filename
print(f"Missing filename argument: --output filename.ext")
print("Make sure to include the extension: .png, .jpg etc etc.")
print(f"Alternative option: --output def")
print("This sets the name of the file to the first 7 hex values")
print("of the hash")
exit(1) # error exit
# output - exists
# output - exists
output_to_file_flag = True
output_filename = sys.argv[sys.argv.index("-o")+1] if "-o" in sys.argv else sys.argv[sys.argv.index("--output")+1]
# output filename inserted
if output_filename == "def":
output_filename = sha256[:7] + ".png"
ext = "".join(output_filename.split(".")[-1:])
if ext not in allowed_formats: # filter for incorrect format of .ext (--filename filename.ext) -> exit
print(f"Incorrect output file format: .{ext}")
print("Available formats: ", end="")
for ext in allowed_formats[:-1]: print(f".{ext}", end=", ")
print(allowed_formats[len(allowed_formats)-1])
exit(1) # error exit
if("-m" in sys.argv or "--mono" in sys.argv):
color = False
if("-v" in sys.argv or "--version" in sys.argv):
print("shavis 0.1.4")
exit() # expected exit
# [=================[Check for pipe]=================]
if not os.isatty(0):
if git:
sha256 = str(sys.stdin.read()).strip()
else:
hashedpipe = hashlib.sha256(sys.stdin.buffer.read()).hexdigest()
sha256 = str(hashedpipe)
# [==============[Check for empty hash]==============]
if sha256 == "":
ls_files = next(os.walk("./"), (None, None, []))[2]
filename = "".join(sys.argv[-1:])
if filename in ls_files:
try: # opening file
file = open(str(filename))
except FileNotFoundError: # filter filename.ext existing -> exit
print(f"Invalid file name: --file {filename}")
print("File does not exist.")
exit(1) # error exit
else:
sha256 = hashfile(str(filename))
else:
print("Hash value empty")
print("Input hash either by hashing a file:")
print(" shavis filename.ext")
print(" shavis -t blue -r 3 filename.ext")
print("(Filename should always be the last argument")
print("and in the same directory)")
print("For files not in the same directory, use --file /dir/to/file.ext\"\"")
print("Or inputting hash manually with:")
print(" shavis --git GIT-HASH")
print(" shavis --hash HASH")
exit(1) # error exit
# [==========[Validate manual hash inputs]==========]
# Check length
if git and len(sha256) != 40: # filter for missing hash (-g GIT-HASH) -> exit
print("Error: Git hash must be 40 characters long (SHA-1).")
print(f"[{sha256}]")
exit(1) # error exit
if not git and len(sha256) != 64: # filter for missing hash -> exit
print("Error: Input must be 64 characters long.")
exit(1) # error exit
# Check for invalid characters (And point them out!!!)
valid_characters = [i in string.hexdigits for i in sha256]
if not all(valid_characters):
print("Error: Invalid SHA256 hashsum character in string.")
print(sha256)
for char in valid_characters:
if not char: print("↑", end='')
else: print(" ", end='')
print()
exit(1) # error exit
# [==================[ Processing ]==================]
sha256_dv = [int(i, 16) for i in sha256] # dv = Decimal Values | hex -> decimal -> list
sha256_dvm = [] # dvm = Decimal Values Matrix |
for i in range(0, 8): sha256_dvm.append(sha256_dv[i*8:(i+1)*8]) # | list[64] -> matrix[8][8]
# [==================[Theme Loader]==================]
theme = theme_dir / (theme + ".hex") # insert theme name
with open(theme) as file: # open theme file
theme = file.readlines() # theme is now list[16] of hex colors from theme
for i in range(16):
theme[i] = theme[i].strip() # strip '\n'
theme[i] = tuple(int(theme[i][i2:i2+2], 16) for i2 in [0, 2, 4]) # hex list[16] -> list of 16 decimal 3 tuple
# [================[Rendering Image]=================]
(x, y) = (8, 5) if git else (8, 8)
image = Image.new("RGB" if color else "L", (x, y))
pixels = image.load()
for v in range(x):
for h in range(y):
pixels[v,h] = theme[sha256_dvm[h][v]] if color else ((sha256_dvm[h][v]+1)*16)-1
size = 8 * (2**(size_select-1))
if git:
xsize, ysize = size, 5 * (2**(size_select-1))
else:
xsize, ysize = size, size
# [=============[Resize and output Image]============]
if output_to_file_flag: image.resize((xsize, ysize), resample=Image.Resampling.NEAREST).save(output_filename)
else: image.resize((xsize, ysize), resample=Image.Resampling.NEAREST).show()
# [==================================================]
if __name__ == "__main__":
start()