From 9fc6dad669f1517839f28227b1d20c75571acf19 Mon Sep 17 00:00:00 2001 From: Sven Eberth Date: Tue, 28 Jan 2025 15:56:29 +0100 Subject: [PATCH 1/2] fix: Remove overwriting `action` from `@deprecated` decorator (#1389) The deprecations warnings on third party packages (which are still in compatibility mode with 3.6) are getting annoying. Therefore I have set up the following warning filter in my project: ```py warnings.filterwarnings("ignore", category=DeprecationWarning, module=r"viur\.(shop|toolkit).*", message="'clonedBoneMap' was renamed into 'bone_map'") warnings.filterwarnings("ignore", category=DeprecationWarning, module=r"viur\.(shop|toolkit).*", message=r"Call to deprecated class method (subSkel|fromDB|toDB)") ``` Unfortunately it does not work, because the deprecations in the `skeleton` module are explicitly called with an `action `parameter. This in turn means that these warnings are executed in a separate context with exactly this action, all other filter conditions are overwritten or ignored. I therefore suggest setting the general filter in the `__init__` to `"once"`. The module filter was wrong at this point anyway. The `module` refers to the module that calls the deprecated method, not the one that triggers the warning (my fault). This general filter can easily be customized from a relaxed `"ignore"` to an annoying `"always"` in the project. Alternatively, the `viur-core` could also use its own `DeprecationWarning` subclass so that these warnings could be filtered even more finely using the class filter. --- src/viur/core/__init__.py | 5 +++-- src/viur/core/skeleton.py | 3 --- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/viur/core/__init__.py b/src/viur/core/__init__.py index 48a95569d..af99a0877 100644 --- a/src/viur/core/__init__.py +++ b/src/viur/core/__init__.py @@ -70,8 +70,9 @@ ] # Show DeprecationWarning from the viur-core -warnings.filterwarnings("always", category=DeprecationWarning, module=r"viur\.core.*") - +warnings.filterwarnings("once", category=DeprecationWarning) +warnings.filterwarnings("ignore", category=DeprecationWarning, module=r"viur\.datastore.*", + message="'clonedBoneMap' was renamed into 'bone_map'") def setDefaultLanguage(lang: str): """ diff --git a/src/viur/core/skeleton.py b/src/viur/core/skeleton.py index dc04fa32a..0adfb036a 100644 --- a/src/viur/core/skeleton.py +++ b/src/viur/core/skeleton.py @@ -465,7 +465,6 @@ class BaseSkeleton(object, metaclass=MetaBaseSkel): @deprecated( version="3.7.0", reason="Function renamed. Use subskel function as alternative implementation.", - action="always" ) def subSkel(cls, *subskel_names, fullClone: bool = False, **kwargs) -> SkeletonInstance: return cls.subskel(*subskel_names, clone=fullClone) # FIXME: REMOVE WITH VIUR4 @@ -1099,7 +1098,6 @@ def fromClient( @deprecated( version="3.7.0", reason="Use skel.read() instead of skel.fromDB()", - action="once" ) def fromDB(cls, skel: SkeletonInstance, key: KeyType) -> bool: """ @@ -1166,7 +1164,6 @@ def read( @deprecated( version="3.7.0", reason="Use skel.write() instead of skel.toDB()", - action="once" ) def toDB(cls, skel: SkeletonInstance, update_relations: bool = True, **kwargs) -> db.Key: """ From 3c8680fde2c95ba67392486d23cf6209641e7fab Mon Sep 17 00:00:00 2001 From: Jan Max Meyer Date: Tue, 28 Jan 2025 16:22:48 +0100 Subject: [PATCH 2/2] fix: Improve and standardize `Script` module `vfuncs` (#1388) - Fixes #1386 - Fixes #1387 --------- Co-authored-by: Sven Eberth --- src/viur/core/modules/script.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/viur/core/modules/script.py b/src/viur/core/modules/script.py index 9634e3d59..d1001f1ce 100644 --- a/src/viur/core/modules/script.py +++ b/src/viur/core/modules/script.py @@ -1,22 +1,18 @@ -import re import typing as t from viur.core.bones import * from viur.core.prototypes.tree import Tree, TreeSkel, SkelType +from viur.core.modules.file import File from viur.core import db, conf, current, skeleton, tasks, errors from viur.core.decorators import exposed from viur.core.i18n import translate -# pre-compile patterns for vfuncs -DIRECTORY_PATTERN = re.compile(r'^[a-zA-Z0-9äöüÄÖÜ_-]*$') -FILE_PATTERN = re.compile(r'^[a-zA-Z0-9äöüÄÖÜ_-]+?.py$') - class BaseScriptAbstractSkel(TreeSkel): path = StringBone( descr="Path", readOnly=True, - unique=UniqueValue(UniqueLockMethod.SameValue, True, "This path name is already taken!") + unique=UniqueValue(UniqueLockMethod.SameValue, True, "This path is already taken!") ) @classmethod @@ -24,7 +20,7 @@ def fromClient(cls, skel, data, *args, **kwargs): # Set script name when provided, so that the path can be regenerated if name := data.get("name"): skel["name"] = name - conf.main_app.vi.script.update_path(skel) + conf.main_app.script.update_path(skel) ret = super().fromClient(skel, data, *args, **kwargs) @@ -54,7 +50,7 @@ class ScriptNodeSkel(BaseScriptAbstractSkel): name = StringBone( descr="Folder", required=True, - vfunc=lambda value: not DIRECTORY_PATTERN.match(value) + vfunc=lambda value: None if File.is_valid_filename(value) else "Foldername is invalid" ) @@ -63,12 +59,15 @@ class ScriptLeafSkel(BaseScriptAbstractSkel): name = StringBone( descr="Filename", - vfunc=lambda value: not FILE_PATTERN.match(value), + required=True, + vfunc=lambda value: + None if File.is_valid_filename(value) and value.endswith(".py") and value.removesuffix(".py") + else "Filename is invalid or doesn't have a '.py'-suffix", ) script = RawBone( descr="Code", - indexed=False + indexed=False, ) access = SelectBone(