-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add description and how to fix errors
- Loading branch information
Showing
5 changed files
with
236 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,109 @@ | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
from m3u8 import protocol | ||
|
||
|
||
# You must use at least protocol version 2 if you have IV in EXT-X-KEY. | ||
def valid_iv_in_EXT_X_KEY(line: str, version: float): | ||
if not protocol.ext_x_key in line: | ||
return True | ||
@dataclass | ||
class VersionMatchingError(Exception): | ||
line_number: int | ||
line: str | ||
how_to_fix: str = "Please fix the version matching error." | ||
description: str = "There is a version matching error in the file." | ||
|
||
def __str__(self): | ||
return ( | ||
"Version matching error found in the file when parsing in strict mode.\n" | ||
f"Line {self.line_number}: {self.description}\n" | ||
f"Line content: {self.line}\n" | ||
f"How to fix: {self.how_to_fix}" | ||
"\n" | ||
) | ||
|
||
|
||
class VersionMatchRuleBase: | ||
description: str = "" | ||
how_to_fix: str = "" | ||
version: float | ||
line_number: int | ||
line: str | ||
|
||
def __init__(self, version: float, line_number: int, line: str) -> None: | ||
self.version = version | ||
self.line_number = line_number | ||
self.line = line | ||
|
||
def validate(self): | ||
raise NotImplementedError | ||
|
||
def get_error(self): | ||
return VersionMatchingError( | ||
line_number=self.line_number, | ||
line=self.line, | ||
description=self.description, | ||
how_to_fix=self.how_to_fix, | ||
) | ||
|
||
|
||
if "IV" in line: | ||
return version >= 2 | ||
class ValidIVInEXTXKEY(VersionMatchRuleBase): | ||
description = ( | ||
"You must use at least protocol version 2 if you have IV in EXT-X-KEY." | ||
) | ||
how_to_fix = "Change the protocol version to 2 or higher." | ||
|
||
return True | ||
def validate(self): | ||
if not protocol.ext_x_key in self.line: | ||
return True | ||
|
||
if "IV" in self.line: | ||
return self.version >= 2 | ||
|
||
# You must use at least protocol version 3 if you have floating point EXTINF duration values. | ||
def valid_floating_point_EXTINF(line: str, version: float): | ||
if not protocol.extinf in line: | ||
return True | ||
|
||
chunks = line.replace(protocol.extinf + ":", "").split(",", 1) | ||
duration = chunks[0] | ||
|
||
def is_number(value: str): | ||
try: | ||
float(value) | ||
class ValidFloatingPointEXTINF(VersionMatchRuleBase): | ||
description = "You must use at least protocol version 3 if you have floating point EXTINF duration values." | ||
how_to_fix = "Change the protocol version to 3 or higher." | ||
|
||
def validate(self): | ||
if not protocol.extinf in self.line: | ||
return True | ||
except: | ||
return False | ||
|
||
def is_floating_number(value: str): | ||
return is_number(value) and "." in value | ||
chunks = self.line.replace(protocol.extinf + ":", "").split(",", 1) | ||
duration = chunks[0] | ||
|
||
if is_floating_number(duration): | ||
return version >= 3 | ||
def is_number(value: str): | ||
try: | ||
float(value) | ||
return True | ||
except: | ||
return False | ||
|
||
return is_number(duration) | ||
def is_floating_number(value: str): | ||
return is_number(value) and "." in value | ||
|
||
if is_floating_number(duration): | ||
return self.version >= 3 | ||
|
||
return is_number(duration) | ||
|
||
|
||
class ValidEXTXBYTERANGEOrEXTXIFRAMESONLY(VersionMatchRuleBase): | ||
description = "You must use at least protocol version 4 if you have EXT-X-BYTERANGE or EXT-X-IFRAME-ONLY." | ||
how_to_fix = "Change the protocol version to 4 or higher." | ||
|
||
def validate(self): | ||
if ( | ||
not protocol.ext_x_byterange in self.line | ||
and not protocol.ext_i_frames_only in self.line | ||
): | ||
return True | ||
|
||
return self.version >= 4 | ||
|
||
# You must use at least protocol version 4 if you have EXT-X-BYTERANGE or EXT-X-IFRAME-ONLY. | ||
def valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY(line: str, version: float): | ||
if not protocol.ext_x_byterange in line and not protocol.ext_i_frames_only in line: | ||
return True | ||
|
||
return version >= 4 | ||
available_rules: List[type[VersionMatchRuleBase]] = [ | ||
ValidIVInEXTXKEY, | ||
ValidFloatingPointEXTINF, | ||
ValidEXTXBYTERANGEOrEXTXIFRAMESONLY, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.