async def from_data(
        ctx: 'CdmCorpusContext', data_obj: 'LocalEntity',
        extension_trait_def_list: List['CdmTraitDefinition'],
        local_extension_trait_def_list: List['CdmTraitDefinition']
    ) -> Optional['CdmDocumentDefinition']:
        doc = ctx.corpus.make_object(CdmObjectType.DOCUMENT_DEF,
                                     '{}.cdm.json'.format(data_obj.name))

        # import at least foundations
        doc.imports.append('cdm:/foundations.cdm.json')

        entity_dec = await EntityPersistence.from_data(
            ctx, data_obj, extension_trait_def_list,
            local_extension_trait_def_list)

        if not entity_dec:
            logger.error(
                DocumentPersistence.__name__, ctx,
                'There was an error while trying to convert a model.json entity to the CDM entity.'
            )
            return None

        if data_obj.get('imports'):
            for element in data_obj.imports:
                if element.corpusPath == 'cdm:/foundations.cdm.json':
                    # don't add foundations twice
                    continue

                doc.imports.append(CdmImportPersistence.from_data(
                    ctx, element))

        doc.definitions.append(entity_dec)

        return doc
示例#2
0
    async def to_data(document_object_or_path: Union[CdmDocumentDefinition, str], manifest: 'CdmManifestDefinition', res_opt: 'ResolveOptions', options: 'CopyOptions',
                      ctx: 'CdmCorpusContext') -> Optional['LocalEntity']:
        if isinstance(document_object_or_path, str):
            # Fetch the document from entity schema.
            cdm_entity = await ctx.corpus.fetch_object_async(document_object_or_path, manifest)

            if not cdm_entity:
                logger.error(DocumentPersistence.__name__, ctx, 'There was an error while trying to fetch cdm entity doc.')
                return None

            entity = await EntityPersistence.to_data(cdm_entity, res_opt, options, ctx)
            if cdm_entity.owner and cdm_entity.owner.object_type == CdmObjectType.DOCUMENT_DEF:
                document = cdm_entity.owner  # type: CdmDocumentDefinition
                for element in document.imports:
                    imp = CdmImportPersistence.to_data(element, res_opt, options)
                    # the corpus path in the imports are relative to the document where it was defined.
                    # when saving in model.json the documents are flattened to the manifest level
                    # so it is necessary to recalculate the path to be relative to the manifest.
                    absolute_path = ctx.corpus.storage.create_absolute_corpus_path(imp.corpusPath, document)

                    if document.namespace and absolute_path.startswith(document.namespace + ':'):
                        absolute_path = absolute_path[len(document.namespace) + 1:]

                    imp.corpusPath = ctx.corpus.storage.create_relative_corpus_path(absolute_path, manifest)
                    entity.imports.append(imp)
            else:
                logger.warning(DocumentPersistence.__name__, ctx, 'Entity {} is not inside a document or its owner is not a document.'.format(
                    cdm_entity.get_name()))
            return entity
        else:
            # TODO: Do something else when document_object_or_path is an object.
            pass
示例#3
0
    async def from_data(
        ctx: 'CdmCorpusContext', data_obj: 'LocalEntity',
        extension_trait_def_list: List['CdmTraitDefinition'],
        local_extension_trait_def_list: List['CdmTraitDefinition']
    ) -> Optional['CdmDocumentDefinition']:
        doc = ctx.corpus.make_object(CdmObjectType.DOCUMENT_DEF,
                                     '{}.cdm.json'.format(data_obj.name))

        # import at least foundations
        doc.imports.append(Constants._FOUNDATIONS_CORPUS_PATH)

        entity_dec = await EntityPersistence.from_data(
            ctx, data_obj, extension_trait_def_list,
            local_extension_trait_def_list)

        if not entity_dec:
            logger.error(
                ctx, DocumentPersistence.__name__,
                DocumentPersistence.from_data.__name__, None,
                CdmLogCode.ERR_PERSIST_MODELJSON_ENTITY_CONVERSION_ERROR,
                data_obj.name)
            return None

        if data_obj.get('imports'):
            for element in data_obj.imports:
                if element.corpusPath == Constants._FOUNDATIONS_CORPUS_PATH:
                    # don't add foundations twice
                    continue

                doc.imports.append(CdmImportPersistence.from_data(
                    ctx, element))

        doc.definitions.append(entity_dec)

        return doc
