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
| 81 | |
| 82 | |
| 83 | class BaseSettings(MutableMapping[_SettingsKey, Any]): |
| 84 | """ |
| 85 | Instances of this class behave like dictionaries, but store priorities |
| 86 | along with their ``(key, value)`` pairs, and can be frozen (i.e. marked |
| 87 | immutable). |
| 88 | |
| 89 | Key-value entries can be passed on initialization with the ``values`` |
| 90 | argument, and they would take the ``priority`` level (unless ``values`` is |
| 91 | already an instance of :class:`~scrapy.settings.BaseSettings`, in which |
| 92 | case the existing priority levels will be kept). If the ``priority`` |
| 93 | argument is a string, the priority name will be looked up in |
| 94 | :attr:`~scrapy.settings.SETTINGS_PRIORITIES`. Otherwise, a specific integer |
| 95 | should be provided. |
| 96 | |
| 97 | Once the object is created, new settings can be loaded or updated with the |
| 98 | :meth:`~scrapy.settings.BaseSettings.set` method, and can be accessed with |
| 99 | the square bracket notation of dictionaries, or with the |
| 100 | :meth:`~scrapy.settings.BaseSettings.get` method of the instance and its |
| 101 | value conversion variants. When requesting a stored key, the value with the |
| 102 | highest priority will be retrieved. |
| 103 | """ |
| 104 | |
| 105 | __default = object() |
| 106 | |
| 107 | def __init__(self, values: _SettingsInput = None, priority: int | str = "project"): |
| 108 | self.frozen: bool = False |
| 109 | self.attributes: dict[_SettingsKey, SettingsAttribute] = {} |
| 110 | if values: |
| 111 | self.update(values, priority) |
| 112 | |
| 113 | def __getitem__(self, opt_name: _SettingsKey) -> Any: |
| 114 | if opt_name not in self: |
| 115 | return None |
| 116 | return self.attributes[opt_name].value |
| 117 | |
| 118 | def __contains__(self, name: Any) -> bool: |
| 119 | return name in self.attributes |
| 120 | |
| 121 | def add_to_list(self, name: _SettingsKey, item: Any) -> None: |
| 122 | """Append *item* to the :class:`list` setting with the specified *name* |
| 123 | if *item* is not already in that list. |
| 124 | |
| 125 | This change is applied regardless of the priority of the *name* |
| 126 | setting. The setting priority is not affected by this change either. |
| 127 | """ |
| 128 | value: list[str] = self.getlist(name) |
| 129 | if item not in value: |
| 130 | self.set(name, [*value, item], self.getpriority(name) or 0) |
| 131 | |
| 132 | def remove_from_list(self, name: _SettingsKey, item: Any) -> None: |
| 133 | """Remove *item* from the :class:`list` setting with the specified |
| 134 | *name*. |
| 135 | |
| 136 | If *item* is missing, raise :exc:`ValueError`. |
| 137 | |
| 138 | This change is applied regardless of the priority of the *name* |
| 139 | setting. The setting priority is not affected by this change either. |
| 140 | """ |
no outgoing calls