Esempio n. 1
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
Esempio n. 2
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
Esempio n. 3
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