def __init__(self): data = {'source': {'operation': 'init', 'timestamp': time.time()}, 'type': 'root', 'policy_path': []} self.policy = ConfigTree(data, sort_key, key_to_string) self.reference = ConfigTree(data, sort_key, key_to_string) self._parser = ConfigParser() self._policy_parser = PolicyParser() self._pattern_parser = PatternParser() self._configs = [ConfigTree(data, sort_key, key_to_string)] self._load_policy()
def clear(self): ''' Clears the current configuration. This removes all current objects and attributes from the configuration. ''' source = {'operation': 'clear', 'timestamp': time.time()} self.current = ConfigTree({'source': source}, sort_key, key_to_string)
def _load_parse_tree(self, parse_tree, cur_stage=None, replace=False, source=None, target='config', allow_new_attrs=False): ''' target can be 'config', 'policy' or 'reference' ''' # TODO accept 'defaults' target too if source is None: source = {} if cur_stage is None: update_target = True if replace: data = {'source': source, 'policy_path': [], 'type': 'root'} stage = ConfigTree(data, sort_key, key_to_string) elif target == 'config': stage = self.current.get_clone() stage.data['source'] = source elif target == 'policy': stage = self.policy.get_clone() stage.data['source'] = source elif target == 'reference': stage = self.reference.get_clone() stage.data['source'] = source else: update_target = False stage = cur_stage loaded = [] log.debug("Loading parse tree %s" % parse_tree) for statement in parse_tree: cur = stage log.debug("Visiting statement %s" % statement) for token in statement: token['source'] = source log.debug("Visiting token %s" % token) if token['type'] == 'obj': log.debug("Loading obj token: %s" % token) if target != 'policy': token = self.validate_obj(token, cur) old = cur.get(token['key']) cur = cur.cine(token['key'], token) if not old: loaded.append(cur) if target != 'policy': self._add_missing_attributes(cur) log.debug("Added object %s" % cur.path) elif token['type'] == 'attr': log.debug("Loading attr token: %s" % token) if target != 'policy': token = self.validate_attr(token, cur, allow_new_attrs) old_nodes = cur.search([(token['key'][0], ".*")]) for old_node in old_nodes: log.debug("Deleting old value: %s\nnew is: %s" % (old_node.path, str(token['key']))) deleted = cur.delete([old_node.key]) log.debug("Deleted: %s" % str(deleted)) cur = cur.cine(token['key'], token) if old_nodes and old_nodes[0].key != cur.key: loaded.append(cur) log.debug("Added attribute %s" % cur.path) elif token['type'] == 'group': log.debug("Loading group token: %s" % token) if target != 'policy': log.debug("cur '%s' token '%s'" % (cur, token)) token['policy_path'] = (cur.data['policy_path'] + [(token['key'][0],)]) old = cur.get(token['key']) cur = cur.cine(token['key'], token) if not old: loaded.append(cur) elif token['type'] == 'block': log.debug("Loading block token: %s" % token) for statement in token['statements']: log.debug("_load_parse_tree recursion on block " "statement: %s" % [statement]) loaded.extend(self._load_parse_tree( [statement], cur, source=source, target=target, allow_new_attrs=allow_new_attrs)) if update_target: if target == 'config': self.current = stage elif target == 'policy': self.policy = stage elif target == 'reference': self.reference = stage return loaded