Skip to content

Commit

Permalink
Format code using black
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricioabreu committed May 11, 2023
1 parent 57d2547 commit a4d6f22
Show file tree
Hide file tree
Showing 14 changed files with 2,274 additions and 1,655 deletions.
84 changes: 65 additions & 19 deletions m3u8/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,87 @@
from urllib.parse import urljoin, urlsplit

from m3u8.httpclient import DefaultHTTPClient
from m3u8.model import (M3U8, Segment, SegmentList, PartialSegment,
PartialSegmentList, Key, Playlist, IFramePlaylist,
Media, MediaList, PlaylistList, Start,
RenditionReport, RenditionReportList, ServerControl,
Skip, PartInformation, PreloadHint, DateRange,
DateRangeList, ContentSteering)
from m3u8.model import (
M3U8,
Segment,
SegmentList,
PartialSegment,
PartialSegmentList,
Key,
Playlist,
IFramePlaylist,
Media,
MediaList,
PlaylistList,
Start,
RenditionReport,
RenditionReportList,
ServerControl,
Skip,
PartInformation,
PreloadHint,
DateRange,
DateRangeList,
ContentSteering,
)
from m3u8.parser import parse, ParseError


__all__ = ('M3U8', 'Segment', 'SegmentList', 'PartialSegment',
'PartialSegmentList', 'Key', 'Playlist', 'IFramePlaylist',
'Media', 'MediaList', 'PlaylistList', 'Start', 'RenditionReport',
'RenditionReportList', 'ServerControl', 'Skip', 'PartInformation',
'PreloadHint', 'DateRange', 'DateRangeList', 'ContentSteering',
'loads', 'load', 'parse', 'ParseError')
__all__ = (
"M3U8",
"Segment",
"SegmentList",
"PartialSegment",
"PartialSegmentList",
"Key",
"Playlist",
"IFramePlaylist",
"Media",
"MediaList",
"PlaylistList",
"Start",
"RenditionReport",
"RenditionReportList",
"ServerControl",
"Skip",
"PartInformation",
"PreloadHint",
"DateRange",
"DateRangeList",
"ContentSteering",
"loads",
"load",
"parse",
"ParseError",
)


def loads(content, uri=None, custom_tags_parser=None):
'''
"""
Given a string with a m3u8 content, returns a M3U8 object.
Optionally parses a uri to set a correct base_uri on the M3U8 object.
Raises ValueError if invalid content
'''
"""

if uri is None:
return M3U8(content, custom_tags_parser=custom_tags_parser)
else:
base_uri = urljoin(uri, '.')
base_uri = urljoin(uri, ".")
return M3U8(content, base_uri=base_uri, custom_tags_parser=custom_tags_parser)


def load(uri, timeout=None, headers={}, custom_tags_parser=None, http_client=DefaultHTTPClient(), verify_ssl=True):
'''
def load(
uri,
timeout=None,
headers={},
custom_tags_parser=None,
http_client=DefaultHTTPClient(),
verify_ssl=True,
):
"""
Retrieves the content from a given URI and returns a M3U8 object.
Raises ValueError if invalid content or IOError if request fails.
'''
"""
if urlsplit(uri).scheme:
content, base_uri = http_client.download(uri, timeout, headers, verify_ssl)
return M3U8(content, base_uri=base_uri, custom_tags_parser=custom_tags_parser)
Expand All @@ -52,7 +98,7 @@ def load(uri, timeout=None, headers={}, custom_tags_parser=None, http_client=Def


def _load_from_file(uri, custom_tags_parser=None):
with open(uri, encoding='utf8') as fileobj:
with open(uri, encoding="utf8") as fileobj:
raw_content = fileobj.read().strip()
base_uri = os.path.dirname(uri)
return M3U8(raw_content, base_uri=base_uri, custom_tags_parser=custom_tags_parser)
4 changes: 1 addition & 3 deletions m3u8/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class DefaultHTTPClient:

def __init__(self, proxies=None):
self.proxies = proxies

Expand All @@ -15,15 +14,14 @@ def download(self, uri, timeout=None, headers={}, verify_ssl=True):
opener = urllib.request.build_opener(proxy_handler, https_handler)
opener.addheaders = headers.items()
resource = opener.open(uri, timeout=timeout)
base_uri = urljoin(resource.geturl(), '.')
base_uri = urljoin(resource.geturl(), ".")
content = resource.read().decode(
resource.headers.get_content_charset(failobj="utf-8")
)
return content, base_uri


class HTTPSHandler:

def __new__(self, verify_ssl=True):
context = ssl.create_default_context()
if not verify_ssl:
Expand Down
6 changes: 2 additions & 4 deletions m3u8/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class BasePathMixin(object):

@property
def absolute_uri(self):
if self.uri is None:
Expand All @@ -14,8 +13,8 @@ def absolute_uri(self):
return ret

if not urlsplit(ret).scheme:
raise ValueError('There can not be `absolute_uri` with no `base_uri` set')
raise ValueError("There can not be `absolute_uri` with no `base_uri` set")

return ret

@property
Expand All @@ -38,7 +37,6 @@ def base_path(self, newbase_path):


class GroupedBasePathMixin(object):

def _set_base_uri(self, new_base_uri):
for item in self:
item.base_uri = new_base_uri
Expand Down
Loading

0 comments on commit a4d6f22

Please sign in to comment.