-
Notifications
You must be signed in to change notification settings - Fork 0
/
vscript.py
executable file
·75 lines (58 loc) · 2.16 KB
/
vscript.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
#!/usr/bin/env python3
import argparse
import json
import logging
import os
import sys
from pathlib import Path
from typing import Any
from vscripts import COMMAND_APPEND, COMMAND_ATEMPO, COMMANDS, NTSC_RATE
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(sys.stdout))
def main(path_name: str, actions: dict[str, Any], queue: list[str]):
path = Path(path_name).absolute()
og_file = Path(path_name).absolute()
intermediate_files: list[Path] = []
assert path.is_file()
for command in queue:
intermediate_files.append(path)
fn = COMMANDS[command]
arg = actions[command]
if command == COMMAND_APPEND and arg is None:
# condition when we append a file at the end of a command queue
path = fn(path, into=og_file)
continue
if arg is not None:
path = fn(path, arg)
continue
path = fn(path)
for f in intermediate_files:
if f != og_file:
f.unlink()
def _parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("path", help="path to be handled")
parser.add_argument("actions", type=str, nargs="*", help="list of actions to be ran")
parsed = parser.parse_args()
todo: dict[str, Any] = {}
order: list[str] = []
for action in parsed.actions:
if "=" in action:
a, v = action.split("=")
if a == COMMAND_ATEMPO:
v = tuple(float(t) for t in v.split(",")) if "," in v else (v, NTSC_RATE)
todo[a] = v
order.append(a)
else:
todo[action] = None
order.append(action)
return parsed, todo, order
if __name__ == "__main__":
args, actions, queue = _parse_arguments()
logger.info(f"{os.path.basename(__file__)}:: args -> {args.__dict__}")
logger.info(f"{json.dumps(actions, indent=4, skipkeys=True, ensure_ascii=False)}")
if any(k not in COMMANDS.keys() for k in actions.keys()):
raise Exception(f"invalid command={next(k not in COMMANDS.keys() for k in actions.keys())}")
# TODO: validate queue is sorted
main(args.path, actions, queue)