From f120be17e4b1d63bd98c2390cf4ec39b8e61e8fd Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Tue, 5 May 2020 18:15:51 +0100 Subject: [PATCH] ufoLib/utils: define _VersionTupleEnumMixin class --- Lib/fontTools/ufoLib/utils.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Lib/fontTools/ufoLib/utils.py b/Lib/fontTools/ufoLib/utils.py index effc71fdf..85878b47a 100644 --- a/Lib/fontTools/ufoLib/utils.py +++ b/Lib/fontTools/ufoLib/utils.py @@ -36,6 +36,39 @@ def deprecated(msg=""): return deprecated_decorator +# To be mixed with enum.Enum in UFOFormatVersion and GLIFFormatVersion +class _VersionTupleEnumMixin: + @property + def major(self): + return self.value[0] + + @property + def minor(self): + return self.value[1] + + @classmethod + def _missing_(cls, value): + # allow to initialize a version enum from a single (major) integer + if isinstance(value, int): + return cls((value, 0)) + # or from None to obtain the current default version + if value is None: + return cls.default() + return super()._missing_(value) + + def __str__(self): + return f"{self.major}.{self.minor}" + + @classmethod + def default(cls): + # get the latest defined version (i.e. the max of all versions) + return max(cls.__members__.values()) + + @classmethod + def supported_versions(cls): + return frozenset(cls.__members__.values()) + + if __name__ == "__main__": import doctest