Skip to content

Commit 6c6388a

Browse files
authored
Simplify reload methods (#1515)
1 parent 6fb09a3 commit 6c6388a

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

plexapi/base.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def listAttrs(self, data, attr, rtag=None, **kwargs):
422422
return results
423423

424424
def reload(self, key=None, **kwargs):
425-
""" Reload the data for this object from self.key.
425+
""" Reload the data for this object.
426426
427427
Parameters:
428428
key (string, optional): Override the key to reload.
@@ -555,6 +555,12 @@ def _loadData(self, data):
555555
""" Load attribute values from Plex XML response. """
556556
raise NotImplementedError('Abstract method not implemented.')
557557

558+
def _findAndLoadElem(self, data, **kwargs):
559+
""" Find and load the first element in the data that matches the specified attributes. """
560+
for elem in data:
561+
if self._checkAttrs(elem, **kwargs):
562+
self._invalidateCacheAndLoadData(elem)
563+
558564
@property
559565
def _searchType(self):
560566
return self.TYPE
@@ -1055,18 +1061,11 @@ def reload(self):
10551061
"""
10561062
return self._reload()
10571063

1058-
def _reload(self, _autoReload=False, **kwargs):
1059-
""" Perform the actual reload. """
1060-
# Do not auto reload sessions
1061-
if _autoReload:
1062-
return self
1063-
1064+
def _reload(self, **kwargs):
1065+
""" Reload the data for the session. """
10641066
key = self._initpath
10651067
data = self._server.query(key)
1066-
for elem in data:
1067-
if elem.attrib.get('sessionKey') == str(self.sessionKey):
1068-
self._invalidateCacheAndLoadData(elem)
1069-
break
1068+
self._findAndLoadElem(data, sessionKey=str(self.sessionKey))
10701069
return self
10711070

10721071
def source(self):

plexapi/library.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,11 @@ def delete(self):
541541
log.error(msg)
542542
raise
543543

544-
def reload(self):
544+
def _reload(self, **kwargs):
545545
""" Reload the data for the library section. """
546-
self._server.library._loadSections()
547-
newLibrary = self._server.library.sectionByID(self.key)
548-
self.__dict__.update(newLibrary.__dict__)
549-
self._invalidateCachedProperties()
546+
key = self._initpath
547+
data = self._server.query(key)
548+
self._findAndLoadElem(data, key=str(self.key))
550549
return self
551550

552551
def edit(self, agent=None, **kwargs):
@@ -2210,10 +2209,10 @@ class Hub(PlexObject):
22102209
context (str): The context of the hub.
22112210
hubKey (str): API URL for these specific hub items.
22122211
hubIdentifier (str): The identifier of the hub.
2213-
items (list): List of items in the hub.
2212+
items (list): List of items in the hub (automatically loads all items if more is True).
22142213
key (str): API URL for the hub.
2215-
more (bool): True if there are more items to load (call reload() to fetch all items).
22162214
random (bool): True if the items in the hub are randomized.
2215+
more (bool): True if there are more items to load (call items to fetch all items).
22172216
size (int): The number of items in the hub.
22182217
style (str): The style of the hub.
22192218
title (str): The title of the hub.
@@ -2248,20 +2247,20 @@ def items(self):
22482247
def __len__(self):
22492248
return self.size
22502249

2251-
def reload(self):
2252-
""" Delete cached data to allow reloading of hub items. """
2253-
self._invalidateCachedProperties()
2254-
if self._data is not None:
2255-
self.more = utils.cast(bool, self._data.attrib.get('more'))
2256-
self.size = utils.cast(int, self._data.attrib.get('size'))
2257-
22582250
def section(self):
22592251
""" Returns the :class:`~plexapi.library.LibrarySection` this hub belongs to.
22602252
"""
22612253
if self._section is None:
22622254
self._section = self._server.library.sectionByID(self.librarySectionID)
22632255
return self._section
22642256

2257+
def _reload(self, **kwargs):
2258+
""" Reload the data for the hub. """
2259+
key = self._initpath
2260+
data = self._server.query(key)
2261+
self._findAndLoadElem(data, hubIdentifier=self.hubIdentifier)
2262+
return self
2263+
22652264

22662265
class LibraryMediaTag(PlexObject):
22672266
""" Base class of library media tags.
@@ -3032,11 +3031,11 @@ def _loadData(self, data):
30323031
parent = self._parent()
30333032
self.librarySectionID = parent.key if isinstance(parent, LibrarySection) else parent.librarySectionID
30343033

3035-
def reload(self):
3034+
def _reload(self, **kwargs):
30363035
""" Reload the data for this managed hub. """
30373036
key = f'/hubs/sections/{self.librarySectionID}/manage'
3038-
hub = self.fetchItem(key, self.__class__, identifier=self.identifier)
3039-
self.__dict__.update(hub.__dict__)
3037+
data = self._server.query(key)
3038+
self._findAndLoadElem(data, identifier=self.identifier)
30403039
return self
30413040

30423041
def move(self, after=None):

plexapi/myplex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def authenticationToken(self):
219219
""" Returns the authentication token for the account. Alias for ``authToken``. """
220220
return self.authToken
221221

222-
def _reload(self, key=None, **kwargs):
222+
def _reload(self, **kwargs):
223223
""" Perform the actual reload. """
224224
data = self.query(self.key)
225225
self._invalidateCacheAndLoadData(data)

0 commit comments

Comments
 (0)