Skip to content

Commit e3d90a5

Browse files
authored
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
1 parent b3ef1c2 commit e3d90a5

3 files changed

Lines changed: 22 additions & 29 deletions

File tree

plexapi/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _buildItem(self, elem, cls=None, initpath=None):
9898
ecls = utils.PLEXOBJECTS.get(ehash, utils.PLEXOBJECTS.get(elem.tag))
9999
# log.debug('Building %s as %s', elem.tag, ecls.__name__)
100100
if ecls is not None:
101-
return ecls(self._server, elem, initpath)
101+
return ecls(self._server, elem, initpath, parent=self)
102102
raise UnknownType(f"Unknown library type <{elem.tag} type='{etype}'../>")
103103

104104
def _buildItemOrNone(self, elem, cls=None, initpath=None):

plexapi/media.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Media(PlexObject):
3737
videoResolution (str): The video resolution of the media (ex: sd).
3838
width (int): The width of the video in pixels (ex: 608).
3939
40-
<Photo_only_attributes>: The following attributes are only available for photos.
40+
Photo_only_attributes: The following attributes are only available for photos.
4141
4242
* aperture (str): The aperture used to take the photo.
4343
* exposure (str): The exposure used to take the photo.
@@ -74,13 +74,13 @@ def _loadData(self, data):
7474
self.width = utils.cast(int, data.attrib.get('width'))
7575
self.uuid = data.attrib.get('uuid')
7676

77-
if self._isChildOf(etag='Photo'):
78-
self.aperture = data.attrib.get('aperture')
79-
self.exposure = data.attrib.get('exposure')
80-
self.iso = utils.cast(int, data.attrib.get('iso'))
81-
self.lens = data.attrib.get('lens')
82-
self.make = data.attrib.get('make')
83-
self.model = data.attrib.get('model')
77+
# Photo only attributes
78+
self.aperture = data.attrib.get('aperture')
79+
self.exposure = data.attrib.get('exposure')
80+
self.iso = utils.cast(int, data.attrib.get('iso'))
81+
self.lens = data.attrib.get('lens')
82+
self.make = data.attrib.get('make')
83+
self.model = data.attrib.get('model')
8484

8585
parent = self._parent()
8686
self._parentKey = parent.key
@@ -158,11 +158,8 @@ def _loadData(self, data):
158158
self.videoProfile = data.attrib.get('videoProfile')
159159

160160
def _buildStreams(self, data):
161-
streams = []
162-
for cls in (VideoStream, AudioStream, SubtitleStream, LyricStream):
163-
items = self.findItems(data, cls, streamType=cls.STREAMTYPE)
164-
streams.extend(items)
165-
return streams
161+
""" Returns a list of :class:`~plexapi.media.MediaPartStream` objects in this MediaPart. """
162+
return self.findItems(data)
166163

167164
@property
168165
def hasPreviewThumbnails(self):
@@ -384,7 +381,7 @@ class AudioStream(MediaPartStream):
384381
samplingRate (int): The sampling rate of the audio stream (ex: xxx)
385382
streamIdentifier (int): The stream identifier of the audio stream.
386383
387-
<Track_only_attributes>: The following attributes are only available for tracks.
384+
Track_only_attributes: The following attributes are only available for tracks.
388385
389386
* albumGain (float): The gain for the album.
390387
* albumPeak (float): The peak for the album.
@@ -411,16 +408,16 @@ def _loadData(self, data):
411408
self.samplingRate = utils.cast(int, data.attrib.get('samplingRate'))
412409
self.streamIdentifier = utils.cast(int, data.attrib.get('streamIdentifier'))
413410

414-
if self._isChildOf(etag='Track'):
415-
self.albumGain = utils.cast(float, data.attrib.get('albumGain'))
416-
self.albumPeak = utils.cast(float, data.attrib.get('albumPeak'))
417-
self.albumRange = utils.cast(float, data.attrib.get('albumRange'))
418-
self.endRamp = data.attrib.get('endRamp')
419-
self.gain = utils.cast(float, data.attrib.get('gain'))
420-
self.loudness = utils.cast(float, data.attrib.get('loudness'))
421-
self.lra = utils.cast(float, data.attrib.get('lra'))
422-
self.peak = utils.cast(float, data.attrib.get('peak'))
423-
self.startRamp = data.attrib.get('startRamp')
411+
# Track only attributes
412+
self.albumGain = utils.cast(float, data.attrib.get('albumGain'))
413+
self.albumPeak = utils.cast(float, data.attrib.get('albumPeak'))
414+
self.albumRange = utils.cast(float, data.attrib.get('albumRange'))
415+
self.endRamp = data.attrib.get('endRamp')
416+
self.gain = utils.cast(float, data.attrib.get('gain'))
417+
self.loudness = utils.cast(float, data.attrib.get('loudness'))
418+
self.lra = utils.cast(float, data.attrib.get('lra'))
419+
self.peak = utils.cast(float, data.attrib.get('peak'))
420+
self.startRamp = data.attrib.get('startRamp')
424421

425422
def setSelected(self):
426423
""" Sets this audio stream as the selected audio stream.

tests/test_video.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ def test_video_Movie_attrs(movies):
135135
assert audio._server._baseurl == utils.SERVER_BASEURL
136136
assert audio.title is None
137137
assert audio.type == 2
138-
with pytest.raises(AttributeError):
139-
assert audio.albumGain is None # Check track only attributes are not available
140138
# Media
141139
media = movie.media[0]
142140
assert media.aspectRatio >= 1.3
@@ -160,8 +158,6 @@ def test_video_Movie_attrs(movies):
160158
assert media.videoProfile == "main"
161159
assert media.videoResolution in utils.RESOLUTIONS
162160
assert utils.is_int(media.width, gte=200)
163-
with pytest.raises(AttributeError):
164-
assert media.aperture is None # Check photo only attributes are not available
165161
# Video
166162
video = movie.media[0].parts[0].videoStreams()[0]
167163
assert video.anamorphic is None

0 commit comments

Comments
 (0)