Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List typing #389

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions m3u8/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# license that can be found in the LICENSE file.
import decimal
import os
from typing import TypeVar

from m3u8.mixins import BasePathMixin, GroupedBasePathMixin
from m3u8.parser import format_date_time, parse
Expand Down Expand Up @@ -449,6 +450,15 @@ def _create_sub_directories(self, filename):
os.makedirs(basename, exist_ok=True)


T = TypeVar("T")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



class TagList(list[T]):
def __str__(self):
output = [str(tag) for tag in self]
return "\n".join(output)


class Segment(BasePathMixin):
"""
A video segment from a M3U8 playlist
Expand Down Expand Up @@ -706,7 +716,7 @@ def base_uri(self, newbase_uri):
self.init_section.base_uri = newbase_uri


class SegmentList(list, GroupedBasePathMixin):
class SegmentList(list[Segment], GroupedBasePathMixin):
def dumps(self, timespec="milliseconds", infspec="auto"):
output = []
last_segment = None
Expand Down Expand Up @@ -821,7 +831,7 @@ def __str__(self):
return self.dumps(None)


class PartialSegmentList(list, GroupedBasePathMixin):
class PartialSegmentList(list[PartialSegment], GroupedBasePathMixin):
def __str__(self):
output = [str(part) for part in self]
return "\n".join(output)
Expand Down Expand Up @@ -1009,6 +1019,10 @@ def __str__(self):
return "#EXT-X-STREAM-INF:" + ",".join(stream_inf) + "\n" + self.uri


class PlaylistList(TagList[Playlist], GroupedBasePathMixin):
pass


class IFramePlaylist(BasePathMixin):
"""
IFramePlaylist object representing a link to a
Expand Down Expand Up @@ -1260,26 +1274,12 @@ def __str__(self):
return self.dumps()


class TagList(list):
def __str__(self):
output = [str(tag) for tag in self]
return "\n".join(output)


class MediaList(TagList, GroupedBasePathMixin):
class MediaList(TagList[Media], GroupedBasePathMixin):
@property
def uri(self):
return [media.uri for media in self]


class PlaylistList(TagList, GroupedBasePathMixin):
pass


class SessionDataList(TagList):
pass


class Start:
def __init__(self, time_offset, precise=None):
self.time_offset = float(time_offset)
Expand Down Expand Up @@ -1314,7 +1314,7 @@ def __str__(self):
return self.dumps()


class RenditionReportList(list, GroupedBasePathMixin):
class RenditionReportList(list[RenditionReport], GroupedBasePathMixin):
def __str__(self):
output = [str(report) for report in self]
return "\n".join(output)
Expand Down Expand Up @@ -1443,7 +1443,7 @@ def __str__(self):
return self.dumps()


class DateRangeList(TagList):
class SessionDataList(TagList[SessionData]):
pass


Expand Down Expand Up @@ -1502,6 +1502,10 @@ def __str__(self):
return self.dumps()


class DateRangeList(TagList[DateRange]):
pass


class ContentSteering(BasePathMixin):
def __init__(self, base_uri, server_uri, pathway_id=None):
self.base_uri = base_uri
Expand Down