| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# This program is free software; you can redistribute it and/or modify |
| 3 |
# it under the terms of the GNU General Public License as published by |
| 4 |
# the Free Software Foundation; either version 2 of the License, or |
| 5 |
# (at your option) any later version. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
# GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License |
| 13 |
# along with this program; if not, write to the Free Software |
| 14 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 15 |
# MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# Author: Mauro Soria |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
from typing import Any, Iterator |
| 22 |
|
| 23 |
|
| 24 |
class CaseInsensitiveDict(dict): |
| 25 |
def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 26 |
super().__init__(*args, **kwargs) |
| 27 |
self._convert_keys() |
| 28 |
|
| 29 |
def __setitem__(self, key: Any, value: Any) -> None: |
| 30 |
if isinstance(key, str): |
| 31 |
key = key.lower() |
| 32 |
|
| 33 |
super().__setitem__(key.lower(), value) |
| 34 |
|
| 35 |
def __getitem__(self, key: Any) -> Any: |
| 36 |
if isinstance(key, str): |
| 37 |
key = key.lower() |
| 38 |
|
| 39 |
return super().__getitem__(key.lower()) |
| 40 |
|
| 41 |
def _convert_keys(self) -> None: |
| 42 |
for key in list(self.keys()): |
| 43 |
value = super().pop(key) |
| 44 |
self.__setitem__(key, value) |
| 45 |
|
| 46 |
|
| 47 |
class OrderedSet: |
| 48 |
def __init__(self, items: list[Any] = []) -> None: |
| 49 |
self._data: dict[Any, Any] = dict() |
| 50 |
|
| 51 |
for item in items: |
| 52 |
self._data[item] = None |
| 53 |
|
| 54 |
def __contains__(self, item: Any) -> bool: |
| 55 |
return item in self._data |
| 56 |
|
| 57 |
def __eq__(self, other: Any) -> bool: |
| 58 |
return self._data.keys() == other._data.keys() |
| 59 |
|
| 60 |
def __iter__(self) -> Iterator[Any]: |
| 61 |
return iter(list(self._data)) |
| 62 |
|
| 63 |
def __len__(self) -> int: |
| 64 |
return len(self._data) |
| 65 |
|
| 66 |
def add(self, item: Any) -> None: |
| 67 |
self._data[item] = None |
| 68 |
|
| 69 |
def clear(self) -> None: |
| 70 |
self._data.clear() |
| 71 |
|
| 72 |
def discard(self, item: Any) -> None: |
| 73 |
self._data.pop(item, None) |
| 74 |
|
| 75 |
def pop(self) -> None: |
| 76 |
self._data.popitem() |
| 77 |
|
| 78 |
def remove(self, item: Any) -> None: |
| 79 |
del self._data[item] |
| 80 |
|
| 81 |
def update(self, items: list[Any]) -> None: |
| 82 |
for item in items: |
| 83 |
self.add(item) |