Decorators.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import functools
  4. import re
  5. from typing import Callable
  6. # An API version must be a semantic version "x.y.z" where ".z" is optional. So the valid formats are as follows:
  7. # - x.y.z
  8. # - x.y
  9. SEMANTIC_VERSION_REGEX = re.compile(r"^[0-9]+\.[0-9]+(\.[0-9]+)?$")
  10. def api(since_version: str) -> Callable:
  11. """Decorator for functions that belong to a set of APIs. For now, this should only be used for officially supported
  12. APIs, meaning that those APIs should be versioned and maintained.
  13. :param since_version: The earliest version since when this API becomes supported. This means that since this version,
  14. this API function is supposed to behave the same. This parameter is not used. It's just a
  15. documentation.
  16. """
  17. # Make sure that APi versions are semantic versions
  18. if not SEMANTIC_VERSION_REGEX.fullmatch(since_version):
  19. raise ValueError("API since_version [%s] is not a semantic version." % since_version)
  20. def api_decorator(function):
  21. @functools.wraps(function)
  22. def api_wrapper(*args, **kwargs):
  23. return function(*args, **kwargs)
  24. return api_wrapper
  25. return api_decorator