Exemple #1
0
    def _extractRules(self):
        """Extract rules to a document fragment
        """

        site = self.environ.getSite()
        storage = queryUtility(IRuleStorage)
        if storage is None:
            return
        fragment = self._doc.createDocumentFragment()

        assignment_paths = set()

        for name, rule in storage.items():
            rule_node = self._doc.createElement('rule')

            rule_node.setAttribute('name', name)
            rule_node.setAttribute('title', rule.title)
            rule_node.setAttribute('description', rule.description)
            rule_node.setAttribute('event', _getDottedName(rule.event))
            rule_node.setAttribute('enabled', str(rule.enabled))
            rule_node.setAttribute('stop-after', str(rule.stop))
            rule_node.setAttribute('cascading', str(rule.cascading))
            # Aq-wrap so that exporting fields with clever getters or
            # vocabularies will work. We also aq-wrap conditions and
            # actions below.

            rule = rule.__of__(site)

            # Add conditions
            conditions_node = self._doc.createElement('conditions')
            for condition in rule.conditions:
                condition_data = IRuleElementData(condition)
                condition = condition.__of__(rule)

                condition_node = self._doc.createElement('condition')
                condition_node.setAttribute('type', condition_data.element)

                handler = IRuleElementExportImportHandler(condition)
                handler.export_element(self._doc, condition_node)
                conditions_node.appendChild(condition_node)
            rule_node.appendChild(conditions_node)

            # Add actions
            actions_node = self._doc.createElement('actions')
            for action in rule.actions:
                action_data = IRuleElementData(action)
                action = action.__of__(rule)

                action_node = self._doc.createElement('action')
                action_node.setAttribute('type', action_data.element)

                handler = IRuleElementExportImportHandler(action)
                handler.export_element(self._doc, action_node)
                actions_node.appendChild(action_node)
            rule_node.appendChild(actions_node)

            fragment.appendChild(rule_node)
            assignment_paths.update(get_assignments(rule))
        # Export assignments last - this is necessary to ensure they
        # are orderd properly

        site_path_length = len('/'.join(site.getPhysicalPath()))
        for path in assignment_paths:
            try:
                container = site.unrestrictedTraverse(path)
            except KeyError:
                continue

            assignable = IRuleAssignmentManager(container, None)
            if assignable is None:
                continue

            location = path[site_path_length:]
            for name, assignment in assignable.items():
                assignment_node = self._doc.createElement('assignment')
                assignment_node.setAttribute('location', location)
                assignment_node.setAttribute('name', name)
                assignment_node.setAttribute('enabled',
                                             str(assignment.enabled))
                assignment_node.setAttribute('bubbles',
                                             str(assignment.bubbles))
                fragment.appendChild(assignment_node)

        return fragment
