Skip to content

Simplify reload methods #1515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions plexapi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
33 changes: 16 additions & 17 deletions plexapi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -2248,20 +2247,20 @@ 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.
"""
if self._section is None:
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.
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion plexapi/myplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading