77from plexapi import log , utils
88from 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' }
1213OPERATORS = {
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
720731class MediaContainer (PlexObject ):
0 commit comments