Skip to content

Commit

Permalink
WIP on #373.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Dec 28, 2022
1 parent 558995d commit 6bf8212
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
33 changes: 24 additions & 9 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
10 changes: 6 additions & 4 deletions workbench_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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
Expand Down

0 comments on commit 6bf8212

Please sign in to comment.