Simplify building MediaPartStream objects (#1328)

* Simplify MediaPart._buildStreams

* Remove isChildOf check for photo and track attributes

* Add parent object when automatically building PlexObject

* Remove check of track only attribute in tests
This commit is contained in:
JonnyWong16 2024-02-04 10:52:19 -08:00 committed by GitHub
parent b3ef1c22dd
commit e3d90a5945
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 29 deletions

View file

@ -98,7 +98,7 @@ class PlexObject:
ecls = utils.PLEXOBJECTS.get(ehash, utils.PLEXOBJECTS.get(elem.tag))
# log.debug('Building %s as %s', elem.tag, ecls.__name__)
if ecls is not None:
return ecls(self._server, elem, initpath)
return ecls(self._server, elem, initpath, parent=self)
raise UnknownType(f"Unknown library type <{elem.tag} type='{etype}'../>")
def _buildItemOrNone(self, elem, cls=None, initpath=None):

View file

@ -37,7 +37,7 @@ class Media(PlexObject):
videoResolution (str): The video resolution of the media (ex: sd).
width (int): The width of the video in pixels (ex: 608).
<Photo_only_attributes>: The following attributes are only available for photos.
Photo_only_attributes: The following attributes are only available for photos.
* aperture (str): The aperture used to take the photo.
* exposure (str): The exposure used to take the photo.
@ -74,7 +74,7 @@ class Media(PlexObject):
self.width = utils.cast(int, data.attrib.get('width'))
self.uuid = data.attrib.get('uuid')
if self._isChildOf(etag='Photo'):
# Photo only attributes
self.aperture = data.attrib.get('aperture')
self.exposure = data.attrib.get('exposure')
self.iso = utils.cast(int, data.attrib.get('iso'))
@ -158,11 +158,8 @@ class MediaPart(PlexObject):
self.videoProfile = data.attrib.get('videoProfile')
def _buildStreams(self, data):
streams = []
for cls in (VideoStream, AudioStream, SubtitleStream, LyricStream):
items = self.findItems(data, cls, streamType=cls.STREAMTYPE)
streams.extend(items)
return streams
""" Returns a list of :class:`~plexapi.media.MediaPartStream` objects in this MediaPart. """
return self.findItems(data)
@property
def hasPreviewThumbnails(self):
@ -384,7 +381,7 @@ class AudioStream(MediaPartStream):
samplingRate (int): The sampling rate of the audio stream (ex: xxx)
streamIdentifier (int): The stream identifier of the audio stream.
<Track_only_attributes>: The following attributes are only available for tracks.
Track_only_attributes: The following attributes are only available for tracks.
* albumGain (float): The gain for the album.
* albumPeak (float): The peak for the album.
@ -411,7 +408,7 @@ class AudioStream(MediaPartStream):
self.samplingRate = utils.cast(int, data.attrib.get('samplingRate'))
self.streamIdentifier = utils.cast(int, data.attrib.get('streamIdentifier'))
if self._isChildOf(etag='Track'):
# Track only attributes
self.albumGain = utils.cast(float, data.attrib.get('albumGain'))
self.albumPeak = utils.cast(float, data.attrib.get('albumPeak'))
self.albumRange = utils.cast(float, data.attrib.get('albumRange'))

View file

@ -135,8 +135,6 @@ def test_video_Movie_attrs(movies):
assert audio._server._baseurl == utils.SERVER_BASEURL
assert audio.title is None
assert audio.type == 2
with pytest.raises(AttributeError):
assert audio.albumGain is None # Check track only attributes are not available
# Media
media = movie.media[0]
assert media.aspectRatio >= 1.3
@ -160,8 +158,6 @@ def test_video_Movie_attrs(movies):
assert media.videoProfile == "main"
assert media.videoResolution in utils.RESOLUTIONS
assert utils.is_int(media.width, gte=200)
with pytest.raises(AttributeError):
assert media.aperture is None # Check photo only attributes are not available
# Video
video = movie.media[0].parts[0].videoStreams()[0]
assert video.anamorphic is None