-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.py
169 lines (150 loc) · 3.96 KB
/
cli.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
#!/usr/bin/env python
# coding: utf-8
import argparse
import asyncio
from pathlib import Path
from main import loop_structure, __version__
from config import load_config, parse_config
from utils.input import parse_bool
from utils.common import yes_no
from utils.display import Write
root = Path(__file__).resolve().parent
license = Path(root / "LICENSE").read_text()
desc = """
Iterate over a directory and perform compression, resizing and formatting
for optimal web capabilities.
"""
epilog = """
examples:
python3 cli.py --skip-existing false --copy true --image-formats webp ./videos
python3 cli.py --skip-existing false --video-formats webm ./videos
python3 cli.py --thumbnail --vtt --video-formats mp4 webm ./videos
python3 cli.py --video-formats mp4 webm ./videos ./go/here
A = required [A] = optional [A ...] = optional list
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter, description=desc, epilog=epilog
)
parser.add_argument(
"-v", "--version", action="version", version="%(prog)s " + __version__
)
parser.add_argument(
"--license", action="version", version=license, help="show program's license"
)
parser.add_argument(
"input",
type=Path,
nargs="?",
default=Path.cwd(),
help="folder to iterate over (default: current dir)",
)
parser.add_argument(
"output",
type=Path,
nargs="?",
default=None,
help="destination folder (default: `-copy` added to input folder)",
)
parser.add_argument(
"--config",
type=Path,
nargs="?",
metavar="PATH",
help="a path to a config .toml file",
)
parser.add_argument(
"--slugify",
type=parse_bool,
nargs="?",
const=True,
default=True,
metavar="BOOL",
help="slugify filenames (default: True)",
)
parser.add_argument(
"--skip-existing",
type=parse_bool,
nargs="?",
const=True,
default=True,
metavar="BOOL",
help="skip existing processed files (default: True)",
)
parser.add_argument(
"--gif",
type=parse_bool,
nargs="?",
const=True,
default=False,
metavar="BOOL",
help="process GIF´s (default: False)",
)
parser.add_argument(
"--copy",
type=parse_bool,
nargs="?",
const=True,
default=False,
metavar="BOOL",
help="copy not processable files (default: False)",
)
parser.add_argument(
"--image-formats",
type=str,
nargs="+",
default=[],
choices=["png", "jpg", "webp"],
metavar="LIST",
help="image types to convert to (increases the execution time)",
)
parser.add_argument(
"--video-formats",
type=str,
nargs="+",
default=[],
choices=["mp4", "webm"],
metavar="LIST",
help="video types to convert to (increases the execution time)",
)
parser.add_argument(
"--thumbnail",
type=parse_bool,
nargs="?",
const=True,
default=False,
metavar="BOOL",
help="create thumbnails from video files (default: False)",
)
parser.add_argument(
"--vtt",
type=parse_bool,
nargs="?",
const=True,
default=False,
metavar="BOOL",
help="create video progress thumbnails (default: False)",
)
args = parser.parse_args()
args = vars(args)
if args["config"]:
args["config"] = Path(args["config"])
if args["config"].exists() and args["config"].is_file():
config = load_config(args["config"])
config = parse_config(config)
args.update(config)
else:
raise ValueError("Cannot find config file: " + str(args["config"]))
if not args["output"]:
args["output"] = Path(str(args["input"]) + "-copy")
args["output"].mkdir(parents=True, exist_ok=True)
Write.text("Input: {}".format(args["input"]))
Write.text("Output: {}".format(args["output"]))
# TODO: add file count based on above generator options
# TODO: add estimated time based on above generator options * delta
proceed = yes_no("Continue?")
if not proceed:
exit(0)
Write.line() # empty
Write.green("Starting...")
Write.line() # empty
asyncio.run(loop_structure(args)) # start the loop