Skip to content

Commit

Permalink
Ensure opt validation checks the VideoSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
JuniorIsAJitterbug committed Jan 17, 2024
1 parent cbddf04 commit 5712e42
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 88 deletions.
4 changes: 3 additions & 1 deletion src/tbc_video_export/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def _run() -> None:

try:
config = Config()
opts = opts_parser.parse_opts(config)
parser, opts = opts_parser.parse_opts(config)
log.set_verbosity(opts)

files = FileHelper(opts, config)
Expand All @@ -38,6 +38,8 @@ async def _run() -> None:
files,
)

opts_parser.validate_opts(state, parser, opts)

handler = ProcessHandler(state)

logging.getLogger("console").info(
Expand Down
49 changes: 21 additions & 28 deletions src/tbc_video_export/opts/opts_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
from typing import Any

from tbc_video_export.config import Config
from tbc_video_export.program_state import ProgramState


def parse_opts(config: Config, args: list[str] | None = None) -> Opts:
def parse_opts(
config: Config, args: list[str] | None = None
) -> tuple[argparse.ArgumentParser, Opts]:
"""Parse program opts."""
parser = argparse.ArgumentParser(
prog=consts.APPLICATION_NAME,
Expand Down Expand Up @@ -205,18 +208,17 @@ def parse_opts(config: Config, args: list[str] | None = None) -> Opts:
opts_ffmpeg.add_ffmpeg_opts(config, parser)

opts = parser.parse_intermixed_args(args, namespace=Opts())
_validate_opts(config, parser, opts)
return (parser, opts)

return opts


def _validate_opts(config: Config, parser: argparse.ArgumentParser, opts: Opts) -> None:
def validate_opts(
state: ProgramState, parser: argparse.ArgumentParser, opts: Opts
) -> None:
"""Validate any nonsensical opt combinations."""
_validate_line_opts(parser, opts)
_validate_video_system(parser, opts)
_validate_video_system(state, parser, opts)
_validate_ansi_support(opts)
_validate_luma_only_opts(config, parser, opts)
_validate_verbosity_opts(opts)
_validate_luma_only_opts(state, parser, opts)


def _validate_line_opts(parser: argparse.ArgumentParser, opts: Opts) -> None:
Expand All @@ -241,9 +243,11 @@ def _validate_line_opts(parser: argparse.ArgumentParser, opts: Opts) -> None:
)


def _validate_video_system(parser: argparse.ArgumentParser, opts: Opts) -> None:
def _validate_video_system(
state: ProgramState, parser: argparse.ArgumentParser, opts: Opts
) -> None:
# check video system incompatible opts
match opts.video_system:
match state.video_system:
case VideoSystem.PAL | VideoSystem.PAL_M:
if opts.oftest:
parser.error(
Expand All @@ -267,9 +271,6 @@ def _validate_video_system(parser: argparse.ArgumentParser, opts: Opts) -> None:
"arguments --simple-pal: not allowed when --video-system is ntsc"
)

case _:
pass


def _validate_ansi_support(opts: Opts) -> None:
# check if ansi is supported on Windows and disable progress if not
Expand All @@ -291,36 +292,28 @@ def _validate_ansi_support(opts: Opts) -> None:


def _validate_luma_only_opts(
config: Config, parser: argparse.ArgumentParser, opts: Opts
state: ProgramState, parser: argparse.ArgumentParser, opts: Opts
) -> None:
# check luma only redundant opts
if opts.luma_only or opts.luma_4fsc:
if opts.chroma_decoder is not None:
parser.error(
"arguments --chroma-decoder: not allowed when --luma-only or "
"arguments --chroma-decoder: not allowed with --luma-only or "
"--luma-4fsc (redundant)"
)

if opts.profile != config.get_default_profile(ProfileType.DEFAULT).name:
if opts.profile != state.config.get_default_profile(ProfileType.DEFAULT).name:
parser.error(
"arguments --profile: not allowed when --luma-only or "
"--luma-4fsc (redundant)"
"arguments --profile: not allowed with --luma-only or "
"--luma-4fsc (redundant), try --profile-luma"
)

elif opts.profile_luma != config.get_default_profile(ProfileType.LUMA).name:
elif opts.profile_luma != state.config.get_default_profile(ProfileType.LUMA).name:
parser.error(
"arguments --profile-luma: not allowed when not --luma-only or "
"--luma-4fsc (redundant)"
"arguments --profile-luma: only allowed with --luma-only or --luma-4fsc"
)


def _validate_verbosity_opts(opts: Opts) -> None:
# verbosity
# disable debug logging to console if progress is displayed or quiet enabled
if opts.debug and opts.no_progress:
logging.getLogger("console").setLevel(logging.DEBUG)


class _ActionDumpConfig(argparse.Action):
def __init__(self, config: Config, nargs: int = 0, **kwargs: Any) -> None:
self._config = config
Expand Down
10 changes: 6 additions & 4 deletions tests/test_verbosity_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def setUp(self) -> None: # noqa: D102
self.parse_opts = partial(opts_parser.parse_opts, self.config)

def test_quiet_mode(self) -> None: # noqa: D102
opts = self.parse_opts([str(self.path),"pal_svideo", "-q"])
_, opts = self.parse_opts([str(self.path), "pal_svideo", "-q"])
self.files = FileHelper(opts, self.config)
log.set_verbosity(opts)

Expand All @@ -36,8 +36,8 @@ def test_quiet_mode(self) -> None: # noqa: D102
self.assertEqual(logging.getLogger("console").level, logging.ERROR)

def test_debug_mode(self) -> None: # noqa: D102
opts = self.parse_opts(
[str(self.path),"pal_svideo", "-d", "--no-progress", "--no-debug-log"]
_, opts = self.parse_opts(
[str(self.path), "pal_svideo", "-d", "--no-progress", "--no-debug-log"]
)
self.files = FileHelper(opts, self.config)
log.set_verbosity(opts)
Expand All @@ -48,7 +48,9 @@ def test_debug_mode(self) -> None: # noqa: D102
# self.assertGreater(len(logging.getLogger("console").handlers), 1)

def test_show_process_output(self) -> None: # noqa: D102
opts = self.parse_opts([str(self.path),"pal_svideo", "--show-process-output"])
_, opts = self.parse_opts(
[str(self.path), "pal_svideo", "--show-process-output"]
)
self.files = FileHelper(opts, self.config)
log.set_verbosity(opts)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_wrappers_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def setUp(self) -> None: # noqa: D102

def test_ffmpeg_audio_track_opts(self) -> None: # noqa: D102
audio_track = "tests/files/audio.flac"
opts = self.parse_opts(
_, opts = self.parse_opts(
[str(self.path), "pal_svideo", "--audio-track", "tests/files/audio.flac"]
)
self.files = FileHelper(opts, self.config)
Expand All @@ -53,7 +53,7 @@ def test_ffmpeg_audio_track_opts(self) -> None: # noqa: D102

def test_ffmpeg_audio_track_advanced_opts(self) -> None: # noqa: D102
audio_track = "tests/files/audio.flac"
opts = self.parse_opts(
_, opts = self.parse_opts(
[
str(self.path),
"pal_svideo",
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_ffmpeg_audio_track_advanced_opts(self) -> None: # noqa: D102
)

def test_ffmpeg_metadata_opts(self) -> None: # noqa: D102
opts = self.parse_opts(
_, opts = self.parse_opts(
[
str(self.path),
"pal_svideo",
Expand Down
24 changes: 12 additions & 12 deletions tests/test_wrappers_ldtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def setUp(self) -> None: # noqa: D102
self.pipe = PipeFactory.create_dummy_pipe()

def test_process_vbi_default_opts(self) -> None: # noqa: D102
opts = self.parse_opts(
_, opts = self.parse_opts(
[str(self.path), "pal_svideo", "--threads", "4", "--process-vbi"]
)
self.files = FileHelper(opts, self.config)
Expand All @@ -62,7 +62,7 @@ def test_process_vbi_custom_json(self) -> None: # noqa: D102
Path(__file__).parent, "files", "ntsc_svideo.tbc.json"
)

opts = self.parse_opts(
_, opts = self.parse_opts(
[
str(self.path),
"pal_svideo",
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_process_vbi_custom_json(self) -> None: # noqa: D102
)

def test_dropout_correct_default_opts(self) -> None: # noqa: D102
opts = opts_parser.parse_opts(
_, opts = opts_parser.parse_opts(
self.config, [str(self.path), "pal_svideo", "--threads", "4"]
)
self.files = FileHelper(opts, self.config)
Expand All @@ -118,7 +118,7 @@ def test_dropout_correct_default_opts(self) -> None: # noqa: D102
)

def test_decoder_invalid_videosystem(self) -> None: # noqa: D102
opts = self.parse_opts(
_, opts = self.parse_opts(
[str(self.path), "pal_svideo", "--chroma-decoder", "ntsc2d"]
)
self.files = FileHelper(opts, self.config)
Expand All @@ -137,7 +137,7 @@ def test_decoder_invalid_videosystem(self) -> None: # noqa: D102

def test_decoder_letterbox_pal(self) -> None: # noqa: D102
path = Path.joinpath(Path(__file__).parent, "files", "pal_svideo")
opts = self.parse_opts([str(path), "pal_svideo", "--letterbox"])
_, opts = self.parse_opts([str(path), "pal_svideo", "--letterbox"])
self.files = FileHelper(opts, self.config)
state = ProgramState(opts, self.config, self.files)

Expand All @@ -159,7 +159,7 @@ def test_decoder_letterbox_pal(self) -> None: # noqa: D102

def test_decoder_letterbox_ntsc(self) -> None: # noqa: D102
path = Path.joinpath(Path(__file__).parent, "files", "ntsc_svideo")
opts = self.parse_opts([str(path), "ntsc_svideo", "--letterbox"])
_, opts = self.parse_opts([str(path), "ntsc_svideo", "--letterbox"])
self.files = FileHelper(opts, self.config)
state = ProgramState(opts, self.config, self.files)

Expand All @@ -181,7 +181,7 @@ def test_decoder_letterbox_ntsc(self) -> None: # noqa: D102

def test_decoder_letterbox_palm(self) -> None: # noqa: D102
path = Path.joinpath(Path(__file__).parent, "files", "palm_svideo")
opts = self.parse_opts([str(path), "palm_svideo", "--letterbox"])
_, opts = self.parse_opts([str(path), "palm_svideo", "--letterbox"])
self.files = FileHelper(opts, self.config)
state = ProgramState(opts, self.config, self.files)

Expand All @@ -198,7 +198,7 @@ def test_decoder_letterbox_palm(self) -> None: # noqa: D102

def test_decoder_vbi_pal(self) -> None: # noqa: D102
path = Path.joinpath(Path(__file__).parent, "files", "pal_svideo")
opts = self.parse_opts([str(path), "pal_svideo", "--vbi"])
_, opts = self.parse_opts([str(path), "pal_svideo", "--vbi"])
self.files = FileHelper(opts, self.config)
state = ProgramState(opts, self.config, self.files)

Expand All @@ -217,7 +217,7 @@ def test_decoder_vbi_pal(self) -> None: # noqa: D102

def test_decoder_vbi_ntsc(self) -> None: # noqa: D102
path = Path.joinpath(Path(__file__).parent, "files", "ntsc_svideo")
opts = self.parse_opts([str(path), "ntsc_svideo", "--vbi"])
_, opts = self.parse_opts([str(path), "ntsc_svideo", "--vbi"])
self.files = FileHelper(opts, self.config)
state = ProgramState(opts, self.config, self.files)

Expand All @@ -239,7 +239,7 @@ def test_decoder_vbi_ntsc(self) -> None: # noqa: D102

def test_decoder_vbi_palm(self) -> None: # noqa: D102
path = Path.joinpath(Path(__file__).parent, "files", "palm_svideo")
opts = self.parse_opts([str(path), "palm_svideo", "--vbi"])
_, opts = self.parse_opts([str(path), "palm_svideo", "--vbi"])
self.files = FileHelper(opts, self.config)
state = ProgramState(opts, self.config, self.files)

Expand All @@ -261,7 +261,7 @@ def test_decoder_vbi_palm(self) -> None: # noqa: D102
def test_decoder_nr_gain_svideo(self) -> None: # noqa: D102
path = Path.joinpath(Path(__file__).parent, "files", "pal_svideo")
tbc_json = Path.joinpath(Path(__file__).parent, "files", "pal_svideo.tbc.json")
opts = opts_parser.parse_opts(
_, opts = opts_parser.parse_opts(
self.config,
[
str(path),
Expand Down Expand Up @@ -338,7 +338,7 @@ def test_decoder_nr_gain_cvbs(self) -> None: # noqa: D102
tbc_json = Path.joinpath(
Path(__file__).parent, "files", "pal_composite.tbc.json"
)
opts = opts_parser.parse_opts(
_, opts = opts_parser.parse_opts(
self.config,
[
str(path),
Expand Down
10 changes: 5 additions & 5 deletions tests/test_wrappers_ntsc_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ def setUp(self) -> None: # noqa: D102
self.pipe = PipeFactory.create_dummy_pipe()

def test_videosystem(self) -> None: # noqa: D102
opts = self.parse_opts(
_, opts = self.parse_opts(
[str(self.path), "ntsc_composite", "--input-tbc-json", str(self.tbc_json)]
)
self.files = FileHelper(opts, self.config)
self.assertTrue(self.files.tbc_json.video_system, VideoSystem.NTSC)

def test_default_decoder_ntsc_cvbs(self) -> None: # noqa: D102
opts = opts_parser.parse_opts(
_, opts = opts_parser.parse_opts(
self.config,
[
str(self.path),
Expand Down Expand Up @@ -126,7 +126,7 @@ def test_ffmpeg_opts_ntsc_cvbs(self) -> None:
MaxSlicesCount : 24
ErrorDetectionType : Per slice
""" # noqa: E501
opts = opts_parser.parse_opts(
_, opts = opts_parser.parse_opts(
self.config,
[
str(self.path),
Expand Down Expand Up @@ -243,7 +243,7 @@ def test_ffmpeg_opts_ntsc_cvbs_luma(self) -> None:
MaxSlicesCount : 24
ErrorDetectionType : Per slice
""" # noqa: E501
opts = self.parse_opts(
_, opts = self.parse_opts(
[
str(self.path),
"ntsc_composite",
Expand Down Expand Up @@ -356,7 +356,7 @@ def test_ffmpeg_opts_ntsc_cvbs_luma_4fsc(self) -> None:
MaxSlicesCount : 24
ErrorDetectionType : Per slice
""" # noqa: E501
opts = self.parse_opts(
_, opts = self.parse_opts(
[
str(self.path),
"ntsc_composite",
Expand Down
12 changes: 6 additions & 6 deletions tests/test_wrappers_ntsc_composite_ld.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setUp(self) -> None: # noqa: D102
self.maxDiff = 99999

def test_videosystem(self) -> None: # noqa: D102
opts = self.parse_opts(
_, opts = self.parse_opts(
[
str(self.path),
"ntsc_composite_ld",
Expand All @@ -46,7 +46,7 @@ def test_videosystem(self) -> None: # noqa: D102
self.assertTrue(self.files.tbc_json.video_system, VideoSystem.NTSC)

def test_ld_detect(self) -> None: # noqa: D102
opts = self.parse_opts(
_, opts = self.parse_opts(
[
str(self.path),
"ntsc_composite_ld",
Expand All @@ -58,7 +58,7 @@ def test_ld_detect(self) -> None: # noqa: D102
self.assertTrue(self.files.is_combined_ld)

def test_default_decoder_ntsc_ld(self) -> None: # noqa: D102
opts = opts_parser.parse_opts(
_, opts = opts_parser.parse_opts(
self.config,
[
str(self.path),
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_ffmpeg_opts_ntsc_ld(self) -> None:
MaxSlicesCount : 24
ErrorDetectionType : Per slice
""" # noqa: E501
opts = opts_parser.parse_opts(
_, opts = opts_parser.parse_opts(
self.config,
[
str(self.path),
Expand Down Expand Up @@ -261,7 +261,7 @@ def test_ffmpeg_opts_ntsc_ld_luma(self) -> None:
MaxSlicesCount : 24
ErrorDetectionType : Per slice
""" # noqa: E501
opts = self.parse_opts(
_, opts = self.parse_opts(
[
str(self.path),
"ntsc_composite_ld",
Expand Down Expand Up @@ -374,7 +374,7 @@ def test_ffmpeg_opts_ntsc_ld_luma_4fsc(self) -> None:
MaxSlicesCount : 24
ErrorDetectionType : Per slice
""" # noqa: E501
opts = self.parse_opts(
_, opts = self.parse_opts(
[
str(self.path),
"ntsc_composite_ld",
Expand Down
Loading

0 comments on commit 5712e42

Please sign in to comment.