1"""The module contains miscellaneous helpers. 2It's not considered part of the public ufoLib API. 3""" 4import warnings 5import functools 6 7 8numberTypes = (int, float) 9 10 11def deprecated(msg=""): 12 """Decorator factory to mark functions as deprecated with given message. 13 14 >>> @deprecated("Enough!") 15 ... def some_function(): 16 ... "I just print 'hello world'." 17 ... print("hello world") 18 >>> some_function() 19 hello world 20 >>> some_function.__doc__ == "I just print 'hello world'." 21 True 22 """ 23 24 def deprecated_decorator(func): 25 @functools.wraps(func) 26 def wrapper(*args, **kwargs): 27 warnings.warn( 28 f"{func.__name__} function is a deprecated. {msg}", 29 category=DeprecationWarning, 30 stacklevel=2, 31 ) 32 return func(*args, **kwargs) 33 34 return wrapper 35 36 return deprecated_decorator 37 38 39# To be mixed with enum.Enum in UFOFormatVersion and GLIFFormatVersion 40class _VersionTupleEnumMixin: 41 @property 42 def major(self): 43 return self.value[0] 44 45 @property 46 def minor(self): 47 return self.value[1] 48 49 @classmethod 50 def _missing_(cls, value): 51 # allow to initialize a version enum from a single (major) integer 52 if isinstance(value, int): 53 return cls((value, 0)) 54 # or from None to obtain the current default version 55 if value is None: 56 return cls.default() 57 return super()._missing_(value) 58 59 def __str__(self): 60 return f"{self.major}.{self.minor}" 61 62 @classmethod 63 def default(cls): 64 # get the latest defined version (i.e. the max of all versions) 65 return max(cls.__members__.values()) 66 67 @classmethod 68 def supported_versions(cls): 69 return frozenset(cls.__members__.values()) 70 71 72if __name__ == "__main__": 73 import doctest 74 75 doctest.testmod() 76