-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
82 lines (65 loc) · 2.45 KB
/
run.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
import sys, os
from typing import List
from src import print_utils, lib
def ask_if_fix_extensions():
while True:
print("Do you want to rename images if images have incorrect extensions?")
print("If you consent, it will rename selected images, even if metadata cannot be applied later in the process.")
print('e.g. a JPG file named "myimage.HEIC" would be renamed "myimage.jpg"')
print("y,yes,n,no")
answer = input()
print()
if answer.lower() in ["y", "yes"]:
lib.FIX_FILE_EXTENSIONS = True
break
elif answer.lower() in ["n", "no"]:
lib.FIX_FILE_EXTENSIONS = False
break
def ask_if_convert():
while True:
print("Do you want to convert all HEIC images to JPG?")
print("JPG is a more common file type than HEIC and will be viewable on almost any device.")
print("y,yes,n,no")
answer = input()
print()
if answer.lower() in ["y", "yes"]:
lib.CONVERT_HEIC_TO_JPG = True
break
elif answer.lower() in ["n", "no"]:
lib.CONVERT_HEIC_TO_JPG = False
break
def format_path(path):
returned_path = path
if path[0] != "/":
returned_path = f"{os.getcwd()}/{returned_path}"
if path[-1] == "/":
returned_path = returned_path[:-1]
return returned_path
def get_args(args: List[str]) -> str:
if len(args) < 2:
print_utils.print_help_and_exit("Not enough arguments")
elif len(args) > 3:
print_utils.print_help_and_exit("Too many arguments")
path = None
for arg in args[1:]:
if arg in ["--help", "-h"]:
print_utils.print_help_and_exit()
elif arg[0] == "-":
print_utils.print_help_and_exit(f"Unknown argument: {arg}")
else:
path = format_path(arg)
if not path:
print_utils.print_help_and_exit("Did not provide <path>")
elif not os.path.exists(path):
print_utils.print_help_and_exit(f"Directory: {path}\n does not exist.")
return path or "" # The LSP hates it when I don't include the 'or ""' bit
def main():
path = get_args(sys.argv)
if type(lib.FIX_FILE_EXTENSIONS) is not bool:
ask_if_fix_extensions()
if type(lib.CONVERT_HEIC_TO_JPG) is not bool:
ask_if_convert()
imgs_modified = lib.process_files_in_dir(path)
print_utils.print_success_message(imgs_modified)
if __name__ == "__main__":
main()