From e069afb8b8818ef0e0e642b069b161598691a2ef Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Tue, 19 Dec 2023 09:52:51 -0800 Subject: [PATCH] more custom regex functions --- .../entries/script/function_scripts.py | 33 +++++++++++++++++-- .../script/functions/error_functions.py | 24 ++++++++++++++ tests/e2e/plugins/test_regex.py | 2 +- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/ytdl_sub/entries/script/function_scripts.py b/src/ytdl_sub/entries/script/function_scripts.py index 696bef35..3a111766 100644 --- a/src/ytdl_sub/entries/script/function_scripts.py +++ b/src/ytdl_sub/entries/script/function_scripts.py @@ -41,7 +41,8 @@ CUSTOM_FUNCTION_SCRIPTS: Dict[str, str] = { # $0 - input variable # $1 - regex array # $2 - defaults - "%regex_capture": """{ + # $3 - error message + "%regex_capture_inner": """{ %assert_then( %array_reduce( %array_apply_fixed( @@ -63,7 +64,35 @@ CUSTOM_FUNCTION_SCRIPTS: Dict[str, str] = { %array_extend( ['using all defaults'], $2 ), True ), - 'Number of regex capture groups must be less than or equal to the number of defaults' + $3 + ) + }""", + "%regex_capture_many_required": """{ + %assert_ne( + %regex_capture_inner( + $0, $1, ['', '', '', '', '', '', '', '', '', ''], + 'When using %regex_capture_many, number of regex capture groups must be less than or equal to the number of defaults' + ), + ['using all defaults', '', '', '', '', '', '', '', '', '', ''], + 'When running %regex_capture_many_required, no regex strings captured' + ) + }""", + "%regex_capture_many_with_defaults": """{ + %regex_capture_inner( + $0, $1, $2, + 'When using %regex_capture_with_defaults, number of regex capture groups must be less than or equal to the number of defaults' + ) + }""", + "%regex_search_any": """{ + %ne( + %array_at( + %regex_capture_inner( + $0, $1, [], + 'When using %regex_search_many, all regex strings must contain no capture groups' + ), + 0 + ), + 'using all defaults' ) }""", } diff --git a/src/ytdl_sub/script/functions/error_functions.py b/src/ytdl_sub/script/functions/error_functions.py index a5132bda..5be2eb3b 100644 --- a/src/ytdl_sub/script/functions/error_functions.py +++ b/src/ytdl_sub/script/functions/error_functions.py @@ -33,3 +33,27 @@ class ErrorFunctions: if not bool(value.value): raise UserThrownRuntimeError(assert_message) return ret + + @staticmethod + def assert_eq( + value: ReturnableArgument, equals: AnyArgument, assert_message: String + ) -> ReturnableArgument: + """ + Explicitly throw an error with the provided assert message if ``value`` does not equal + ``equals``. If they do equal, then return ``value``. + """ + if not value.value == equals.value: + raise UserThrownRuntimeError(assert_message) + return value + + @staticmethod + def assert_ne( + value: ReturnableArgument, equals: AnyArgument, assert_message: String + ) -> ReturnableArgument: + """ + Explicitly throw an error with the provided assert message if ``value`` equals + ``equals``. If they do equal, then return ``value``. + """ + if value.value == equals.value: + raise UserThrownRuntimeError(assert_message) + return value \ No newline at end of file diff --git a/tests/e2e/plugins/test_regex.py b/tests/e2e/plugins/test_regex.py index 97a5273f..63b4be53 100644 --- a/tests/e2e/plugins/test_regex.py +++ b/tests/e2e/plugins/test_regex.py @@ -77,7 +77,7 @@ def regex_subscription_dict(regex_subscription_dict_base, output_directory): }, "overrides": { "title_capture_list": f"""{{ - %regex_capture( + %regex_capture_many_with_defaults( title, [ "should not cap (.+) - (.+)",