From da91209fa97176442c77766d296587100738f4f9 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Wed, 9 Apr 2025 10:22:02 -0700 Subject: [PATCH] Simplify reload methods --- plexapi/base.py | 21 ++++++++++----------- plexapi/library.py | 33 ++++++++++++++++----------------- plexapi/myplex.py | 2 +- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/plexapi/base.py b/plexapi/base.py index df92fb2d7..f0229ac85 100644 --- a/plexapi/base.py +++ b/plexapi/base.py @@ -422,7 +422,7 @@ def listAttrs(self, data, attr, rtag=None, **kwargs): return results def reload(self, key=None, **kwargs): - """ Reload the data for this object from self.key. + """ Reload the data for this object. Parameters: key (string, optional): Override the key to reload. @@ -555,6 +555,12 @@ def _loadData(self, data): """ Load attribute values from Plex XML response. """ raise NotImplementedError('Abstract method not implemented.') + def _findAndLoadElem(self, data, **kwargs): + """ Find and load the first element in the data that matches the specified attributes. """ + for elem in data: + if self._checkAttrs(elem, **kwargs): + self._invalidateCacheAndLoadData(elem) + @property def _searchType(self): return self.TYPE @@ -1055,18 +1061,11 @@ def reload(self): """ return self._reload() - def _reload(self, _autoReload=False, **kwargs): - """ Perform the actual reload. """ - # Do not auto reload sessions - if _autoReload: - return self - + def _reload(self, **kwargs): + """ Reload the data for the session. """ key = self._initpath data = self._server.query(key) - for elem in data: - if elem.attrib.get('sessionKey') == str(self.sessionKey): - self._invalidateCacheAndLoadData(elem) - break + self._findAndLoadElem(data, sessionKey=str(self.sessionKey)) return self def source(self): diff --git a/plexapi/library.py b/plexapi/library.py index 271ad74f2..6ad35684b 100644 --- a/plexapi/library.py +++ b/plexapi/library.py @@ -541,12 +541,11 @@ def delete(self): log.error(msg) raise - def reload(self): + def _reload(self, **kwargs): """ Reload the data for the library section. """ - self._server.library._loadSections() - newLibrary = self._server.library.sectionByID(self.key) - self.__dict__.update(newLibrary.__dict__) - self._invalidateCachedProperties() + key = self._initpath + data = self._server.query(key) + self._findAndLoadElem(data, key=str(self.key)) return self def edit(self, agent=None, **kwargs): @@ -2210,10 +2209,10 @@ class Hub(PlexObject): context (str): The context of the hub. hubKey (str): API URL for these specific hub items. hubIdentifier (str): The identifier of the hub. - items (list): List of items in the hub. + items (list): List of items in the hub (automatically loads all items if more is True). key (str): API URL for the hub. - more (bool): True if there are more items to load (call reload() to fetch all items). random (bool): True if the items in the hub are randomized. + more (bool): True if there are more items to load (call items to fetch all items). size (int): The number of items in the hub. style (str): The style of the hub. title (str): The title of the hub. @@ -2248,13 +2247,6 @@ def items(self): def __len__(self): return self.size - def reload(self): - """ Delete cached data to allow reloading of hub items. """ - self._invalidateCachedProperties() - if self._data is not None: - self.more = utils.cast(bool, self._data.attrib.get('more')) - self.size = utils.cast(int, self._data.attrib.get('size')) - def section(self): """ Returns the :class:`~plexapi.library.LibrarySection` this hub belongs to. """ @@ -2262,6 +2254,13 @@ def section(self): self._section = self._server.library.sectionByID(self.librarySectionID) return self._section + def _reload(self, **kwargs): + """ Reload the data for the hub. """ + key = self._initpath + data = self._server.query(key) + self._findAndLoadElem(data, hubIdentifier=self.hubIdentifier) + return self + class LibraryMediaTag(PlexObject): """ Base class of library media tags. @@ -3032,11 +3031,11 @@ def _loadData(self, data): parent = self._parent() self.librarySectionID = parent.key if isinstance(parent, LibrarySection) else parent.librarySectionID - def reload(self): + def _reload(self, **kwargs): """ Reload the data for this managed hub. """ key = f'/hubs/sections/{self.librarySectionID}/manage' - hub = self.fetchItem(key, self.__class__, identifier=self.identifier) - self.__dict__.update(hub.__dict__) + data = self._server.query(key) + self._findAndLoadElem(data, identifier=self.identifier) return self def move(self, after=None): diff --git a/plexapi/myplex.py b/plexapi/myplex.py index 680132bba..411d56183 100644 --- a/plexapi/myplex.py +++ b/plexapi/myplex.py @@ -219,7 +219,7 @@ def authenticationToken(self): """ Returns the authentication token for the account. Alias for ``authToken``. """ return self.authToken - def _reload(self, key=None, **kwargs): + def _reload(self, **kwargs): """ Perform the actual reload. """ data = self.query(self.key) self._invalidateCacheAndLoadData(data)