
    i"                     f   U d Z 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ZddlmZ eedf   ed   z  ez  ez  ez  ez  dz  Zej*                  ed<   eeef   Zej*                  ed<   ej0                  j3                  ej0                  j5                  e            Z e
d	      Z e
e      ez  Z	 d
ee
z  defdZd
ee
z  defdZ de
dz  fdZ!deeejD                  f   deeejD                  f   fdZ#deeejD                  f   deeejD                  f   fdZ$dedefdZ%ddede&eef   fdZ'y)a  Implementations for loading configurations from YAML files.  This essentially
includes the configuration of the (:ref:`SearXNG appl <searxng settings.yml>`)
server. The default configuration for the application server is loaded from the
:origin:`DEFAULT_SETTINGS_FILE <searx/settings.yml>`.  This default
configuration can be completely replaced or :ref:`customized individually
<use_default_settings.yml>` and the ``SEARXNG_SETTINGS_PATH`` environment
variable can be used to set the location from which the local customizations are
to be loaded. The rules used for this can be found in the
:py:obj:`get_user_cfg_folder` function.

- By default, local configurations are expected in folder ``/etc/searxng`` from
  where applications can load them with the :py:obj:`get_yaml_cfg` function.

- By default, customized :ref:`SearXNG appl <searxng settings.yml>` settings are
  expected in a file named ``settings.yml``.

    N)MutableMapping)filterfalse)Path)SearxSettingsExceptionJSONTypeSettingsTypezsettings.yml	file_namereturnc                 $   	 t        | dd      5 }t        j                  |      xs i cddd       S # 1 sw Y   yxY w# t        $ r}t	        |t        |             |d}~wt        j                  $ r}t	        |t        |             |d}~ww xY w)zLoad YAML config from a file.rzutf-8)encodingN)openyaml	safe_loadIOErrorr   str	YAMLError)r	   settings_yamles      &/root/searxng/searx/settings_loader.py	load_yamlr   (   s}    ?)S73 	7}>>-06B	7 	7 	7 ?$QI7Q>>> ?$QI7Q>?s5   ? 3	? <? ? 	BAB4B

Bc                     t               xs t        t              }|| z  }|j                         st	        d| d      t        |      S )zShortcut to load a YAML config from a file, located in the

    - :py:obj:`get_user_cfg_folder` or
    - in the ``searx`` folder of the SearXNG installation
    zFile z does not exist!)get_user_cfg_folderr   	searx_diris_fileFileNotFoundErrorr   )r	   folderfnames      r   get_yaml_cfgr   3   sH     !"5d9oFYE==?%w.> ?@@U    c                     d} t         j                  j                  d      }t         j                  j                  dd      j                         dv }|rKt	        |      }|j                         r|} n-|j                         r|j                  } nt        d| d|      | s|st	        d      } | j                         sd} | S )	a  Returns folder where the local configurations are located.

    1. If the ``SEARXNG_SETTINGS_PATH`` environment is set and points to a
       folder (e.g. ``/etc/mysxng/``), all local configurations are expected in
       this folder.  The settings of the :ref:`SearXNG appl <searxng
       settings.yml>` then expected in ``settings.yml``
       (e.g. ``/etc/mysxng/settings.yml``).

    2. If the ``SEARXNG_SETTINGS_PATH`` environment is set and points to a file
       (e.g. ``/etc/mysxng/myinstance.yml``), this file contains the settings of
       the :ref:`SearXNG appl <searxng settings.yml>` and the folder
       (e.g. ``/etc/mysxng/``) is used for all other configurations.

       This type (``SEARXNG_SETTINGS_PATH`` points to a file) is suitable for
       use cases in which different profiles of the :ref:`SearXNG appl <searxng
       settings.yml>` are to be managed, such as in test scenarios.

    3. If folder ``/etc/searxng`` exists, it is used.

    In case none of the above path exists, ``None`` is returned.  In case of
    environment ``SEARXNG_SETTINGS_PATH`` is set, but the (folder or file) does
    not exists, a :py:obj:`EnvironmentError` is raised.

    NSEARXNG_SETTINGS_PATHSEARXNG_DISABLE_ETC_SETTINGS )1true   z not exists!z/etc/searxng)	osenvirongetlowerr   is_dirr   parentEnvironmentError)r   settings_pathdisable_etcs      r   r   r   B   s    4 FJJNN#:;M **..!?DJJLP]]K]+!"F""$"))F"1|&DmTT+n%}}FMr    default_dict	user_dictc                     |j                         D ]:  \  }}t        |t              r t        | j	                  |i       |      | |<   6|| |<   < | S )N)items
