def load_from(sub_from, type=None, language=None): if hasattr(sub_from, 'read'): if type is None and not getattr(sub_from, 'name', None): raise TypeError("Couldn't find out the type by myself. Care to specify?") extension = sub_from.name.split(".")[-1] # if the file extension is not given or is not a registred # fallback to the given type available_types = get_available_formats() target_type = None if type and type in available_types: target_type = type elif extension and extension in available_types: target_type = extension else: raise TypeError("Type %s is not an available type" % (type or extension)) parser = parsers.discover(target_type) with sub_from: sub_from = sub_from.read() elif isinstance(sub_from, basestring) and type is None: raise TypeError("Couldn't find out the type by myself. Care to specify?") else: parser = parsers.discover(type) no_unicode = getattr(parser, 'NO_UNICODE', False) if not isinstance(sub_from, unicode): if not no_unicode: sub_from = sub_from.decode("utf-8") else: if no_unicode: sub_from = sub_from.encode("utf-8") return parser.parse(sub_from, language=language)
def load_from(sub_from, type=None, language=None): if hasattr(sub_from, 'read'): if type is None and not getattr(sub_from, 'name', None): raise TypeError( "Couldn't find out the type by myself. Care to specify?") extension = sub_from.name.split(".")[-1] # if the file extension is not given or is not a registred # fallback to the given type available_types = get_available_formats() target_type = None if type and type in available_types: target_type = type elif extension and extension in available_types: target_type = extension else: raise TypeError("Type %s is not an available type" % (type or extension)) parser = parsers.discover(target_type) with sub_from: sub_from = sub_from.read() elif isinstance(sub_from, basestring) and type is None: raise TypeError( "Couldn't find out the type by myself. Care to specify?") else: parser = parsers.discover(type) no_unicode = getattr(parser, 'NO_UNICODE', False) if not isinstance(sub_from, unicode): if not no_unicode: sub_from = sub_from.decode("utf-8") else: if no_unicode: sub_from = sub_from.encode("utf-8") return parser.parse(sub_from, language=language)
def loads(self, language_code, content, file_type): try: parser = parsers.discover(file_type) except KeyError: raise TypeError("No parser for %s" % file_type) parsed_subs = parser.parse(content, language=language_code).to_internal() if parser is parsers.DFXPParser: # return the subtitles as-is return parsed_subs ttml = self._empty_ttml(language_code, '', '') self._move_elements(parsed_subs._ttml.find(TTML + 'body'), ttml.find(TTML + 'body')) return storage.SubtitleSet.create_with_raw_ttml(ttml)
def test_dfxp_aliases(self): self.assertTrue(discover('xml'))
def test_parsing_discover_missing(self): with self.assertRaises(KeyError): ParserList['badformat'] with self.assertRaises(KeyError): discover('badformat')
def test_parsing_discover_lowercase(self): self.assertTrue(base.ParserList['srt']) self.assertTrue(base.ParserList['SRT']) self.assertTrue(discover('srt')) self.assertTrue(discover('SRT'))