def wrapper(*args, **kwargs): try: base = ipwrapper.ruleList() func(*args, **kwargs) except Exception: restoreRules(base) raise
def rules(): rules_data = ruleList() for rule_data in rules_data: try: r = Rule.fromText(rule_data) yield IPRuleData(r.destination, r.source, r.srcDevice, r.table) except ValueError: logging.warning('Could not parse rule %s', rule_data)
def restoreRules(base): current = ipwrapper.ruleList() added = set(current) - set(base) for rule in added: ipwrapper.ruleDel(ipwrapper.Rule.fromText(rule))