From 756efd2cd8ed32324fdc5278257300e71880e350 Mon Sep 17 00:00:00 2001 From: Dealga McArdle Date: Fri, 3 Jun 2022 16:53:40 +0200 Subject: [PATCH] Oldnodes parser retrofit (#4509) * retrofit parser module * fix module-string --- old_nodes/formula.py | 2 +- old_nodes/formula2.py | 2 +- old_nodes/profile_mk2.py | 2 +- utils/modules/parser_subset.py | 26 ++++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 utils/modules/parser_subset.py diff --git a/old_nodes/formula.py b/old_nodes/formula.py index d2196dfd0a..a882cd26cc 100644 --- a/old_nodes/formula.py +++ b/old_nodes/formula.py @@ -16,11 +16,11 @@ # # ##### END GPL LICENSE BLOCK ##### -import parser import bpy from bpy.props import StringProperty +from sverchok.utils.modules.parser_subset import parser from sverchok.node_tree import SverchCustomTreeNode from sverchok.data_structure import updateNode from math import cos, sin, pi, tan diff --git a/old_nodes/formula2.py b/old_nodes/formula2.py index 114463e316..f3ff75820e 100644 --- a/old_nodes/formula2.py +++ b/old_nodes/formula2.py @@ -16,7 +16,6 @@ # # ##### END GPL LICENSE BLOCK ##### -import parser from math import ( acos, acosh, asin, asinh, atan, atan2, atanh, ceil, copysign, cos, cosh, degrees, e, @@ -29,6 +28,7 @@ import bpy from bpy.props import BoolProperty, StringProperty +from sverchok.utils.modules.parser_subset import parser from sverchok.node_tree import SverchCustomTreeNode from sverchok.data_structure import ( updateNode, multi_socket, changable_sockets, diff --git a/old_nodes/profile_mk2.py b/old_nodes/profile_mk2.py index 2553963e4c..fcb7b5cb93 100644 --- a/old_nodes/profile_mk2.py +++ b/old_nodes/profile_mk2.py @@ -22,7 +22,7 @@ import json from string import ascii_lowercase -import parser +from sverchok.utils.modules.parser_subset import parser from ast import literal_eval import bpy diff --git a/utils/modules/parser_subset.py b/utils/modules/parser_subset.py new file mode 100644 index 0000000000..dd08172451 --- /dev/null +++ b/utils/modules/parser_subset.py @@ -0,0 +1,26 @@ +""" +in Python 3.10 the parser module has been removed. some old nodes +(namely: formula.py / formula2.py / profile_mk2.py) make light use of that module. + +The main goal of this module is to suppress a startup import exception caused by +the missing parser module, and at the same time offer the basic features of that +module as used by these nodes. +""" + +try: + import parser + +except (ImportError, ModuleNotFoundError) as err: + + from dataclasses import dataclass + + @dataclass + class Expr(str): + str_formula: str + def compile(self): + # print('use mock') + return compile(self.str_formula, '', mode='eval') + + parser = lambda: None + parser.expr = lambda str_formula: Expr(str_formula) +