
    g                         d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZ  G d de      Z G d d	e      Zej                   G d
 de             Zy)    )defaultdict)quote)logutils)
PlexObject)
BadRequestNotFoundc                   X     e Zd ZdZdZd fd	Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Z xZS )Settingsz Container class for all settings. Allows getting and setting PlexServer settings.

        Attributes:
            key (str): '/:/prefs'
    z/:/prefsc                 >    i | _         t        t        |   |||       y N)	_settingssuperr   __init__)selfserverdatainitpath	__class__s       %/opt/Tautulli/lib/plexapi/settings.pyr   zSettings.__init__   s    h&vtX>    c                     |j                  d      r	 | j                  |   S | j	                  |      j
                  S # t        $ r t        w xY wN_)
startswith__dict__KeyErrorAttributeErrorgetvalue)r   attrs     r   __getattr__zSettings.__getattr__   sO    ??3%}}T** xx~###  %$$%s	   = Ac                     |j                  d      s | j                  |      j                  |      S || j                  |<   y r   )r   r   setr   )r   r!   r    s      r   __setattr__zSettings.__setattr__   s5    s#88D>%%e,,#dr   c                    || _         |D ]  }t        j                  |j                  d         }|| j                  v r| j                  |   j                  |       Rt        | j                  || j                        | j                  |<    y)/ Load attribute values from Plex XML response. idN)	_datar   
lowerFirstattribr   	_loadDataSetting_server	_initpath)r   r   elemr(   s       r   r,   zSettings._loadData#   sx    
 	MD!!$++d"34BT^^#r",,T2!(tT^^!LDNN2	Mr   c                 z    t        | j                  j                               D cg c]  \  }}|	 c}}S c c}}w )zM Returns a list of all :class:`~plexapi.settings.Setting` objects available. )sortedr   items)r   r(   vs      r   allzSettings.all-   s,    %dnn&:&:&<=>eb!>>>s   7c                     t        j                  |      }|| j                  v r| j                  |   S t        d|       )zM Return the :class:`~plexapi.settings.Setting` object with the specified id. zInvalid setting id: )r   r*   r   r	   )r   r(   s     r   r   zSettings.get1   s@    b!>>"%%-bT233r   c                     t        t              }| j                         D ]   }||j                     j	                  |       " t        |      S )zz Returns a dict of lists for all :class:`~plexapi.settings.Setting`
            objects grouped by setting group.
        )r   listr5   groupappenddict)r   groupssettings      r   r<   zSettings.groups8   sD     T"xxz 	2G7==!((1	2F|r   c                 B    | j                         j                  |g       S )z Return a list of all :class:`~plexapi.settings.Setting` objects in the specified group.

            Parameters:
                group (str): Group to return all settings.
        )r<   r   )r   r9   s     r   r9   zSettings.groupA   s     {{}  ++r   c                    i }| j                         D ]\  }|j                  st        j                  d|j                  |j                         t        |j                        ||j                  <   ^ |st        d      dj                  d |j                         D              }| j                   d| }| j                  j                  || j                  j                  j                         | j                          y)z Save any outstanding setting changes to the :class:`~plexapi.server.PlexServer`. This
            performs a full reload() of Settings after complete.
        z!Saving PlexServer setting %s = %szNo setting have been modified.&c              3   0   K   | ]  \  }}| d |   yw)=N ).0kr4   s      r   	<genexpr>z Settings.save.<locals>.<genexpr>T   s     B41aqc1#JBs   ?N)r5   	_setValuer   infor(   r   r   joinr3   keyr.   query_sessionputreload)r   paramsr=   querystrurls        r   savezSettings.saveI   s     xxz 	>G  <gjj'J[J[\%*7+<+<%=wzz"	> =>>88B6<<>BB
!H:&3 5 5 9 9:r   r   )__name__
__module____qualname____doc__rK   r   r"   r%   r,   r5   r   r<   r9   rS   __classcell__)r   s   @r   r   r   
   s=    
 C?$$
