Add helper object to retrieve PlexObject (#1369)

This commit is contained in:
JonnyWong16 2024-03-16 15:03:44 -07:00 committed by GitHub
parent 7802b79d30
commit 0601aaa40e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View file

@ -95,7 +95,7 @@ class PlexObject:
ehash = f"{ehash}.session"
elif initpath.startswith('/status/sessions/history'):
ehash = f"{ehash}.history"
ecls = utils.PLEXOBJECTS.get(ehash, utils.PLEXOBJECTS.get(elem.tag))
ecls = utils.getPlexObject(ehash, default=elem.tag)
# log.debug('Building %s as %s', elem.tag, ecls.__name__)
if ecls is not None:
return ecls(self._server, elem, initpath, parent=self)

View file

@ -136,6 +136,19 @@ def registerPlexObject(cls):
return cls
def getPlexObject(ehash, default):
""" Return the PlexObject class for the specified ehash. This recursively looks up the class
with the highest specificity, falling back to the default class if not found.
"""
cls = PLEXOBJECTS.get(ehash)
if cls is not None:
return cls
if '.' in ehash:
ehash = ehash.rsplit('.', 1)[0]
return getPlexObject(ehash, default=default)
return PLEXOBJECTS.get(default)
def cast(func, value):
""" Cast the specified value to the specified type (returned by func). Currently this
only support str, int, float, bool. Should be extended if needed.