
    3 da@                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dddd	d
Z
d Z G d d          Z G d de          Z G d de          Zd Zd ZdS )    N)MutableMapping)import_module)pformat)default_settings
         (   )defaultcommandprojectspidercmdlinec                 J    t          | t                    rt          |          S | S )z
    Small helper function that looks up a given string priority in the
    :attr:`~scrapy.settings.SETTINGS_PRIORITIES` dictionary and returns its
    numerical value, or directly returns a given numerical priority.
    )
isinstancestrSETTINGS_PRIORITIESprioritys    8lib/python3.11/site-packages/scrapy/settings/__init__.pyget_settings_priorityr      s&     (C   -"8,,O    c                   $    e Zd ZdZd Zd Zd ZdS )SettingsAttributezClass for storing data related to settings attributes.

    This class is intended for internal usage, you should try Settings class
    for settings configuration, not this one.
    c                     || _         t          | j         t                    r.t          | j                                         |          | _        d S || _        d S N)valuer   BaseSettingsmaxmaxpriorityr   selfr   r   s      r   __init__zSettingsAttribute.__init__%   sK    
dj,// 	%
 6 6 8 8(CCDMMM$DMMMr   c                     || j         k    r;t          | j        t                    rt          ||          }|| _        || _         dS dS )z@Sets value if priority is higher or equal than current priority.r   N)r   r   r   r   r!   s      r   setzSettingsAttribute.set,   sS    t}$ 	%$*l33 ?$UX>>>DJ$DMMM		% 	%r   c                 (    d| j         d| j         dS )Nz<SettingsAttribute value=z
 priority=>)r   r   r"   s    r   __repr__zSettingsAttribute.__repr__4   s    S4:SS4=SSSSr   N)__name__
__module____qualname____doc__r#   r%   r)    r   r   r   r      sP         % % %% % %T T T T Tr   r   c                       e Zd ZdZd%dZd Zd Zd&dZd'd	Zd(dZ	d)dZ
d&dZd&dZd&dZd Zd Zd Zd Zd*dZd*dZd*dZd*dZd*dZd Zd Zd Zd Zd Zd Zd  Zd! Zd" Zd# Z d$ Z!dS )+r   a&  
    Instances of this class behave like dictionaries, but store priorities
    along with their ``(key, value)`` pairs, and can be frozen (i.e. marked
    immutable).

    Key-value entries can be passed on initialization with the ``values``
    argument, and they would take the ``priority`` level (unless ``values`` is
    already an instance of :class:`~scrapy.settings.BaseSettings`, in which
    case the existing priority levels will be kept).  If the ``priority``
    argument is a string, the priority name will be looked up in
    :attr:`~scrapy.settings.SETTINGS_PRIORITIES`. Otherwise, a specific integer
    should be provided.

    Once the object is created, new settings can be loaded or updated with the
    :meth:`~scrapy.settings.BaseSettings.set` method, and can be accessed with
    the square bracket notation of dictionaries, or with the
    :meth:`~scrapy.settings.BaseSettings.get` method of the instance and its
    value conversion variants. When requesting a stored key, the value with the
    highest priority will be retrieved.
    Nr   c                 V    d| _         i | _        |r|                     ||           d S d S )NF)frozen
attributesupdater"   valuesr   s      r   r#   zBaseSettings.__init__N   s<     	*KK)))))	* 	*r   c                 2    || vrd S | j         |         j        S r   )r2   r   )r"   opt_names     r   __getitem__zBaseSettings.__getitem__T   s$    4 	4x(..r   c                     || j         v S r   )r2   r"   names     r   __contains__zBaseSettings.__contains__Y   s    t&&r   c                 &    | |         | |         n|S )z
        Get a setting value without affecting its original type.

        :param name: the setting name
        :type name: str

        :param default: the value to return if no setting is found
        :type default: object
        r.   r"   r;   r   s      r   getzBaseSettings.get\   s     "$Z@tDzz@r   Fc                     |                      ||          }	 t          t          |                    S # t          $ r |dv rY dS |dv rY dS t          d          w xY w)a  
        Get a setting value as a boolean.

        ``1``, ``'1'``, `True`` and ``'True'`` return ``True``,
        while ``0``, ``'0'``, ``False``, ``'False'`` and ``None`` return ``False``.

        For example, settings populated through environment variables set to
        ``'0'`` will return ``False`` when using this method.

        :param name: the setting name
        :type name: str

        :param default: the value to return if no setting is found
        :type default: object
        )TruetrueT)FalsefalseFzeSupported values for boolean settings are 0/1, True/False, '0'/'1', 'True'/'False' and 'true'/'false')r?   boolint
ValueError)r"   r;   r   gots       r   getboolzBaseSettings.getboolh   s      hhtW%%	C>>! 		 		 		&& tt(( uu4  		s   4 AAAr   c                 H    t          |                     ||                    S )z
        Get a setting value as an int.

        :param name: the setting name
        :type name: str

        :param default: the value to return if no setting is found
        :type default: object
        )rF   r?   r>   s      r   getintzBaseSettings.getint   s      488D'**+++r           c                 H    t          |                     ||                    S )z
        Get a setting value as a float.

        :param name: the setting name
        :type name: str

        :param default: the value to return if no setting is found
        :type default: object
        )floatr?   r>   s      r   getfloatzBaseSettings.getfloat   s      TXXdG,,---r   c                     |                      ||pg           }t          |t                    r|                    d          }t	          |          S )a  
        Get a setting value as a list. If the setting original type is a list, a
        copy of it will be returned. If it's a string it will be split by ",".

        For example, settings populated through environment variables set to
        ``'one,two'`` will return a list ['one', 'two'] when using this method.

        :param name: the setting name
        :type name: str

        :param default: the value to return if no setting is found
        :type default: object
        ,)r?   r   r   splitlistr"   r;   r   r   s       r   getlistzBaseSettings.getlist   sJ     w}"--eS!! 	%KK$$EE{{r   c                     |                      ||pi           }t          |t                    rt          j        |          }t          |          S )a  
        Get a setting value as a dictionary. If the setting original type is a
        dictionary, a copy of it will be returned. If it is a string it will be
        evaluated as a JSON dictionary. In the case that it is a
        :class:`~scrapy.settings.BaseSettings` instance itself, it will be
        converted to a dictionary, containing all its current settings values
        as they would be returned by :meth:`~scrapy.settings.BaseSettings.get`,
        and losing all information about priority and mutability.

        :param name: the setting name
        :type name: str

        :param default: the value to return if no setting is found
        :type default: object
        )r?   r   r   jsonloadsdictrT   s       r   getdictzBaseSettings.getdict   sH      w}"--eS!! 	&Ju%%EE{{r   c                     |                      ||          }|i S t          |t                    r:	 t          j        |          S # t
          $ r |                    d          cY S w xY wt          j        |          S )a  Get a setting value as either a :class:`dict` or a :class:`list`.

        If the setting is already a dict or a list, a copy of it will be
        returned.

        If it is a string it will be evaluated as JSON, or as a comma-separated
        list of strings as a fallback.

        For example, settings populated from the command line will return:

        -   ``{'key1': 'value1', 'key2': 'value2'}`` if set to
            ``'{"key1": "value1", "key2": "value2"}'``

        -   ``['one', 'two']`` if set to ``'["one", "two"]'`` or ``'one,two'``

        :param name: the setting name
        :type name: string

        :param default: the value to return if no setting is found
        :type default: any
        NrQ   )	r?   r   r   rW   rX   rG   rR   copydeepcopyrT   s       r   getdictorlistzBaseSettings.getdictorlist   s    , w'' 	IeS!! 	((z%((( ( ( ({{3'''''(}U###s   A A'&A'c                     t                      }|                    | |dz                       |                    | |                    |S )zGet a composition of a dictionary-like setting and its `_BASE`
        counterpart.

        :param name: name of the dictionary-like setting
        :type name: str
        _BASE)r   r3   )r"   r;   compbss      r   getwithbasezBaseSettings.getwithbase   sC     d4'>*+++d4j!!!r   c                 2    || vrdS | j         |         j        S )z
        Return the current numerical priority value of a setting, or ``None`` if
        the given ``name`` does not exist.

        :param name: the setting name
        :type name: str
        N)r2   r   r:   s     r   getpriorityzBaseSettings.getpriority   s&     t 	4t$--r   c                 ~     t                     dk    rt           fd D                       S t          d          S )z
        Return the numerical value of the highest priority present throughout
        all settings, or the numerical value for ``default`` from
        :attr:`~scrapy.settings.SETTINGS_PRIORITIES` if there are no settings
        stored.
        r   c              3   B   K   | ]}                     |          V  d S r   )rd   ).0r;   r"   s     r   	<genexpr>z+BaseSettings.maxpriority.<locals>.<genexpr>  s1      ??$t''--??????r   r   )lenr   r   r(   s   `r   r    zBaseSettings.maxpriority   sI     t99q= 	@????$??????$Y///r   c                 2    |                      ||           d S r   )r%   )r"   r;   r   s      r   __setitem__zBaseSettings.__setitem__	  s    ur   c                    |                                   t          |          }|| vr;t          |t                    r|| j        |<   dS t          ||          | j        |<   dS | j        |                             ||           dS )aV  
        Store a key/value attribute with a given priority.

        Settings should be populated *before* configuring the Crawler object
        (through the :meth:`~scrapy.crawler.Crawler.configure` method),
        otherwise they won't have any effect.

        :param name: the setting name
        :type name: str

        :param value: the value to associate with the setting
        :type value: object

        :param priority: the priority of the setting. Should be a key of
            :attr:`~scrapy.settings.SETTINGS_PRIORITIES` or an integer
        :type priority: str or int
        N)_assert_mutabilityr   r   r   r2   r%   )r"   r;   r   r   s       r   r%   zBaseSettings.set  s    $ 	!!!(22t 	7%!233 K(-%%%(9%(J(J%%%OD!%%eX66666r   c                 2    |                      ||           d S r   )r3   r4   s      r   setdictzBaseSettings.setdict(  s    FH%%%%%r   c                    |                                   t          |t                    rt          |          }t	          |          D ];}|                                r%|                     |t          ||          |           <dS )a*  
        Store settings from a module with a given priority.

        This is a helper function that calls
        :meth:`~scrapy.settings.BaseSettings.set` for every globally declared
        uppercase variable of ``module`` with the provided ``priority``.

        :param module: the module or the path of the module
        :type module: types.ModuleType or str

        :param priority: the priority of the settings. Should be a key of
            :attr:`~scrapy.settings.SETTINGS_PRIORITIES` or an integer
        :type priority: str or int
        N)rm   r   r   r   dirisupperr%   getattr)r"   moduler   keys       r   	setmodulezBaseSettings.setmodule+  s     	!!!fc"" 	+"6**Fv;; 	> 	>C{{}} >gfc22H===	> 	>r   c                    |                                   t          |t                    rt          j        |          }|t          |t
                    rF|                                D ]/\  }}|                     |||                    |                     0dS |                                D ]\  }}|                     |||           dS dS )a  
        Store key/value pairs with a given priority.

        This is a helper function that calls
        :meth:`~scrapy.settings.BaseSettings.set` for every item of ``values``
        with the provided ``priority``.

        If ``values`` is a string, it is assumed to be JSON-encoded and parsed
        into a dict with ``json.loads()`` first. If it is a
        :class:`~scrapy.settings.BaseSettings` instance, the per-key priorities
        will be used and the ``priority`` parameter ignored. This allows
        inserting/updating settings with different priorities with a single
        command.

        :param values: the settings names and values
        :type values: dict or string or :class:`~scrapy.settings.BaseSettings`

        :param priority: the priority of the settings. Should be a key of
            :attr:`~scrapy.settings.SETTINGS_PRIORITIES` or an integer
        :type priority: str or int
        N)	rm   r   r   rW   rX   r   itemsr%   rd   )r"   r5   r   r;   r   s        r   r3   zBaseSettings.updateA  s    , 	!!!fc"" 	(Z''F 	4&,// 4#)<<>> D DKD%HHT5&*<*<T*B*BCCCCD D $*<<>> 4 4KD%HHT5(3333	4 	4
4 4r   c                     |                                   t          |          }||                     |          k    r
| j        |= d S d S r   )rm   r   rd   r2   )r"   r;   r   s      r   deletezBaseSettings.deleteb  sV    !!!(22t''--- 	&%%%	& 	&r   c                 >    |                                   | j        |= d S r   )rm   r2   r:   s     r   __delitem__zBaseSettings.__delitem__h  s$    !!!OD!!!r   c                 2    | j         rt          d          d S )Nz-Trying to modify an immutable Settings object)r1   	TypeErrorr(   s    r   rm   zBaseSettings._assert_mutabilityl  s(    ; 	MKLLL	M 	Mr   c                 *    t          j        |           S )a  
        Make a deep copy of current settings.

        This method returns a new instance of the :class:`Settings` class,
        populated with the same values and their priorities.

        Modifications to the new object won't be reflected on the original
        settings.
        )r\   r]   r(   s    r   r\   zBaseSettings.copyp  s     }T"""r   c                     d| _         dS )a  
        Disable further changes to the current settings.

        After calling this method, the present state of the settings will become
        immutable. Trying to change values through the :meth:`~set` method and
        its variants won't be possible and will be alerted.
        TN)r1   r(   s    r   freezezBaseSettings.freeze|  s     r   c                 L    |                                  } |j                     |S )z
        Return an immutable copy of the current settings.

        Alias for a :meth:`~freeze` call in the object returned by :meth:`copy`.
        )r\   r   )r"   r\   s     r   
frozencopyzBaseSettings.frozencopy  s#     yy{{r   c                 *    t          | j                  S r   )iterr2   r(   s    r   __iter__zBaseSettings.__iter__  s    DO$$$r   c                 *    t          | j                  S r   )ri   r2   r(   s    r   __len__zBaseSettings.__len__  s    4?###r   c                 D      fd                                  D             S )Nc                     i | ]D\  }}                     |          t          |t                    r|                                n|ES r.   )_get_keyr   r   _to_dict)rg   kvr"   s      r   
<dictcomp>z)BaseSettings._to_dict.<locals>.<dictcomp>  sW     
 
 
1 MM!z!\/J/JQqzz|||PQ
 
 
r   )rx   r(   s   `r   r   zBaseSettings._to_dict  s6    
 
 
 



 
 
 	
r   c           
          t          |t          t          t          t          t          d           f          r|nt	          |          S r   )r   rE   rN   rF   r   type)r"   	key_values     r   r   zBaseSettings._get_key  s9     )dE3T$ZZ%HII IIY	
r   c                 R    |                                  }|                                S )a  
        Make a copy of current settings and convert to a dict.

        This method returns a new dict populated with the same values
        and their priorities as the current settings.

        Modifications to the returned dict won't be reflected on the original
        settings.

        This method can be useful for example for printing settings
        in Scrapy shell.
        )r\   r   )r"   settingss     r   copy_to_dictzBaseSettings.copy_to_dict  s#     99;;  """r   c                     |r$|                     t          |                      d S |                     t          |                                                      d S r   )textreprr   r   )r"   pcycles      r   _repr_pretty_zBaseSettings._repr_pretty_  sT     	1FF4::FF74,,..//00000r   Nr   r   )F)r   )rL   )r   )"r*   r+   r,   r-   r#   r8   r<   r?   rI   rK   rO   rU   rZ   r^   rb   rd   r    rk   r%   ro   rv   r3   rz   r|   rm   r\   r   r   r   r   r   r   r   r   r.   r   r   r   r   8   s*        ** * * */ / /