Exemple #2
0
    def _initRules(self, node):
        """Import rules from the given node
        """

        site = self.environ.getSite()
        storage = queryUtility(IRuleStorage)
        if storage is None:
            return

        for child in node.childNodes:
            if child.nodeName == 'rule':

                rule = None
                name = child.getAttribute('name')
                if name:
                    rule = storage.get(name, None)

                if rule is None:
                    rule = Rule()

                    if not name:
                        chooser = INameChooser(storage)
                        name = chooser.chooseName(None, rule)

                    storage[name] = rule
                else:
                    # Clear out conditions and actions since we're expecting
                    # new ones
                    del rule.conditions[:]
                    del rule.actions[:]

                rule.title = child.getAttribute('title')
                rule.description = child.getAttribute('description')
                event_name = child.getAttribute('event')
                rule.event = _resolveDottedName(event_name)
                if not rule.event:
                    raise ImportError('Can not import {0}'.format(event_name))

                rule.enabled = as_bool(child.getAttribute('enabled'), True)
                rule.stop = as_bool(child.getAttribute('stop-after'))
                rule.cascading = as_bool(child.getAttribute('cascading'))
                # Aq-wrap to enable complex setters for elements below
                # to work

                rule = rule.__of__(site)

                for rule_config_node in child.childNodes:
                    if rule_config_node.nodeName == 'conditions':
                        for condition_node in rule_config_node.childNodes:
                            if not condition_node.nodeName == 'condition':
                                continue

                            type_ = condition_node.getAttribute('type')
                            element_type = getUtility(IRuleCondition,
                                                      name=type_)
                            if element_type.factory is None:
                                continue

                            condition = element_type.factory()

                            # Aq-wrap in case of complex setters
                            condition = condition.__of__(rule)

                            handler = IRuleElementExportImportHandler(
                                condition)
                            handler.import_element(condition_node)

                            rule.conditions.append(aq_base(condition))

                    elif rule_config_node.nodeName == 'actions':
                        for action_node in rule_config_node.childNodes:
                            if not action_node.nodeName == 'action':
                                continue

                            type_ = action_node.getAttribute('type')
                            element_type = getUtility(IRuleAction, name=type_)
                            if element_type.factory is None:
                                continue

                            action = element_type.factory()

                            # Aq-wrap in case of complex setters
                            action = action.__of__(rule)

                            handler = IRuleElementExportImportHandler(action)
                            handler.import_element(action_node)

                            rule.actions.append(aq_base(action))

            elif child.nodeName == 'assignment':
                location = child.getAttribute('location')
                if location.startswith('/'):
                    location = location[1:]

                try:
                    container = site.unrestrictedTraverse(str(location))
                except KeyError:
                    continue

                name = child.getAttribute('name')
                api.assign_rule(
                    container,
                    name,
                    enabled=as_bool(child.getAttribute('enabled')),
                    bubbles=as_bool(child.getAttribute('bubbles')),
                    insert_before=child.getAttribute('insert-before'),
                )
    def _initRules(self, node):
        """Import rules from the given node
        """

        site = self.environ.getSite()
        storage = queryUtility(IRuleStorage)
        if storage is None:
            return

        for child in node.childNodes:
            if child.nodeName == 'rule':

                rule = None
                name = child.getAttribute('name')
                if name:
                    rule = storage.get(name, None)

                if rule is None:
                    rule = Rule()

                    if not name:
                        chooser = INameChooser(storage)
                        name = chooser.chooseName(None, rule)

                    storage[name] = rule
                else:
                    # Clear out conditions and actions since we're expecting new ones
                    del rule.conditions[:]
                    del rule.actions[:]

                rule.title = child.getAttribute('title')
                rule.description = child.getAttribute('description')
                event_name = child.getAttribute('event')
                rule.event = _resolveDottedName(event_name)
                if not rule.event:
                    raise ImportError("Can not import %s" % event_name)

                rule.enabled = as_bool(child.getAttribute('enabled'), True)
                rule.stop = as_bool(child.getAttribute('stop-after'))
                rule.cascading = as_bool(child.getAttribute('cascading'))
                # Aq-wrap to enable complex setters for elements below
                # to work

                rule = rule.__of__(site)

                for rule_config_node in child.childNodes:
                    if rule_config_node.nodeName == 'conditions':
                        for condition_node in rule_config_node.childNodes:
                            if not condition_node.nodeName == 'condition':
                                continue

                            type_ = condition_node.getAttribute('type')
                            element_type = getUtility(IRuleCondition, name=type_)
                            if element_type.factory is None:
                                continue

                            condition = element_type.factory()

                            # Aq-wrap in case of complex setters
                            condition = condition.__of__(rule)

                            handler = IRuleElementExportImportHandler(condition)
                            handler.import_element(condition_node)

                            rule.conditions.append(aq_base(condition))

                    elif rule_config_node.nodeName == 'actions':
                        for action_node in rule_config_node.childNodes:
                            if not action_node.nodeName == 'action':
                                continue

                            type_ = action_node.getAttribute('type')
                            element_type = getUtility(IRuleAction, name=type_)
                            if element_type.factory is None:
                                continue

                            action = element_type.factory()

                            # Aq-wrap in case of complex setters
                            action = action.__of__(rule)

                            handler = IRuleElementExportImportHandler(action)
                            handler.import_element(action_node)

                            rule.actions.append(aq_base(action))

            elif child.nodeName == 'assignment':
                location = child.getAttribute('location')
                if location.startswith("/"):
                    location = location[1:]

                try:
                    container = site.unrestrictedTraverse(str(location))
                except KeyError:
                    continue

                name = child.getAttribute('name')
                api.assign_rule(container, name,
                                enabled=as_bool(child.getAttribute('enabled')),
                                bubbles=as_bool(child.getAttribute('bubbles')),
                                insert_before=child.getAttribute('insert-before'),
                                )
    def _extractRules(self):
        """Extract rules to a document fragment
        """

        site = self.environ.getSite()
        storage = queryUtility(IRuleStorage)
        if storage is None:
            return
        fragment = self._doc.createDocumentFragment()

        assignment_paths = set()

        for name, rule in storage.items():
            rule_node = self._doc.createElement('rule')

            rule_node.setAttribute('name', name)
            rule_node.setAttribute('title', rule.title)
            rule_node.setAttribute('description', rule.description)
            rule_node.setAttribute('event', _getDottedName(rule.event))
            rule_node.setAttribute('enabled', str(rule.enabled))
            rule_node.setAttribute('stop-after', str(rule.stop))
            rule_node.setAttribute('cascading', str(rule.cascading))
            # Aq-wrap so that exporting fields with clever getters or
            # vocabularies will work. We also aq-wrap conditions and
            # actions below.

            rule = rule.__of__(site)

            # Add conditions
            conditions_node = self._doc.createElement('conditions')
            for condition in rule.conditions:
                condition_data = IRuleElementData(condition)
                condition = condition.__of__(rule)

                condition_node = self._doc.createElement('condition')
                condition_node.setAttribute('type', condition_data.element)

                handler = IRuleElementExportImportHandler(condition)
                handler.export_element(self._doc, condition_node)
                conditions_node.appendChild(condition_node)
            rule_node.appendChild(conditions_node)

            # Add actions
            actions_node = self._doc.createElement('actions')
            for action in rule.actions:
                action_data = IRuleElementData(action)
                action = action.__of__(rule)

                action_node = self._doc.createElement('action')
                action_node.setAttribute('type', action_data.element)

                handler = IRuleElementExportImportHandler(action)
                handler.export_element(self._doc, action_node)
                actions_node.appendChild(action_node)
            rule_node.appendChild(actions_node)

            fragment.appendChild(rule_node)
            assignment_paths.update(get_assignments(rule))
        # Export assignments last - this is necessary to ensure they
        # are orderd properly

        site_path_length = len('/'.join(site.getPhysicalPath()))
        for path in assignment_paths:
            try:
                container = site.unrestrictedTraverse(path)
            except KeyError:
                continue

            assignable = IRuleAssignmentManager(container, None)
            if assignable is None:
                continue

            location = path[site_path_length:]
            for name, assignment in assignable.items():
                assignment_node = self._doc.createElement('assignment')
                assignment_node.setAttribute('location', location)
                assignment_node.setAttribute('name', name)
                assignment_node.setAttribute('enabled', str(assignment.enabled))
                assignment_node.setAttribute('bubbles', str(assignment.bubbles))
                fragment.appendChild(assignment_node)

        return fragment
    def _initRules(self, node):
        """Import rules from the given node
        """

        site = self.environ.getSite()
        storage = queryUtility(IRuleStorage)
        if storage is None:
            return

        for child in node.childNodes:
            if child.nodeName == 'rule':

                rule = None
                name = child.getAttribute('name')
                if name:
                    rule = storage.get(name, None)

                if rule is None:
                    rule = Rule()

                    if not name:
                        chooser = INameChooser(storage)
                        name = chooser.chooseName(None, rule)

                    storage[name] = rule
                else:
                    # Clear out conditions and actions since we're expecting new ones
                    del rule.conditions[:]
                    del rule.actions[:]

                rule.title = child.getAttribute('title')
                rule.description = child.getAttribute('description')
                event_name = child.getAttribute('event')
                rule.event = _resolveDottedName(event_name)
                if not rule.event:
                    raise ImportError("Can not import %s" % event_name)

                rule.enabled = as_bool(child.getAttribute('enabled'), True)
                rule.stop = as_bool(child.getAttribute('stop-after'))

                # Aq-wrap to enable complex setters for elements below
                # to work

                rule = rule.__of__(site)

                for rule_config_node in child.childNodes:
                    if rule_config_node.nodeName == 'conditions':
                        for condition_node in rule_config_node.childNodes:
                            if not condition_node.nodeName == 'condition':
                                continue

                            type_ = condition_node.getAttribute('type')
                            element_type = getUtility(IRuleCondition,
                                                      name=type_)
                            if element_type.factory is None:
                                continue

                            condition = element_type.factory()

                            # Aq-wrap in case of complex setters
                            condition = condition.__of__(rule)

                            handler = IRuleElementExportImportHandler(
                                condition)
                            handler.import_element(condition_node)

                            rule.conditions.append(aq_base(condition))

                    elif rule_config_node.nodeName == 'actions':
                        for action_node in rule_config_node.childNodes:
                            if not action_node.nodeName == 'action':
                                continue

                            type_ = action_node.getAttribute('type')
                            element_type = getUtility(IRuleAction, name=type_)
                            if element_type.factory is None:
                                continue

                            action = element_type.factory()

                            # Aq-wrap in case of complex setters
                            action = action.__of__(rule)

                            handler = IRuleElementExportImportHandler(action)
                            handler.import_element(action_node)

                            rule.actions.append(aq_base(action))

            elif child.nodeName == 'assignment':
                location = child.getAttribute('location')
                if location.startswith("/"):
                    location = location[1:]

                try:
                    container = site.unrestrictedTraverse(str(location))
                except KeyError:
                    continue

                assignable = IRuleAssignmentManager(container, None)
                if assignable is None:
                    continue

                name = child.getAttribute('name')
                assignment = assignable.get(name, None)
                if assignment is None:
                    assignment = assignable[name] = RuleAssignment(name)

                assignment.enabled = as_bool(child.getAttribute('enabled'))
                assignment.bubbles = as_bool(child.getAttribute('bubbles'))

                insert_before = child.getAttribute('insert-before')
                if insert_before:
                    position = None
                    keys = list(assignable.keys())

                    if insert_before == "*":
                        position = 0
                    elif insert_before in keys:
                        position = keys.index(insert_before)

                    if position is not None:
                        keys.remove(name)
                        keys.insert(position, name)
                        assignable.updateOrder(keys)

                path = '/'.join(container.getPhysicalPath())
                get_assignments(storage[name]).insert(path)