isinstancer   update_dictr*   )r1   r2   kvs       r   r6   r6   v   sT    !  1a(),*:*:1b*A1ELOLO	 
 r    default_settingsuser_settingsc                    |j                         D ]3  \  }}|dvs|| v r t        |t              rt        | |   |       /|| |<   5 |j	                  d      }|r|| d<   |j	                  d      }||| d<   d d |j	                  d      }t        |t
              rB|j	                  di       j	                  d      |j	                  di       j	                  d      d|v s| d   }t        t        fd|            }t        t        fd	|            }|j	                  d      }|rMt        d
 |D              }	|D ]6  }
|	j	                  |
d         }|rt        ||
       &|j                  |
       8 || d<   | S )N)use_default_settingsenginescategories_as_tabspluginsr<   r=   remove	keep_onlyc                 *    | j                  d      v S Nnamer*   )engineremove_enginess    r   <lambda>z!update_settings.<locals>.<lambda>   s    vzz&7In6\ r    c                 *    | j                  d      v S rC   rE   )rF   keep_only_enginess    r   rH   z!update_settings.<locals>.<lambda>   s    &**V2DIZ1Z r    c              3   *   K   | ]  }|d    |f  yw)rD   N ).0
definitions     r   	<genexpr>z"update_settings.<locals>.<genexpr>   s     [ZF!3Z @[s   rD   )
r4   r5   r   r6   r*   dictlistr   filterappend)r9   r:   r7   r8   r>   r?   r<   r=   user_enginesengines_dictuser_enginedefault_enginerJ   rG   s               @@r   update_settingsrX      s    ##% (177$$A~)F,Q/3&' #( '**+?@1C-.	*G&-# (,N*.4A4E4EF\4]&--11)R@DDXN044YCGGTM!^%?CTC`*:9*E %;'\^efgG (6"Z\cdeG %((3[SZ[[L+ 0:F:J:J;W]K^:_!<NN;/0 '.#r    c                 t    | j                  d      }|du ryt        |t              ry|du s|yt        d      )Nr<   TFz&Invalid value for use_default_settings)r*   r5   rP   
ValueError)r:   r<   s     r   is_use_default_settingsr[      sJ    ,9,=,=>T,Ut#&-u$(<(D
=
>>r    load_user_settingsc                    dt          }t        t               }t               }| r|s||fS t        j                  j                  d      }|r/t        |      j                         rt        |      j                  }nt        }||z  }|j                         s||fS d| }t        |      }t        |      rdt          d| d}t        ||       ||fS |}||fS )znFunction for loading the settings of the SearXNG application
    (:ref:`settings.yml <searxng settings.yml>`).zload the default settings from r"   zload the user settings from zmerge the default settings ( z ) and the user settings ( z ))DEFAULT_SETTINGS_FILEr   r   r(   r)   r*   r   r   rD   SETTINGS_YAMLexistsr[   rX   )r\   msgcfg
cfg_foldersettings_ymlcfg_fileuser_cfgs          r   load_settingsrg      s     ,,A+B
CC
)
*C$&JZCx::>>"9:L\*224L).. %L(H??Cx(

3C"Hx(-.C-DD_`h_iiklX& 8O 8Or    )T)(__doc__typingtos.pathr(   collections.abcr   	itertoolsr   pathlibr   r   searx.exceptionsr   rP   r   rQ   intfloatboolr   	TypeAlias__annotations__r   pathabspathdirname__file__r   r_   r^   r   r   r   Anyr6   rX   r[   tuplerg   rL   r    r   <module>r{      s  $   * !   3S*_-Z0@@3FLuTW[[^bb!++ b h/akk /GGOOBGGOOH56	^$Y-7  J?t ? ?C$J < 1TD[ 1hnS!%%Z8 ^TWYZY^Y^T^E_ 4nS!%%Z&@ 4Q_`cefejej`jQk 4n	?< 	?D 	?!d !eL#<M6N !r    