formatters.py python 2 compliant and misc.
Remove use of ABC, not part of Python 2 and implementing ABCMeta changes between Python 2 and 3 so left it out entirely. Base class now raises NotImplementedError manually. Fix parse_timecode issue with start and end times being identical Replaced uses of F-strings with .format() also for compatibility.
This commit is contained in:
		
							parent
							
								
									74d36a821e
								
							
						
					
					
						commit
						c4b8b5b18d
					
				|  | @ -1,5 +1,3 @@ | ||||||
| from abc import ABC |  | ||||||
| from abc import abstractclassmethod |  | ||||||
| from collections import defaultdict | from collections import defaultdict | ||||||
| import json | import json | ||||||
| import re | import re | ||||||
|  | @ -17,17 +15,18 @@ def parse_timecode(time): | ||||||
|     >>> parse_timecode(6.93) |     >>> parse_timecode(6.93) | ||||||
|     '00:00:06,930' |     '00:00:06,930' | ||||||
|     """ |     """ | ||||||
|  |      | ||||||
|     time = float(time) |     time = float(time) | ||||||
|     hours, mins, secs = ( |     times = { | ||||||
|         str(int(time)//3600).rjust(2, '0'), |         'hours': str(int(time) // 3600).rjust(2, '0'), | ||||||
|         str(int(time)//60).rjust(2, '0'), |         'mins': str(int(time) // 60).rjust(2, '0'), | ||||||
|         str(int(time)%60).rjust(2, '0'), |         'secs': str(int(time) % 60).rjust(2, '0'), | ||||||
|     ) |         'ms': str(int(round((time - int(time))*1000, 2))).rjust(3, '0') | ||||||
|     ms = str(int(round((time - int(time))*1000, 2))).rjust(3, '0') |     } | ||||||
|     return f"{hours}:{mins}:{secs},{ms}" |     return "{hours}:{mins}:{secs},{ms}".format(**times) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class TranscriptFormatter(ABC): | class TranscriptFormatter(object): | ||||||
|     """Abstract Base TranscriptFormatter class |     """Abstract Base TranscriptFormatter class | ||||||
| 
 | 
 | ||||||
|     This class should be inherited from to create additional |     This class should be inherited from to create additional | ||||||
|  | @ -51,7 +50,7 @@ class TranscriptFormatter(ABC): | ||||||
|         return cls.DELIMITER.join( |         return cls.DELIMITER.join( | ||||||
|                 str(transcript) for transcript in transcripts) |                 str(transcript) for transcript in transcripts) | ||||||
| 
 | 
 | ||||||
|     @abstractclassmethod |     @classmethod | ||||||
|     def format(cls, transcript_data): |     def format(cls, transcript_data): | ||||||
|         """Any subclass must implement this format class method. |         """Any subclass must implement this format class method. | ||||||
| 
 | 
 | ||||||
|  | @ -61,7 +60,9 @@ class TranscriptFormatter(ABC): | ||||||
|          as a string. |          as a string. | ||||||
|         :rtype: list[str] |         :rtype: list[str] | ||||||
|         """ |         """ | ||||||
|         pass |         raise NotImplementedError( | ||||||
|  |             cls.__name__ + '.format' | ||||||
|  |         ) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class JSONTranscriptFormatter(TranscriptFormatter): | class JSONTranscriptFormatter(TranscriptFormatter): | ||||||
|  | @ -103,22 +104,20 @@ class SRTTranscriptFormatter(TranscriptFormatter): | ||||||
|         output = [] |         output = [] | ||||||
|         for frame, item in enumerate(transcript_data, start=1): |         for frame, item in enumerate(transcript_data, start=1): | ||||||
|             start_time = float(item.get('start')) |             start_time = float(item.get('start')) | ||||||
|             duration = float(item.get('dur', '0.0')) |             duration = float(item.get('duration', '0.0')) | ||||||
| 
 |  | ||||||
|             end_time = parse_timecode(start_time + duration) |  | ||||||
|             start_time = parse_timecode(start_time) |  | ||||||
| 
 | 
 | ||||||
|             output.append("{frame}\n".format(frame=frame)) |             output.append("{frame}\n".format(frame=frame)) | ||||||
|             output.append("{start_time} --> {end_time}\n".format( |             output.append("{start_time} --> {end_time}\n".format( | ||||||
|                 start_time=start_time, end_time=end_time)) |                 start_time=parse_timecode(start_time), | ||||||
|  |                 end_time=parse_timecode(start_time + duration) | ||||||
|  |             )) | ||||||
|             output.append("{text}".format(text=item.get('text'))) |             output.append("{text}".format(text=item.get('text'))) | ||||||
|             if frame < len(transcript_data): |             if frame < len(transcript_data): | ||||||
|                 output.append('\n\n') |                 output.append('\n\n') | ||||||
| 
 |  | ||||||
|         return '{}\n'.format(''.join(output)) |         return '{}\n'.format(''.join(output)) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class TranscriptFormatterFactory: | class TranscriptFormatterFactory(object): | ||||||
|     """A Transcript Class Factory |     """A Transcript Class Factory | ||||||
| 
 | 
 | ||||||
|     Allows for adding additional custom Transcript classes for the API |     Allows for adding additional custom Transcript classes for the API | ||||||
|  | @ -139,8 +138,10 @@ class TranscriptFormatterFactory: | ||||||
|         :rtype None |         :rtype None | ||||||
|         """ |         """ | ||||||
|         if not issubclass(formatter_class, TranscriptFormatter): |         if not issubclass(formatter_class, TranscriptFormatter): | ||||||
|             raise TypeError( |             raise TypeError(( | ||||||
|                 f'{formatter_class} must be a subclass of TranscriptFormatter') |                 '{0} must be a subclass of TranscriptFormatter' | ||||||
|  |                 ).format(formatter_class) | ||||||
|  |             ) | ||||||
|         self._formatters.update({name: formatter_class}) |         self._formatters.update({name: formatter_class}) | ||||||
| 
 | 
 | ||||||
|     def add_formatters(self, formatters_dict): |     def add_formatters(self, formatters_dict): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue