Skip to content

Commit b943bc6

Browse files
authored
Merge pull request #765 from JonnyWong16/feature/auto_reload
Allow overwriting attributes with None when manually reloading
2 parents 7da32a7 + 8ef3d0a commit b943bc6

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

plexapi/base.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
from plexapi import log, utils
88
from plexapi.exceptions import BadRequest, NotFound, UnknownType, Unsupported
99

10-
DONT_RELOAD_FOR_KEYS = {'key', 'session'}
11-
DONT_OVERWRITE_SESSION_KEYS = {'usernames', 'players', 'transcodeSessions', 'session'}
10+
USER_DONT_RELOAD_FOR_KEYS = set()
11+
_DONT_RELOAD_FOR_KEYS = {'key', 'session'}
12+
_DONT_OVERWRITE_SESSION_KEYS = {'usernames', 'players', 'transcodeSessions', 'session'}
1213
OPERATORS = {
1314
'exact': lambda v, q: v == q,
1415
'iexact': lambda v, q: v.lower() == q.lower(),
@@ -52,6 +53,7 @@ def __init__(self, server, data, initpath=None, parent=None):
5253
if data is not None:
5354
self._loadData(data)
5455
self._details_key = self._buildDetailsKey()
56+
self._autoReload = False
5557

5658
def __repr__(self):
5759
uid = self._clean(self.firstAttr('_baseurl', 'key', 'id', 'playQueueID', 'uri'))
@@ -60,10 +62,12 @@ def __repr__(self):
6062

6163
def __setattr__(self, attr, value):
6264
# Don't overwrite session specific attr with []
63-
if attr in DONT_OVERWRITE_SESSION_KEYS and value == []:
65+
if attr in _DONT_OVERWRITE_SESSION_KEYS and value == []:
6466
value = getattr(self, attr, [])
65-
# Don't overwrite an attr with None unless it's a private variable
66-
if value is not None or attr.startswith('_') or attr not in self.__dict__:
67+
68+
autoReload = self.__dict__.get('_autoReload')
69+
# Don't overwrite an attr with None unless it's a private variable or not auto reload
70+
if value is not None or attr.startswith('_') or attr not in self.__dict__ or not autoReload:
6771
self.__dict__[attr] = value
6872

6973
def _clean(self, value):
@@ -331,13 +335,19 @@ def reload(self, key=None, **kwargs):
331335
movie.isFullObject() # Returns True
332336
333337
"""
338+
return self._reload(key=key, **kwargs)
339+
340+
def _reload(self, key=None, _autoReload=False, **kwargs):
341+
""" Perform the actual reload. """
334342
details_key = self._buildDetailsKey(**kwargs) if kwargs else self._details_key
335343
key = key or details_key or self.key
336344
if not key:
337345
raise Unsupported('Cannot reload an object not built from a URL.')
338346
self._initpath = key
339347
data = self._server.query(key)
348+
self._autoReload = _autoReload
340349
self._loadData(data[0])
350+
self._autoReload = False
341351
return self
342352

343353
def _checkAttrs(self, elem, **kwargs):
@@ -443,8 +453,9 @@ def __getattribute__(self, attr):
443453
# Dragons inside.. :-/
444454
value = super(PlexPartialObject, self).__getattribute__(attr)
445455
# Check a few cases where we dont want to reload
446-
if attr in DONT_RELOAD_FOR_KEYS: return value
447-
if attr in DONT_OVERWRITE_SESSION_KEYS: return value
456+
if attr in _DONT_RELOAD_FOR_KEYS: return value
457+
if attr in _DONT_OVERWRITE_SESSION_KEYS: return value
458+
if attr in USER_DONT_RELOAD_FOR_KEYS: return
448459
if attr.startswith('_'): return value
449460
if value not in (None, []): return value
450461
if self.isFullObject(): return value
@@ -454,7 +465,7 @@ def __getattribute__(self, attr):
454465
objname = "%s '%s'" % (clsname, title) if title else clsname
455466
log.debug("Reloading %s for attr '%s'", objname, attr)
456467
# Reload and return the value
457-
self.reload()
468+
self._reload(_autoReload=True)
458469
return super(PlexPartialObject, self).__getattribute__(attr)
459470

460471
def analyze(self):
@@ -696,7 +707,7 @@ def updateProgress(self, time, state='stopped'):
696707
key = '/:/progress?key=%s&identifier=com.plexapp.plugins.library&time=%d&state=%s' % (self.ratingKey,
697708
time, state)
698709
self._server.query(key)
699-
self.reload()
710+
self._reload(_autoReload=True)
700711

701712
def updateTimeline(self, time, state='stopped', duration=None):
702713
""" Set the timeline progress for this video.
@@ -714,7 +725,7 @@ def updateTimeline(self, time, state='stopped', duration=None):
714725
key = '/:/timeline?ratingKey=%s&key=%s&identifier=com.plexapp.plugins.library&time=%d&state=%s%s'
715726
key %= (self.ratingKey, self.key, time, state, durationStr)
716727
self._server.query(key)
717-
self.reload()
728+
self._reload(_autoReload=True)
718729

719730

720731
class MediaContainer(PlexObject):

tests/test_collection.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ def test_Collection_edit(collection, movies):
135135
"summary.locked": 0
136136
}
137137
)
138-
# Cannot use collection.reload() since PlexObject.__setattr__()
139-
# will not overwrite contentRating with None
140-
collection = movies.collection("Test Collection")
138+
collection.reload()
141139
assert collection.title == title
142140
assert collection.titleSort == titleSort
143141
assert collection.contentRating is None

tests/test_mixins.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ def edit_rating(obj):
185185
assert utils.is_datetime(obj.lastRatedAt)
186186
assert obj.userRating == 10.0
187187
obj.rate()
188-
# Cannot use obj.reload() since PlexObject.__setattr__()
189-
# will not overwrite userRating with None
190-
obj = obj.fetchItem(obj._details_key)
188+
obj.reload()
191189
assert obj.userRating is None
192190
with pytest.raises(BadRequest):
193191
assert obj.rate('bad-rating')

0 commit comments

Comments
 (0)