Ejemplo n.º 1
0
    def visit(self, path_from: str, pre_children: 'VisitCallback',
              post_children: 'VisitCallback') -> bool:
        path = self._fetch_declared_path(path_from)

        if pre_children and pre_children(self, path):
            return False

        if self.parent and self.parent.visit('{}/parent/'.format(path),
                                             pre_children, post_children):
            return True

        if self.definition and self.definition.visit(
                '{}/definition/'.format(path), pre_children, post_children):
            return True

        if self.contents and CdmObject._visit_array(
                self.contents, '{}/'.format(path), pre_children,
                post_children):
            return True

        if self.lineage is not None and CdmObject._visit_array(
                self.lineage, '{}/lineage/'.format(path), pre_children,
                post_children):
            return True

        if self._visit_def(path, pre_children, post_children):
            return True

        if post_children and post_children(self, path):
            return True

        return False
Ejemplo n.º 2
0
    def visit(self, path_from: str, pre_children: 'VisitCallback',
              post_children: 'VisitCallback') -> bool:
        path = ''
        if self.ctx.corpus._block_declared_path_changes is False:
            path = self._declared_path
            if not path:
                path = path_from + self.name
                self._declared_path = path

        if pre_children and pre_children(self, path):
            return False

        if self.parent and self.parent.visit('{}/parent/'.format(path),
                                             pre_children, post_children):
            return True

        if self.definition and self.definition.visit(
                '{}/definition/'.format(path), pre_children, post_children):
            return True

        if self.contents and CdmObject._visit_array(
                self.contents, '{}/'.format(path), pre_children,
                post_children):
            return True

        if self._visit_def(path, pre_children, post_children):
            return True

        if post_children and post_children(self, path):
            return True

        return False
Ejemplo n.º 3
0
    def __init__(self, res_opt: 'ResolveOptions') -> None:
        from cdm.objectmodel import CdmObject

        self.res_opt = CdmObject._copy_resolve_options(res_opt)  # type: ResolveOptions
        self.rt_set = []  # type: List[ResolvedTrait]
        self.lookup_by_trait = {}  # type: Dict[CdmTraitDefinition, ResolvedTrait]
        self.has_elevated = False  # type: bool
Ejemplo n.º 4
0
    def _create_child_under(
            res_opt: 'ResolveOptions',
            acp: 'AttributeContextParameters') -> 'CdmAttributeContext':
        if not acp:
            return None

        if acp._type == CdmAttributeContextType.PASS_THROUGH:
            return acp._under

        # This flag makes sure we hold on to any resolved object refs when things get copied.
        res_opt_copy = CdmObject._copy_resolve_options(res_opt)
        res_opt_copy._save_resolutions_on_copy = True

        definition = None  # type: CdmObjectReference
        rts_applied = None  # type: ResolvedTraitSet

        # Get a simple reference to definition object to avoid getting the traits that might be part of this ref
        # included in the link to the definition.
        if acp._regarding:
            definition = acp._regarding.create_simple_reference(res_opt_copy)
            definition.in_document = acp._under.in_document  # ref is in same doc as context
            # Now get the traits applied at this reference (applied only, not the ones that are part of the definition
            # of the object) and make them the traits for this context.
            if acp._include_traits:
                rts_applied = acp._regarding._fetch_resolved_traits(
                    res_opt_copy)

        under_child = acp._under.ctx.corpus.make_object(
            CdmObjectType.ATTRIBUTE_CONTEXT_DEF,
            acp._name)  # type: CdmAttributeContext
        # Need context to make this a 'live' object.
        under_child.ctx = acp._under.ctx
        under_child.in_document = acp._under.in_document
        under_child.type = acp._type
        under_child.definition = definition
        # Add traits if there are any.
        if rts_applied and rts_applied.rt_set:
            for rt in rts_applied.rt_set:
                trait_ref = CdmObject._resolved_trait_to_trait_ref(
                    res_opt_copy, rt)
                under_child.exhibits_traits.append(trait_ref,
                                                   isinstance(trait_ref, str))

        # Add to parent.
        under_child._update_parent(res_opt_copy, acp._under)

        return under_child
Ejemplo n.º 5
0
    def __init__(self, res_opt: 'ResolveOptions', res_guide: 'CdmAttributeResolutionGuidanceDefinition', traits: 'ResolvedTraitSet') -> None:
        # Collect a set of appliers for all traits.
        self.res_opt = res_opt  # type: ResolveOptions
        self.res_guide = res_guide  # type: CdmAttributeResolutionGuidanceDefinition
        self.traits_to_apply = traits  # type: ResolvedTraitSet

        self.actions_modify = []  # type: List[AttributeResolutionApplier]
        self.actions_group_add = []  # type: List[AttributeResolutionApplier]
        self.actions_round_add = []  # type: List[AttributeResolutionApplier]
        self.actions_attribute_add = []  # type: List[AttributeResolutionApplier]
        self.actions_remove = []  # type: List[AttributeResolutionApplier]
        self.applier_caps = None  # type: Optional[AttributeResolutionApplierCapabilities]

        from cdm.objectmodel import CdmObject
        self.res_opt = CdmObject._copy_resolve_options(res_opt)

        if not res_guide:
            return

        if not self.applier_caps:
            self.applier_caps = AttributeResolutionApplierCapabilities()

        def add_applier(apl: 'AttributeResolutionApplier') -> bool:
            # Collect the code that will perform the right action.
            # Associate with the resolved trait and get the priority.

            if apl._will_attribute_modify and apl._do_attribute_modify:
                self.actions_modify.append(apl)
                self.applier_caps.can_attribute_modify = True

            if apl._will_attribute_add and apl._do_attribute_add:
                self.actions_attribute_add.append(apl)
                self.applier_caps.can_attribute_add = True

            if apl._will_group_add and apl._do_group_add:
                self.actions_group_add.append(apl)
                self.applier_caps.can_group_add = True

            if apl._will_round_add and apl._do_round_add:
                self.actions_round_add.append(apl)
                self.applier_caps.can_round_add = True

            if apl._will_alter_directives and apl._do_alter_directives:
                self.applier_caps.can_alter_directives = True
                apl._do_alter_directives(self.res_opt, res_guide)

            if apl._will_create_context and apl._do_create_context:
                self.applier_caps.can_create_context = True

            if apl._will_remove:
                self.actions_remove.append(apl)
                self.applier_caps.can_remove = True

            return True

        if res_guide.remove_attribute:
            add_applier(primitive_appliers._is_removed)
        if res_guide.imposed_directives:
            add_applier(primitive_appliers._does_impose_directives)
        if res_guide.removed_directives:
            add_applier(primitive_appliers._does_remove_directives)
        if res_guide.add_supporting_attribute:
            add_applier(primitive_appliers._does_add_supporting_attribute)
        if res_guide.rename_format:
            add_applier(primitive_appliers._does_disambiguate_names)
        if res_guide.cardinality == 'many':
            add_applier(primitive_appliers._does_explain_array)
        if res_guide.entity_by_reference:
            add_applier(primitive_appliers._does_reference_entity_via)
        if res_guide.selects_sub_attribute and res_guide.selects_sub_attribute.selects == 'one':
            add_applier(primitive_appliers._does_select_attributes)

        # Sorted by priority.
        self.actions_modify.sort(key=lambda ara: ara._priority)
        self.actions_group_add.sort(key=lambda ara: ara._priority)
        self.actions_round_add.sort(key=lambda ara: ara._priority)
        self.actions_attribute_add.sort(key=lambda ara: ara._priority)