Exemple #1
0
    def _parse(self, lines):
        self._compute_blocks(lines)

        for block in self.blocks:
            if self._is_cue_block(block):
                caption = self._parse_cue_block(block)
                self.captions.append(caption)
            elif self._is_comment_block(block):
                continue
            elif self._is_style_block(block):
                if self.captions:
                    raise MalformedFileError(
                        'Style block defined after the first cue in line {}.'.
                        format(block.line_number))
                style = Style()
                style.lines = block.lines[1:]
                self.styles.append(style)
            else:
                if len(block.lines) == 1:
                    raise MalformedCaptionError(
                        'Standalone cue identifier in line {}.'.format(
                            block.line_number))
                else:
                    raise MalformedCaptionError(
                        'Missing timing cue in line {}.'.format(
                            block.line_number + 1))
Exemple #2
0
    def _read_content(self, file):
        with open(file, encoding='utf-8') as f:
            lines = [line.rstrip() for line in f.readlines()]

        if not lines:
            raise MalformedFileError('The file is empty.')

        return lines
Exemple #3
0
    def _read_content(self, file):
        with open(file, 'rb') as f:
            raw = f.read(32)
            encoding = chardet.detect(raw)['encoding']

        with open(file, encoding=encoding) as f:
            lines = [line.rstrip() for line in f.readlines()]

        if not lines:
            raise MalformedFileError('The file is empty.')

        return lines
Exemple #4
0
    def _read_content(self, file):

        first_bytes = min(32, os.path.getsize(file))
        with open(file, 'rb') as f:
            raw = f.read(first_bytes)

        if raw.startswith(codecs.BOM_UTF8):
            encoding = 'utf-8-sig'
        else:
            encoding = 'utf-8'

        with open(file, encoding=encoding) as f:
            lines = [line.rstrip('\n') for line in f.readlines()]

        if not lines:
            raise MalformedFileError('The file is empty.')

        return lines
Exemple #5
0
 def _validate(self, lines):
     if not self._validate_timeframe_line(lines[0]):
         raise MalformedFileError('The file does not have a valid format')
Exemple #6
0
 def _validate(self, lines):
     if not re.match('WEBVTT', lines[0]):
         raise MalformedFileError('The file does not have a valid format')
Exemple #7
0
 def _validate(self, lines):
     if len(lines
            ) < 2 or lines[0] != '1' or not self._validate_timeframe_line(
                lines[1]):
         raise MalformedFileError('The file does not have a valid format.')
Exemple #8
0
 def _validate(self, lines):
     if 'WEBVTT' not in lines[0]:
         raise MalformedFileError('The file does not have a valid format')