示例#4
0
    async def to_data(document_object_or_path: Union[CdmDocumentDefinition,
                                                     str],
                      res_opt: 'ResolveOptions', options: 'CopyOptions',
                      ctx: 'CdmCorpusContext') -> Optional['LocalEntity']:
        if isinstance(document_object_or_path, str):
            # Fetch the document from entity schema.
            cdm_entity = await ctx.corpus.fetch_object_async(
                document_object_or_path)

            if not cdm_entity:
                ctx.logger.error(
                    'There was an error while trying to fetch cdm entity doc.')
                return None

            entity = await EntityPersistence.to_data(cdm_entity, res_opt,
                                                     options, ctx)
            if cdm_entity.owner and cdm_entity.owner.object_type == CdmObjectType.DOCUMENT_DEF:
                document = cdm_entity.owner  # type: CdmDocumentDefinition
                for element in document.imports:
                    imp = CdmImportPersistence.to_data(element, res_opt,
                                                       options)
                    if imp:
                        entity.imports.append(imp)
            else:
                ctx.logger.warning(
                    'Entity {} is not inside a document or its owner is not a document.'
                    .format(cdm_entity.get_name()))
            return entity
        else:
            # TODO: Do something else when document_object_or_path is an object.
            pass
示例#5
0
    async def to_data(document_object_or_path: Union[CdmDocumentDefinition,
                                                     str],
                      manifest: 'CdmManifestDefinition',
                      res_opt: 'ResolveOptions', options: 'CopyOptions',
                      ctx: 'CdmCorpusContext') -> Optional['LocalEntity']:
        if isinstance(document_object_or_path, str):
            # Fetch the document from entity schema.
            cdm_entity = await ctx.corpus.fetch_object_async(
                document_object_or_path, manifest)

            if not isinstance(cdm_entity, CdmEntityDefinition):
                logger.error(ctx, DocumentPersistence.__name__,
                             DocumentPersistence.to_data.__name__,
                             manifest.at_corpus_path,
                             CdmLogCode.ERR_INVALID_CAST,
                             document_object_or_path, 'CdmEntityDefinition')
                return None
            if not cdm_entity:
                logger.error(ctx, DocumentPersistence.__name__,
                             DocumentPersistence.to_data.__name__,
                             manifest.at_corpus_path,
                             CdmLogCode.ERR_PERSIST_CDM_ENTITY_FETCH_ERROR)
                return None

            entity = await EntityPersistence.to_data(cdm_entity, res_opt,
                                                     options, ctx)
            if cdm_entity.owner and cdm_entity.owner.object_type == CdmObjectType.DOCUMENT_DEF:
                document = cdm_entity.owner  # type: CdmDocumentDefinition
                entity.imports = []
                for element in document.imports:
                    imp = CdmImportPersistence.to_data(element, res_opt,
                                                       options)
                    # the corpus path in the imports are relative to the document where it was defined.
                    # when saving in model.json the documents are flattened to the manifest level
                    # so it is necessary to recalculate the path to be relative to the manifest.
                    absolute_path = ctx.corpus.storage.create_absolute_corpus_path(
                        imp.corpusPath, document)

                    if not StringUtils.is_blank_by_cdm_standard(
                            document._namespace) and absolute_path.startswith(
                                document._namespace + ':'):
                        absolute_path = absolute_path[len(document._namespace
                                                          ) + 1:]

                    imp.corpusPath = ctx.corpus.storage.create_relative_corpus_path(
                        absolute_path, manifest)
                    entity.imports.append(imp)
            else:
                logger.warning(ctx, _TAG, DocumentPersistence.to_data.__name__,
                               manifest.at_corpus_path,
                               CdmLogCode.WARN_PERSIST_ENTITY_MISSING,
                               cdm_entity.get_name())
            return entity
        else:
            # TODO: Do something else when document_object_or_path is an object.
            pass