amyasnikov/validity

Bug: Unable to use imports inside Name Sets

Closed this issue · 0 comments

Discussed in #128

Originally posted by aascedu October 7, 2024
Hello Anton, hello everyone,

I am trying to use name sets to help with complience tests.
I want to use the re module.

import re

def find_deny_rule(device, target):
    normalized_target = re.sub(r'\s+', ' ', target).strip()
    
    for rule in device.config["access-lists"]["98"]["rules"]:
        if "source" in rule and "mode" in rule:
            normalized_rule = re.sub(r'\s+', ' ', rule).strip()
            if normalized_rule == normalized_target:
                return True
    return False

__all__ = ['find_deny_rule', 're']

Is not allowed in the GUI since the import re is not a from-import.
However how do I import the module re then ? I tried this then :

from re import sub

def find_deny_rule(device, target):
    normalized_target = sub(r'\s+', ' ', target).strip()
    
    for rule in device.config["access-lists"]["98"]["rules"]:
        if "source" in rule and "mode" in rule:
            normalized_rule = sub(r'\s+', ' ', rule).strip()
            if normalized_rule == normalized_target:
                return True
    return False

__all__ = ['find_deny_rule', 'sub']

And I get this error : NameError: name 'sub' is not defined