From 7af546c617d99ed38f9fbe585e0b03c11525de22 Mon Sep 17 00:00:00 2001 From: David Trattnig <david@subsquare.at> Date: Fri, 5 Aug 2022 14:18:42 +0200 Subject: [PATCH] add new private decorator --- src/aura_engine/base/lang.py | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/aura_engine/base/lang.py diff --git a/src/aura_engine/base/lang.py b/src/aura_engine/base/lang.py new file mode 100644 index 00000000..c1814103 --- /dev/null +++ b/src/aura_engine/base/lang.py @@ -0,0 +1,49 @@ +# +# Aura Engine (https://gitlab.servus.at/aura/engine) +# +# Copyright (C) 2017-2020 - The Aura Engine Team. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +""" +A collection of Python language utilities. +""" + +import inspect +from functools import wraps + + +def private(member): + """ + @private Decorator. + + Use this to annotate your methods for private-visibility. + + This is an more expressive alternative to the pythonic underscore visibility. + """ + + @wraps(member) + def wrapper(*args): + me = member.__name__ + stack = inspect.stack() + calling_class = stack[1][0].f_locals["self"].__class__.__name__ + calling_method = stack[1][0].f_code.co_name + if calling_method not in dir(args[0]) and calling_method is not me: + msg = f'"{me}(..)" called by "{calling_class}.{calling_method}(..)" is private' + print(msg) + raise Exception(msg) + return member(*args) + + return wrapper -- GitLab