Beispiel #1
0
    def to_data(instance: CdmAttributeResolutionGuidanceDefinition,
                res_opt: ResolveOptions,
                options: CopyOptions) -> Optional[AttributeResolutionGuidance]:
        if not instance:
            return None

        result = AttributeResolutionGuidance()
        result.removeAttribute = instance.remove_attribute
        result.imposedDirectives = instance.imposed_directives
        result.removedDirectives = instance.removed_directives
        result.cardinality = instance.cardinality
        result.renameFormat = instance.rename_format

        if instance.add_supporting_attribute:
            result.addSupportingAttribute = persistence_layer.to_data(
                instance.add_supporting_attribute, res_opt, 'CdmFolder',
                options)

        if instance.expansion:
            result.expansion = Expansion()
            result.expansion.startingOrdinal = instance.expansion.starting_ordinal
            result.expansion.maximumExpansion = instance.expansion.maximum_expansion

            if instance.expansion.count_attribute:
                result.expansion.countAttribute = persistence_layer.to_data(
                    instance.expansion.count_attribute, res_opt, 'CdmFolder',
                    options)

        if instance.entity_by_reference:
            result.entityByReference = EntityByReference()
            result.entityByReference.allowReference = instance.entity_by_reference.allow_reference
            result.entityByReference.alwaysIncludeForeignKey = instance.entity_by_reference.always_include_foreign_key
            result.entityByReference.referenceOnlyAfterDepth = instance.entity_by_reference.reference_only_after_depth

            if instance.entity_by_reference.foreign_key_attribute:
                result.entityByReference.foreignKeyAttribute = persistence_layer.to_data(
                    instance.entity_by_reference.foreign_key_attribute,
                    res_opt, 'CdmFolder', options)

        if instance.selects_sub_attribute:
            result.selectsSubAttribute = SelectsSubAttribute()
            result.selectsSubAttribute.selects = instance.selects_sub_attribute.selects
            if instance.selects_sub_attribute.selected_type_attribute:
                result.selectsSubAttribute.selectedTypeAttribute = persistence_layer.to_data(
                    instance.selects_sub_attribute.selected_type_attribute,
                    res_opt, 'CdmFolder', options)
                result.selectsSubAttribute.selectsSomeTakeNames = instance.selects_sub_attribute.selects_some_take_names
                result.selectsSubAttribute.selectsSomeAvoidNames = instance.selects_sub_attribute.selects_some_avoid_names

        return result
    def to_data(instance: CdmObjectReference, res_opt: ResolveOptions, options: CopyOptions) -> CdmJsonType:
        # We don't know what object we are creating to initialize to any
        copy = None
        replace = None

        if instance.named_reference:
            identifier = utils.copy_identifier_ref(instance, res_opt, options)
            if instance.simple_named_reference:
                return identifier

            replace = CdmObjectRefPersistence._copy_ref_data(instance, res_opt, copy, identifier, options)

            if replace:
                copy = replace

        elif instance.explicit_reference:
            er_copy = persistence_layer.to_data(instance.explicit_reference, res_opt, 'CdmFolder', options)
            replace = CdmObjectRefPersistence._copy_ref_data(instance, res_opt, copy, er_copy, options)

            if replace:
                copy = replace

        if instance.applied_traits:
            # We don't know if the object we are copying has applied traits or not and hence use any
            copy.appliedTraits = utils.array_copy_data(res_opt, instance.applied_traits, options)

        return copy
