Skip to content

Commit 8303d6f

Browse files
authored
Allow disabling auto-reloading when accessing missing attributes (#935)
* Rename _autoReload to _overwriteNone * Allow disabling auto reloading * Update test for disabling auto reloading * Change autoReload to private attribute
1 parent b14a653 commit 8303d6f

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

plexapi/base.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ def __init__(self, server, data, initpath=None, parent=None):
5353
if data is not None:
5454
self._loadData(data)
5555
self._details_key = self._buildDetailsKey()
56-
self._autoReload = False
56+
self._overwriteNone = True
5757
self._edits = None # Save batch edits for a single API call
58+
self._autoReload = True # Automatically reload the object when accessing a missing attribute
5859

5960
def __repr__(self):
6061
uid = self._clean(self.firstAttr('_baseurl', 'key', 'id', 'playQueueID', 'uri'))
@@ -66,9 +67,9 @@ def __setattr__(self, attr, value):
6667
if attr in _DONT_OVERWRITE_SESSION_KEYS and value == []:
6768
value = getattr(self, attr, [])
6869

69-
autoReload = self.__dict__.get('_autoReload')
70-
# Don't overwrite an attr with None unless it's a private variable or not auto reload
71-
if value is not None or attr.startswith('_') or attr not in self.__dict__ or not autoReload:
70+
overwriteNone = self.__dict__.get('_overwriteNone')
71+
# Don't overwrite an attr with None unless it's a private variable or overwrite None is True
72+
if value is not None or attr.startswith('_') or attr not in self.__dict__ or overwriteNone:
7273
self.__dict__[attr] = value
7374

7475
def _clean(self, value):
@@ -346,17 +347,17 @@ def reload(self, key=None, **kwargs):
346347
"""
347348
return self._reload(key=key, **kwargs)
348349

349-
def _reload(self, key=None, _autoReload=False, **kwargs):
350+
def _reload(self, key=None, _overwriteNone=True, **kwargs):
350351
""" Perform the actual reload. """
351352
details_key = self._buildDetailsKey(**kwargs) if kwargs else self._details_key
352353
key = key or details_key or self.key
353354
if not key:
354355
raise Unsupported('Cannot reload an object not built from a URL.')
355356
self._initpath = key
356357
data = self._server.query(key)
357-
self._autoReload = _autoReload
358+
self._overwriteNone = _overwriteNone
358359
self._loadData(data[0])
359-
self._autoReload = False
360+
self._overwriteNone = True
360361
return self
361362

362363
def _checkAttrs(self, elem, **kwargs):
@@ -472,13 +473,14 @@ def __getattribute__(self, attr):
472473
if attr.startswith('_'): return value
473474
if value not in (None, []): return value
474475
if self.isFullObject(): return value
476+
if self._autoReload is False: return value
475477
# Log the reload.
476478
clsname = self.__class__.__name__
477479
title = self.__dict__.get('title', self.__dict__.get('name'))
478480
objname = "%s '%s'" % (clsname, title) if title else clsname
479481
log.debug("Reloading %s for attr '%s'", objname, attr)
480482
# Reload and return the value
481-
self._reload(_autoReload=True)
483+
self._reload()
482484
return super(PlexPartialObject, self).__getattribute__(attr)
483485

484486
def analyze(self):
@@ -791,7 +793,7 @@ def updateProgress(self, time, state='stopped'):
791793
key = '/:/progress?key=%s&identifier=com.plexapp.plugins.library&time=%d&state=%s' % (self.ratingKey,
792794
time, state)
793795
self._server.query(key)
794-
self._reload(_autoReload=True)
796+
self._reload(_overwriteNone=False)
795797

796798
def updateTimeline(self, time, state='stopped', duration=None):
797799
""" Set the timeline progress for this video.
@@ -809,7 +811,7 @@ def updateTimeline(self, time, state='stopped', duration=None):
809811
key = '/:/timeline?ratingKey=%s&key=%s&identifier=com.plexapp.plugins.library&time=%d&state=%s%s'
810812
key %= (self.ratingKey, self.key, time, state, durationStr)
811813
self._server.query(key)
812-
self._reload(_autoReload=True)
814+
self._reload(_overwriteNone=False)
813815

814816

815817
class MediaContainer(PlexObject):

tests/test_video.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ def test_video_Movie_isFullObject_and_reload(plex):
339339

340340
def test_video_Movie_isPartialObject(movie):
341341
assert movie.isPartialObject()
342+
movie._autoReload = False
343+
assert movie.originalTitle is None
344+
assert movie.isPartialObject()
345+
movie._autoReload = True
342346

343347

344348
def test_video_Movie_media_delete(movie, patched_http_call):

0 commit comments

Comments
 (0)