M?4,r   r   c                   h    e Zd ZdZd Zd Zeeedeeede	e	edeeeddZ
d Zd Zd Zd	 Zd
 Zy)r-   a   Represents a single Plex setting.

        Attributes:
            id (str): Setting id (or name).
            label (str): Short description of what this setting is.
            summary (str): Long description of what this setting is.
            type (str): Setting type (text, int, double, bool).
            default (str): Default value for this setting.
            value (str,bool,int,float): Current value for this setting.
            hidden (bool): True if this is a hidden setting.
            advanced (bool): True if this is an advanced setting.
            group (str): Group name this setting is categorized as.
            enumValues (list,dict): List or dictionary of valid values for this setting.
    c                 ,    t        | dk(  xs | dk(        S )Ntrue1)boolxs    r   <lambda>zSetting.<lambda>i   s    4V 7qCx8 r   c                 4    t        |       j                         S r   )strlowerr^   s    r   r`   zSetting.<lambda>j   s    #a&,,. r   )typecasttostr)r]   doubleinttextc                    |j                   j                  d      | _        t        j                  t
        |j                   j                  d            | _        | j                  |j                   j                  d            | _        | j                  |      | _
        |j                   j                  d      | _        t        j                  t
        |j                   j                  d            | _        |j                   j                  d      | _        |j                   j                  d      | _        |j                   j                  d      | _        t        j                  t
        |j                   j                  d	            | _        |j                   j                  d
      | _        | j                  |j                   j                  d            | _        d| _        y)r'   rd   advanceddefaultr9   hiddenr(   labeloptionsecuresummaryr    N)r+   r   rd   r   re   r]   rk   _castrl   _getEnumValues
enumValuesr9   rm   r(   rn   ro   rp   rq   r    rH   )r   r   s     r   r,   zSetting._loadDatar   s*   KKOOF+	

4)DEzz$++//)"<=--d3[[__W-
jjt{{x'@A++//$'[[__W-
kkooh/jjt{{x'@A{{y1ZZ 89
r   c                     | j                   dk7  r<t        j                  | j                  j	                  | j                         d   |      }|S )z6 Cast the specific value to the type of this setting. enumre   )rd   r   re   TYPESr   )r   r    s     r   rr   zSetting._cast   s9    99JJtzz~~dii8@%HEr   c                 n   |j                   j                  d      xs |j                   j                  d      }|syd|v rCi }|j                  d      D ]+  }	 |j                  d      \  }}||| j                  |      <   - |S |j                  d      S # t        $ r ||| j                  |      <   Y `w xY w)z: Returns a list or dictionary of values for this setting. rt   valuesN:|)r+   r   splitrr   
ValueError)r   r   enumstrdkvrE   r4   s          r   rs   zSetting._getEnumValues   s    ++//,/L4;;??83L'>AmmC( ++88C=DAq'(Adjjm$+ H}}S!! " +(*Adjjn%+s   (BB43B4c           
         t        || j                  | j                     d         s=t        |      j                  }t	        d| j
                   d| j                   d|       | j                  r?|| j                  vr1t	        d| j
                   d| dt        | j                               | j                  | j                     d   } ||      | _        y)	z Set a new value for this setting. NOTE: You must call plex.settings.save() for before
            any changes to setting values are persisted to the :class:`~plexapi.server.PlexServer`.
        rd   zInvalid value for z: a z is required, not z: z not in rf   N)	
isinstancerw   rd   rT   r   r(   rt   r8   rH   )r   r    badtyperf   s       r   r$   zSetting.set   s    
 %DII!6v!>?5k**G1$''$tyykI[\c[deff??uDOO;1$''"UG8DQUQ`Q`LaKbcdd

499%g.ur   c                 T    | j                    d| j                  xs | j                   S )zHelper for urlsrB   )r(   _valuer    )r   s    r   toUrlzSetting.toUrl   s%    ''!DKK54::677r   N)rT   rU   rV   rW   
_bool_cast	_bool_strr]   floatrb   rh   rw   r,   rr   rs   r$   r   rC   r   r   r-   r-   Z   s[     9J(IzIF %#>S37cC8	E " &8r   r-   c                       e Zd ZdZdZdZd Zy)Preferencesz Represents a single Preferences.

        Attributes:
            TAG (str): 'Setting'
            FILTER (str): 'preferences'
    r-   preferencesc                     | j                    d}|| j                   d| j                   z   }| j                  j	                  || j                  j
                  j                         y)z( Set the default value for this setting.z/prefs?rB   )methodN)r/   r(   rl   r.   rL   rM   rN   )r   rK   rR   s      r   _defaultzPreferences._default   sW     (twwiq//3t||'<'<'@'@Ar   N)rT   rU   rV   rW   TAGFILTERr   rC   r   r   r   r      s     CFBr   r   N)collectionsr   urllib.parser   plexapir   r   plexapi.baser   plexapi.exceptionsr   r	   r   r-   registerPlexObjectr   rC   r   r   <module>r      sV    #   # 3Mz M`N8j N8b B' B Br   