From c29f1be7372de3cb68937d01a5c3e793e1881b03 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 16 Nov 2022 08:41:23 -0600 Subject: firewall: T4821: add utility to load script as module --- python/vyos/util.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'python/vyos/util.py') diff --git a/python/vyos/util.py b/python/vyos/util.py index a80584c5a..9ebe69b6c 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -1143,3 +1143,11 @@ def camel_to_snake_case(name: str) -> str: pattern = r'\d+|[A-Z]?[a-z]+|\W|[A-Z]{2,}(?=[A-Z][a-z]|\d|\W|$)' words = re.findall(pattern, name) return '_'.join(map(str.lower, words)) + +def load_as_module(name: str, path: str): + import importlib.util + + spec = importlib.util.spec_from_file_location(name, path) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod -- cgit v1.2.3