Beispiel #3
0
    def to_data(instance: CdmParameterDefinition, res_opt: ResolveOptions,
                options: CopyOptions) -> Parameter:
        def_val = None

        if instance.default_value:
            if isinstance(instance.default_value, CdmObject):
                def_val = persistence_layer.to_data(instance.default_value,
                                                    res_opt, 'CdmFolder',
                                                    options)
            elif isinstance(instance.default_value, str):
                def_val = instance.default_value

        result = Parameter()
        result.name = instance.name
        result.dataType = persistence_layer.to_data(
            instance.data_type_ref, res_opt, 'CdmFolder',
            options) if instance.data_type_ref else None
        result.explanation = instance.explanation
        result.defaultValue = def_val
        result.required = instance.required

        return result
    def to_data(instance: CdmAttributeContext, res_opt: ResolveOptions, options: CopyOptions) -> AttributeContext:
        result = AttributeContext()

        exhibits_traits = [trait for trait in instance.exhibits_traits if not trait.is_from_property]

        result.explanation = instance.explanation
        result.name = instance.name
        result.type = map_enum_to_type_name[instance.type]
        result.parent = AttributeContextReferencePersistence.to_data(instance.parent, res_opt, options) if instance.parent is not None else None
        result.definition = persistence_layer.to_data(instance.definition, res_opt, 'CdmFolder', options) if instance.definition is not None else None
        # I know the trait collection names look wrong. but I wanted to use the def baseclass
        result.appliedTraits = utils.array_copy_data(res_opt, exhibits_traits, options)
        result.contents = utils.array_copy_data(res_opt, instance.contents, options)

        return result
Beispiel #5
0
def array_copy_data(res_opt: ResolveOptions, source: Union['CdmCollection',
                                                           List['CdmObject']],
                    options: CopyOptions) -> Optional[List]:
    """Creates a list object that is a copy of the input IEnumerable object"""
    if not source:
        return None

    casted = []

    for elem in source:
        if elem:
            data = persistence_layer.to_data(elem, res_opt, 'CdmFolder',
                                             options)
            casted.append(data)

    return casted
Beispiel #6
0
    def to_data(instance: 'CdmArgumentDefinition', res_opt: 'ResolveOptions',
                options: 'CopyOptions') -> CdmJsonType:
        from cdm.objectmodel import CdmObject

        value = None

        if instance.value is not None:
            if isinstance(instance.value, CdmObject):
                value = persistence_layer.to_data(instance.value, res_opt,
                                                  'CdmFolder', options)
            else:
                value = instance.value

        # Skip the argument if just a value
        if not instance.name:
            return value

        arg = Argument()
        arg.explanation = instance.explanation
        arg.name = instance.name
        arg.value = value
        return arg
Beispiel #7
0
    def fetch_value_string(self, res_opt: 'ResolveOptions') -> str:
        from cdm.objectmodel import CdmObject

        if self.value is None:
            return ''

        if isinstance(self.value, str):
            return self.value
        elif isinstance(self.value, CdmObject):
            # If this is a constant table, then expand into an HTML table.
            object_def = self.value.fetch_object_definition(
                res_opt)  # type: CdmConstantEntityDefinition
            if self.value.object_type == CdmObjectType.ENTITY_REF and object_def is not None and object_def.object_type == CdmObjectType.CONSTANT_ENTITY_DEF:
                ent_shape = object_def.entity_shape
                ent_values = object_def.constant_values
                if not ent_values:
                    return ''

                rows = []
                shape_atts = ent_shape._fetch_resolved_attributes(res_opt)

                for row_data in ent_values:
                    if not row_data:
                        continue

                    row = {}
                    for (c, tvalue) in enumerate(row_data):
                        col_att = shape_atts.set[c]
                        if col_att is not None and tvalue is not None:
                            row[col_att.resolved_name] = tvalue

                    rows.append(row)

                if rows:
                    keys = list(rows[0].keys())
                    keys.sort()
                    first_key = keys[0]
                    second_key = keys[1] if len(keys) > 1 else keys[0]

                    rows.sort(key=lambda row: (row[first_key].lower(), row[
                        second_key].lower()))

                rows_string = [self._spew_dict(row) for row in rows]

                return '[' + ','.join(rows_string) + ']'

            # Should be a reference to an object.

            from cdm.persistence import persistence_layer
            from cdm.utilities import CopyOptions
            data = persistence_layer.to_data(self.value, res_opt, 'CdmFolder',
                                             CopyOptions(string_refs=False))
            if isinstance(data, str):
                return data

            # TODO: the line bellow won't work, the result is going to be the address of the object.
            return str(data)
        else:
            return str(self.value)

        return ''