-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_utils.py
163 lines (129 loc) · 5.38 KB
/
shared_utils.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
from pathlib import Path
import sys
import shutil
from argparse import ArgumentParser, ArgumentTypeError
from packages.dd_ddp import dd_ddp_bitrates
import xmltodict
def get_working_dir():
"""
Used to determine the correct working directory automatically.
This way we can utilize files/relative paths easily.
Returns:
(Path): Current working directory
"""
# we're in a pyinstaller.exe bundle
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
return Path(sys.executable).parent
# we're running from a *.py file
else:
return Path.cwd()
def save_xml(output_dir: Path, output_file_name: Path, xml_base: dict):
"""Creates/Deletes old XML files for use with DEE
Args:
output_dir (Path): Full output directory
output_file_name (Path): File name
xml_base (dict): XML generated dictionary
Returns:
Path: Path to XML file for DEE
"""
# Save out the updated template (use filename output with xml suffix)
updated_template_file = Path(output_dir / Path(output_file_name)).with_suffix(
".xml"
)
# delete xml output template if one already exists
if updated_template_file.exists():
updated_template_file.unlink()
# write new xml template for dee
with open(updated_template_file, "w", encoding="utf-8") as xml_out:
xml_out.write(xmltodict.unparse(xml_base, pretty=True, indent=" "))
# check to ensure template file was created
if updated_template_file.exists():
return updated_template_file
else:
raise ArgumentTypeError("XML file could not be created")
def validate_track_index(value: any):
"""
Determines if the input is a valid number.
If it's not returns the default of 0.
Args:
value (any): Can be any input
Returns:
int: Corrected track index
"""
# Check if the input is valid
if value.isdigit():
return int(value)
# If the input is invalid, return the default value
return 0
def validate_channels_with_format(arguments: ArgumentParser.parse_args):
"""
Validate channel count based on encoder format.
We'll only check channels if we're using dd/ddp, ignoring this function for atmos.
If an invalid input is detected, raise a parser error that will update
the user with valid options and exit the program automatically.
Args:
arguments (ArgumentParser.parse_args): Parsed arguments from parser instance
"""
if arguments.format != "atmos":
if arguments.format == "dd":
valid_channels = [1, 2, 6]
elif arguments.format == "ddp":
valid_channels = [1, 2, 6, 8]
else:
raise ArgumentTypeError("Unknown file format.")
if arguments.channels not in valid_channels:
ArgumentTypeError.error(
message=f"Invalid channel count for designated file type: {arguments.format}.\nValid options: {valid_channels}"
)
def validate_bitrate_with_channels_and_format(arguments: ArgumentParser.parse_args):
"""
Validate bitrate input based on channel input and file format.
If an invalid input is detected, raise a parser error that will update
the user with valid options and exit the program automatically.
Args:
arguments (ArgumentParser.parse_args): Parsed arguments from parser instance
"""
if arguments.format != "atmos":
if arguments.format == "dd":
if arguments.channels == 1:
valid_bitrates = dd_ddp_bitrates.get("dd_10")
elif arguments.channels == 2:
valid_bitrates = dd_ddp_bitrates.get("dd_20")
elif arguments.channels == 6:
valid_bitrates = dd_ddp_bitrates.get("dd_51")
else:
raise ArgumentTypeError("Invalid channel count.")
elif arguments.format == "ddp":
if arguments.channels == 1:
valid_bitrates = dd_ddp_bitrates.get("ddp_10")
elif arguments.channels == 2:
valid_bitrates = dd_ddp_bitrates.get("ddp_20")
elif arguments.channels == 6:
valid_bitrates = dd_ddp_bitrates.get("ddp_51")
elif arguments.channels == 8:
valid_bitrates = dd_ddp_bitrates.get("ddp_71_standard")
else:
raise ArgumentTypeError("Invalid channel count.")
else:
raise ArgumentTypeError("Unknown file format.")
if arguments.bitrate not in valid_bitrates:
ArgumentTypeError(
f"Invalid bitrate for input channel count and file type: {arguments.format} {str(arguments.channels)}.\nValid options: {', '.join(str(v) for v in valid_bitrates)}"
)
def check_disk_space(drive_path: Path, free_space: int):
"""
Check for free space at the drive path, rounding to nearest whole number.
If there isn't at least "free_space" GB of space free, raise an ArgumentTypeError.
Args:
drive_path (Path): Path to check
free_space (int): Minimum space (GB)
"""
# get free space in bytes
free_space_cwd = shutil.disk_usage(Path(drive_path)).free
# convert to GB's
free_space_gb = round(free_space_cwd / (1024**3))
# check to ensure at least 50 GB's is free
if free_space_gb < int(free_space):
raise ArgumentTypeError("There isn't enough free space to decode Dolby Atmos")
else:
return True