' ' '
A 
A 
A 
A   <
, 
, 
, 
,
. 
. 
. 
.   &   *$ $ $ $@
 
 

. 
. 
.	0 	0 	0  7 7 7 78& & & &> > > >,4 4 4 4B& & & &" " "M M M
# 
# 
#    % % %$ $ $
 
 

 
 
# # # 1 1 1 1 1r   r   c                   $     e Zd ZdZd fd	Z xZS )Settingsa  
    This object stores Scrapy settings for the configuration of internal
    components, and can be used for any further customization.

    It is a direct subclass and supports all methods of
    :class:`~scrapy.settings.BaseSettings`. Additionally, after instantiation
    of this class, the new object will have the global default settings
    described on :ref:`topics-settings-ref` already populated.
    Nr   c                 R   t                                                       |                     t          d           |                                 D ]?\  }}t          |t                    r%|                     |t          |d          d           @| 	                    ||           d S )Nr   )
superr#   rv   r   rx   r   rY   r%   r   r3   )r"   r5   r   r;   val	__class__s        r   r#   zSettings.__init__  s     	'333  	H 	HID##t$$ H|C;;YGGGFH%%%%%r   r   )r*   r+   r,   r-   r#   __classcell__)r   s   @r   r   r     sG         & & & & & & & & & &r   r   c               #      K   t          t                    D ]/} |                                 r| t          t          |           fV  0dS )zBReturn the default settings as an iterator of (name, value) tuplesN)rq   r   rr   rs   )r;   s    r   iter_default_settingsr     sU      $%% 8 8<<>> 	8 0$7777778 8r   c              #      K   t                      D ].\  }}| |         }t          |t                    s||k    r||fV  /dS )z7Return a dict of the settings that have been overriddenN)r   r   rY   )r   r;   defvaluer   s       r   overridden_settingsr     sa      /11  h(D)) 	ex.? 	+ r   )r\   rW   collections.abcr   	importlibr   pprintr   scrapy.settingsr   r   r   r   r   r   r   r   r.   r   r   <module>r      sA     * * * * * * # # # # # #       , , , , , ,     T T T T T T T T61 1 1 1 1> 1 1 1D& & & & &| & & &28 8 8    r   