From b2afaf305d0b623f33bd36fc62801cfff9aafe1a Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Wed, 23 Oct 2019 08:32:58 +0200 Subject: [PATCH 1/4] Add @api decorator CURA-6665 --- cura/Utils/Decorators.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cura/Utils/Decorators.py diff --git a/cura/Utils/Decorators.py b/cura/Utils/Decorators.py new file mode 100644 index 0000000000..0d7065f2f9 --- /dev/null +++ b/cura/Utils/Decorators.py @@ -0,0 +1,19 @@ +# Copyright (c) 2019 Ultimaker B.V. +# Cura is released under the terms of the LGPLv3 or higher. + +import functools + + +## Decorator for functions that belong to a set of APIs. For now, this should only be used for officially supported +# APIs, meaning that those APIs should be versioned and maintained. +# +# \param since The earliest version since when this API becomes supported. This means that since this version, this +# API function is supposed to behave the same. This parameter is not used. It's just a documentation. +# +def api(since = "Unknown"): + def api_decorator(function): + @functools.wraps(function) + def api_wrapper(*args, **kwargs): + return function(*args, **kwargs) + return api_wrapper + return api_decorator From 33cf7491ef94c289595978c9916e4aa11cda9caa Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Wed, 23 Oct 2019 09:58:03 +0200 Subject: [PATCH 2/4] dos2unix Decorator.py CURA-6665 --- cura/Utils/Decorators.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/cura/Utils/Decorators.py b/cura/Utils/Decorators.py index 0d7065f2f9..3f652234d5 100644 --- a/cura/Utils/Decorators.py +++ b/cura/Utils/Decorators.py @@ -1,19 +1,19 @@ -# Copyright (c) 2019 Ultimaker B.V. -# Cura is released under the terms of the LGPLv3 or higher. - -import functools - - -## Decorator for functions that belong to a set of APIs. For now, this should only be used for officially supported -# APIs, meaning that those APIs should be versioned and maintained. -# -# \param since The earliest version since when this API becomes supported. This means that since this version, this -# API function is supposed to behave the same. This parameter is not used. It's just a documentation. -# -def api(since = "Unknown"): - def api_decorator(function): - @functools.wraps(function) - def api_wrapper(*args, **kwargs): - return function(*args, **kwargs) - return api_wrapper - return api_decorator +# Copyright (c) 2019 Ultimaker B.V. +# Cura is released under the terms of the LGPLv3 or higher. + +import functools + + +## Decorator for functions that belong to a set of APIs. For now, this should only be used for officially supported +# APIs, meaning that those APIs should be versioned and maintained. +# +# \param since The earliest version since when this API becomes supported. This means that since this version, this +# API function is supposed to behave the same. This parameter is not used. It's just a documentation. +# +def api(since = "Unknown"): + def api_decorator(function): + @functools.wraps(function) + def api_wrapper(*args, **kwargs): + return function(*args, **kwargs) + return api_wrapper + return api_decorator From 08b1e0e9a87be0b9c8e359a39bc37b1d61c3904c Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Wed, 23 Oct 2019 10:34:47 +0200 Subject: [PATCH 3/4] @api checked for semantic versions CURA-6665 --- cura/Utils/Decorators.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cura/Utils/Decorators.py b/cura/Utils/Decorators.py index 3f652234d5..52f61eb1f5 100644 --- a/cura/Utils/Decorators.py +++ b/cura/Utils/Decorators.py @@ -2,15 +2,25 @@ # Cura is released under the terms of the LGPLv3 or higher. import functools +import re + +# An API version must be a semantic version "x.y.z" where ".z" is optional. So the valid formats are as follows: +# - x.y.z +# - x.y +SEMANTIC_VERSION_REGEX = re.compile(r"^[0-9]+\.[0-9]+(\.[0-9]+)?$") ## Decorator for functions that belong to a set of APIs. For now, this should only be used for officially supported # APIs, meaning that those APIs should be versioned and maintained. # -# \param since The earliest version since when this API becomes supported. This means that since this version, this -# API function is supposed to behave the same. This parameter is not used. It's just a documentation. -# -def api(since = "Unknown"): +# \param since_version The earliest version since when this API becomes supported. This means that since this version, +# this API function is supposed to behave the same. This parameter is not used. It's just a +# documentation. +def api(since_version: str) -> callable: + # Make sure that APi versions are semantic versions + if not SEMANTIC_VERSION_REGEX.fullmatch(since_version): + raise ValueError("API since_version [%s] is not a semantic version." % since_version) + def api_decorator(function): @functools.wraps(function) def api_wrapper(*args, **kwargs): From 5436e68ea4ed86b142272c2aa71c2fb43828d6c9 Mon Sep 17 00:00:00 2001 From: Nino van Hooff Date: Wed, 23 Oct 2019 15:29:54 +0200 Subject: [PATCH 4/4] Fix mypy warning CURA-6665 --- cura/Utils/Decorators.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cura/Utils/Decorators.py b/cura/Utils/Decorators.py index 52f61eb1f5..9275ee6ce9 100644 --- a/cura/Utils/Decorators.py +++ b/cura/Utils/Decorators.py @@ -3,6 +3,7 @@ import functools import re +from typing import Callable # An API version must be a semantic version "x.y.z" where ".z" is optional. So the valid formats are as follows: # - x.y.z @@ -16,7 +17,7 @@ SEMANTIC_VERSION_REGEX = re.compile(r"^[0-9]+\.[0-9]+(\.[0-9]+)?$") # \param since_version The earliest version since when this API becomes supported. This means that since this version, # this API function is supposed to behave the same. This parameter is not used. It's just a # documentation. -def api(since_version: str) -> callable: +def api(since_version: str) -> Callable: # Make sure that APi versions are semantic versions if not SEMANTIC_VERSION_REGEX.fullmatch(since_version): raise ValueError("API since_version [%s] is not a semantic version." % since_version)