From 02a64630aa83e37e47a28a60366e1c65f0eba3ac Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Thu, 8 Apr 2021 16:20:14 +0200 Subject: [PATCH] Added configtype to wrap valid prefixes. --- qutebrowser/config/configtypes.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 4439cd4f4..55676207f 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -142,6 +142,27 @@ class ValidValues: self.descriptions == other.descriptions) +class ValidPrefixes(ValidValues): + + def __init__(self, *values, separator: str = ':', **kwargs): + super().__init__(*values, **kwargs) + self.separator = separator + + def __contains__(self, item) -> bool: + return any(map(lambda x: item.startswith(x + self.separator), self.values)) + + def __iter__(self) -> Iterator[str]: + return map(lambda x: x + self.separator, super().__iter__()) + + def __repr__(self) -> str: + return utils.get_repr(self, values=self.values, separator=self.separator, + descriptions=self.descriptions) + + def __eq__(self, other: object) -> bool: + assert (isinstance(other, ValidPrefixes)) + return super().__eq__(other) and self.separator == other.separator + + class BaseType: """A type used for a setting value.