|
22 | 22 | from opaque_keys.edx.locator import LibraryLocatorV2 |
23 | 23 |
|
24 | 24 | from xblocks_contrib.video.content import StaticContent |
25 | | -from xblocks_contrib.video.exceptions import NotFoundError, TranscriptsGenerationException |
| 25 | +from xblocks_contrib.video.exceptions import NotFoundError, TranscriptNotFoundError, TranscriptsGenerationException |
26 | 26 |
|
27 | 27 |
|
28 | 28 | try: |
|
36 | 36 | NON_EXISTENT_TRANSCRIPT = 'non_existent_dummy_file_name' |
37 | 37 |
|
38 | 38 |
|
| 39 | +def get_transcript_from_store(video_block, location, subs_id, lang='en', filename=None): |
| 40 | + """ |
| 41 | + Get transcript from video config service. |
| 42 | + """ |
| 43 | + video_config_service = video_block.runtime.service(video_block, 'video_config') |
| 44 | + if not video_config_service: |
| 45 | + raise TranscriptNotFoundError("Video config service was not found") |
| 46 | + |
| 47 | + # HACK Warning! this is temporary and will be removed once edx-val take over the |
| 48 | + # transcript module and contentstore will only function as fallback until all the |
| 49 | + # data is migrated to edx-val. It will be saving a contentstore hit for a hardcoded |
| 50 | + # dummy-non-existent-transcript name. |
| 51 | + if NON_EXISTENT_TRANSCRIPT in [subs_id, filename]: |
| 52 | + raise TranscriptNotFoundError |
| 53 | + asset_filename = subs_filename(subs_id, lang) if not filename else filename |
| 54 | + return video_config_service.get_transcript_from_store(location.course_key, asset_filename) |
| 55 | + |
| 56 | + |
39 | 57 | class TranscriptException(Exception): |
40 | 58 | pass |
41 | 59 |
|
@@ -281,12 +299,12 @@ def save_subs_to_store(video_block, subs, subs_id, item, language='en'): |
281 | 299 | # return subs |
282 | 300 |
|
283 | 301 |
|
284 | | -def remove_subs_from_store(subs_id, item, lang='en'): |
285 | | - """ |
286 | | - Remove from store, if transcripts content exists. |
287 | | - """ |
288 | | - filename = subs_filename(subs_id, lang) |
289 | | - Transcript.delete_asset(item, item.location, filename) |
| 302 | +# def remove_subs_from_store(subs_id, item, lang='en'): |
| 303 | +# """ |
| 304 | +# Remove from store, if transcripts content exists. |
| 305 | +# """ |
| 306 | +# filename = subs_filename(subs_id, lang) |
| 307 | +# Transcript.delete_asset(item, item.location, filename) |
290 | 308 |
|
291 | 309 |
|
292 | 310 | def generate_subs_from_source(video_block, speed_subs, subs_type, subs_filedata, block, language='en'): |
@@ -494,7 +512,10 @@ def manage_video_subtitles_save(item, user, old_metadata=None, generate_translat |
494 | 512 | for lang in old_langs.difference(new_langs): # 3a |
495 | 513 | for video_id in possible_video_id_list: |
496 | 514 | if video_id: |
497 | | - remove_subs_from_store(video_id, item, lang) |
| 515 | + # remove_subs_from_store(video_id, item, lang) |
| 516 | + filename = subs_filename(video_id, lang) |
| 517 | + video_config_service = item.runtime.service(item, 'video_config') |
| 518 | + video_config_service.delete_transcript_from_store(item.location.course_key, filename) |
498 | 519 |
|
499 | 520 | reraised_message = '' |
500 | 521 | if not isinstance(item.usage_key.context_key, LibraryLocatorV2): |
@@ -539,9 +560,13 @@ def generate_sjson_for_all_speeds(block, user_filename, result_subs_dict, lang): |
539 | 560 | """ |
540 | 561 | _ = block.runtime.service(block, "i18n").gettext |
541 | 562 |
|
| 563 | + video_config_service = block.runtime.service(block, 'video_config') |
| 564 | + if not video_config_service: |
| 565 | + raise TranscriptNotFoundError("Video config service was not found") |
| 566 | + |
542 | 567 | try: |
543 | | - srt_transcripts = Transcript.find_transcript_from_store(block, block.location.course_key, user_filename) |
544 | | - except NotFoundError as ex: |
| 568 | + srt_transcripts = video_config_service.find_transcript_from_store(block.location.course_key, user_filename) |
| 569 | + except TranscriptNotFoundError as ex: |
545 | 570 | raise TranscriptException(_("{exception_message}: Can't find uploaded transcripts: {user_filename}").format( # lint-amnesty, pylint: disable=raise-missing-from |
546 | 571 | exception_message=str(ex), |
547 | 572 | user_filename=user_filename |
@@ -579,10 +604,10 @@ def get_or_create_sjson(block, transcripts): |
579 | 604 | user_subs_id = os.path.splitext(user_filename)[0] |
580 | 605 | source_subs_id, result_subs_dict = user_subs_id, {1.0: user_subs_id} |
581 | 606 | try: |
582 | | - sjson_transcript = Transcript.asset(block, block.location, source_subs_id, block.transcript_language).data |
583 | | - except NotFoundError: # generating sjson from srt |
| 607 | + sjson_transcript = get_transcript_from_store(block, block.location, source_subs_id, block.transcript_language).data |
| 608 | + except TranscriptNotFoundError: # generating sjson from srt |
584 | 609 | generate_sjson_for_all_speeds(block, user_filename, result_subs_dict, block.transcript_language) |
585 | | - sjson_transcript = Transcript.asset(block, block.location, source_subs_id, block.transcript_language).data |
| 610 | + sjson_transcript = get_transcript_from_store(block, block.location, source_subs_id, block.transcript_language).data |
586 | 611 | return sjson_transcript |
587 | 612 |
|
588 | 613 |
|
@@ -710,14 +735,6 @@ class Transcript: |
710 | 735 | SJSON: 'application/json', |
711 | 736 | } |
712 | 737 |
|
713 | | - @staticmethod |
714 | | - def find_transcript_from_store(video_block, course_key, filename): |
715 | | - """ |
716 | | - Find transcript from store by course_key and filename. |
717 | | - """ |
718 | | - video_config_service = video_block.runtime.service(video_block, 'video_config') |
719 | | - return video_config_service.find_transcript_from_store(course_key, filename) |
720 | | - |
721 | 738 | @staticmethod |
722 | 739 | def convert(content, input_format, output_format): |
723 | 740 | """ |
@@ -780,48 +797,48 @@ def convert(content, input_format, output_format): |
780 | 797 | elif output_format == 'srt': |
781 | 798 | return generate_srt_from_sjson(content_dict, speed=1.0) |
782 | 799 |
|
783 | | - @staticmethod |
784 | | - def asset(video_block, location, subs_id, lang='en', filename=None): |
785 | | - """ |
786 | | - Get asset from contentstore, asset location is built from subs_id and lang. |
787 | | -
|
788 | | - `location` is block location. |
789 | | - """ |
790 | | - # HACK Warning! this is temporary and will be removed once edx-val take over the |
791 | | - # transcript module and contentstore will only function as fallback until all the |
792 | | - # data is migrated to edx-val. It will be saving a contentstore hit for a hardcoded |
793 | | - # dummy-non-existent-transcript name. |
794 | | - if NON_EXISTENT_TRANSCRIPT in [subs_id, filename]: |
795 | | - raise NotFoundError |
796 | | - |
797 | | - asset_filename = subs_filename(subs_id, lang) if not filename else filename |
798 | | - return Transcript.get_asset(video_block, location, asset_filename) |
799 | | - |
800 | | - @staticmethod |
801 | | - def get_asset(video_block, location, filename): |
802 | | - """ |
803 | | - Return asset by location and filename. |
804 | | - """ |
805 | | - video_config_service = video_block.runtime.service(video_block, 'video_config') |
806 | | - return video_config_service.get_transcript_from_store(location.course_key, filename) |
807 | | - |
808 | | - @staticmethod |
809 | | - def asset_location(location, filename): |
810 | | - """ |
811 | | - Return asset location. `location` is block location. |
812 | | - """ |
813 | | - # If user transcript filename is empty, raise `TranscriptException` to avoid `InvalidKeyError`. |
814 | | - if not filename: |
815 | | - raise TranscriptException("Transcript not uploaded yet") |
816 | | - return StaticContent.compute_location(location.course_key, filename) |
817 | | - |
818 | | - @staticmethod |
819 | | - def delete_asset(video_block, location, filename): |
820 | | - """ |
821 | | - Delete asset by location and filename. |
822 | | - """ |
823 | | - video_config_service = video_block.runtime.service(video_block, 'video_config') |
824 | | - video_config_service.delete_transcript_from_store(location.course_key, filename) |
| 800 | + # @staticmethod |
| 801 | + # def asset(video_block, location, subs_id, lang='en', filename=None): |
| 802 | + # """ |
| 803 | + # Get asset from contentstore, asset location is built from subs_id and lang. |
| 804 | + |
| 805 | + # `location` is block location. |
| 806 | + # """ |
| 807 | + # # HACK Warning! this is temporary and will be removed once edx-val take over the |
| 808 | + # # transcript module and contentstore will only function as fallback until all the |
| 809 | + # # data is migrated to edx-val. It will be saving a contentstore hit for a hardcoded |
| 810 | + # # dummy-non-existent-transcript name. |
| 811 | + # if NON_EXISTENT_TRANSCRIPT in [subs_id, filename]: |
| 812 | + # raise NotFoundError |
| 813 | + |
| 814 | + # asset_filename = subs_filename(subs_id, lang) if not filename else filename |
| 815 | + # return Transcript.get_asset(video_block, location, asset_filename) |
| 816 | + |
| 817 | + # @staticmethod |
| 818 | + # def get_asset(video_block, location, filename): |
| 819 | + # """ |
| 820 | + # Return asset by location and filename. |
| 821 | + # """ |
| 822 | + # video_config_service = video_block.runtime.service(video_block, 'video_config') |
| 823 | + # return video_config_service.get_transcript_from_store(location.course_key, filename) |
| 824 | + |
| 825 | + # @staticmethod |
| 826 | + # def asset_location(location, filename): |
| 827 | + # """ |
| 828 | + # Return asset location. `location` is block location. |
| 829 | + # """ |
| 830 | + # # If user transcript filename is empty, raise `TranscriptException` to avoid `InvalidKeyError`. |
| 831 | + # if not filename: |
| 832 | + # raise TranscriptException("Transcript not uploaded yet") |
| 833 | + # return StaticContent.compute_location(location.course_key, filename) |
| 834 | + |
| 835 | + # @staticmethod |
| 836 | + # def delete_asset(video_block, location, filename): |
| 837 | + # """ |
| 838 | + # Delete asset by location and filename. |
| 839 | + # """ |
| 840 | + # video_config_service = video_block.runtime.service(video_block, 'video_config') |
| 841 | + # video_config_service.delete_transcript_from_store(location.course_key, filename) |
825 | 842 |
|
826 | 843 |
|
827 | 844 | class VideoTranscriptsMixin: |
@@ -993,11 +1010,11 @@ def get_transcript_for_video(video_block, video_location, subs_id, file_name, la |
993 | 1010 | try: |
994 | 1011 | if subs_id is None: |
995 | 1012 | raise NotFoundError |
996 | | - content = Transcript.asset(video_block, video_location, subs_id, language).data.decode('utf-8') |
| 1013 | + content = get_transcript_from_store(video_block, video_location, subs_id, language).data.decode('utf-8') |
997 | 1014 | base_name = subs_id |
998 | 1015 | input_format = Transcript.SJSON |
999 | 1016 | except NotFoundError: |
1000 | | - content = Transcript.asset(video_block, video_location, None, language, file_name).data.decode('utf-8') |
| 1017 | + content = get_transcript_from_store(video_block, video_location, None, language, file_name).data.decode('utf-8') |
1001 | 1018 | base_name = os.path.splitext(file_name)[0] |
1002 | 1019 | input_format = Transcript.SRT |
1003 | 1020 |
|
|
0 commit comments