From 6bf821217f536073e6f58c97daaf244d4d7f29a7 Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Wed, 28 Dec 2022 15:00:28 -0800 Subject: [PATCH] WIP on #373. --- tests/unit_tests.py | 33 ++++++++++++++++++++++++--------- workbench_utils.py | 10 ++++++---- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/tests/unit_tests.py b/tests/unit_tests.py index beb69e8c..dffc9bd8 100644 --- a/tests/unit_tests.py +++ b/tests/unit_tests.py @@ -194,28 +194,43 @@ class TestMediaTrackString(unittest.TestCase): def test_split_media_track_string_single(self): config = {'subdelimiter': '|'} - res = workbench_utils.split_media_track_string(config, 'en:subtitles:en:/path/to/file') - self.assertDictEqual(res[0], {'label': 'en', 'kind': 'subtitles', 'srclang': 'en', 'file_path': '/path/to/file'}) + res = workbench_utils.split_media_track_string(config, 'Transcript:subtitles:en:/path/to/file') + self.assertDictEqual(res[0], {'label': 'Transcript', 'kind': 'subtitles', 'srclang': 'en', 'file_path': '/path/to/file'}) def test_split_media_track_string_single_windows(self): config = {'subdelimiter': '|'} - res = workbench_utils.split_media_track_string(config, 'en:subtitles:en:c:/path/to/file') - self.assertDictEqual(res[0], {'label': 'en', 'kind': 'subtitles', 'srclang': 'en', 'file_path': 'c:/path/to/file'}) + res = workbench_utils.split_media_track_string(config, 'Foo:subtitles:en:c:/path/to/file') + self.assertDictEqual(res[0], {'label': 'Foo', 'kind': 'subtitles', 'srclang': 'en', 'file_path': 'c:/path/to/file'}) def test_split_media_track_multiple(self): config = {'subdelimiter': '|'} - res = workbench_utils.split_media_track_string(config, 'en:subtitles:en:c:/path/to/file.vtt|fr:subtitles:fr:/path/to/file2.vtt') - self.assertDictEqual(res[0], {'label': 'en', 'kind': 'subtitles', 'srclang': 'en', 'file_path': 'c:/path/to/file.vtt'}) - self.assertDictEqual(res[1], {'label': 'fr', 'kind': 'subtitles', 'srclang': 'fr', 'file_path': '/path/to/file2.vtt'}) + res = workbench_utils.split_media_track_string(config, 'Bar:subtitles:en:c:/path/to/file.vtt|Baz:subtitles:fr:/path/to/file2.vtt') + self.assertDictEqual(res[0], {'label': 'Bar', 'kind': 'subtitles', 'srclang': 'en', 'file_path': 'c:/path/to/file.vtt'}) + self.assertDictEqual(res[1], {'label': 'Baz', 'kind': 'subtitles', 'srclang': 'fr', 'file_path': '/path/to/file2.vtt'}) class TestValidateMediaTrackString(unittest.TestCase): def test_validate_media_track_values(self): - res = workbench_utils.validate_media_track_value('en:subtitles:en:c:/path/to/file') + res = workbench_utils.validate_media_track_value('Transcript:subtitles:en:c:/path/to/file.vtt') self.assertTrue(res) - res = workbench_utils.validate_media_track_value('en:subtitle:en:c:/path/to/file') + res = workbench_utils.validate_media_track_value('Transcript:captions:de:c:/path/to/file.vtt') + self.assertTrue(res) + + res = workbench_utils.validate_media_track_value('Transcript:subtitles:fr:c:/path/to/file.VTT') + self.assertTrue(res) + + res = workbench_utils.validate_media_track_value('Transcript:subtitles:ff:c:/path/to/file.VTT') + self.assertFalse(res) + + res = workbench_utils.validate_media_track_value('Transcript:subtitles:en:c:/path/to/file.ccc') + self.assertFalse(res) + + res = workbench_utils.validate_media_track_value(':subtitles:en:c:/path/to/file.VTT') + self.assertFalse(res) + + res = workbench_utils.validate_media_track_value('Transcript:subtitle:en:c:/path/to/file.vtt') self.assertFalse(res) diff --git a/workbench_utils.py b/workbench_utils.py index e31b7826..181a85ed 100644 --- a/workbench_utils.py +++ b/workbench_utils.py @@ -3879,8 +3879,7 @@ def validate_authority_link_fields(config, field_definitions, csv_data): def validate_media_track_value(media_track_value): - """Validates that the string in "media_track_value" has valid Drupal language - codes and valid 'kind' values. + """Validates that the string in "media_track_value" has valid values in its subparts. """ """Parameters ---------- @@ -3891,9 +3890,12 @@ def validate_media_track_value(media_track_value): boolean True if it does, False if not. """ - valid_kinds = ['subtitles'] + valid_kinds = ['subtitles', 'descriptions', 'metadata', 'captions', 'chapters'] parts = media_track_value.split(':', 3) - if validate_language_code(parts[0]) and validate_language_code(parts[2]) and parts[1] in valid_kinds: + # First part, the label, needs to have a length; second part needs to be one of the + # values in 'valid_kinds'; third part needs to be a valid Drupal language code; the fourth + # part needs to end in '.vtt'. + if len(parts[0]) > 0 and validate_language_code(parts[2]) and parts[1] in valid_kinds and parts[3].lower().endswith('.vtt'): return True else: return False