Ejemplo n.º 1
0
    async def test_pattern_with_non_existing_folder(self):
        corpus = TestHelper.get_local_corpus(
            self.test_subpath, "test_pattern_with_non_existing_folder")
        content = TestHelper.get_input_file_content(
            self.test_subpath, "test_pattern_with_non_existing_folder",
            "entities.manifest.cdm.json")
        manifest_content = ManifestContent()
        manifest_content.decode(content)
        cdmManifest = ManifestPersistence.from_object(
            CdmCorpusContext(corpus, None), "entities", "local", "/",
            manifest_content)

        error_logged = 0

        def callback(level: CdmStatusLevel, message: str):
            if 'Failed to fetch all files in the folder location \'local:/testLocation\' described by a partition pattern. Exception:' in message:
                nonlocal error_logged
                error_logged += 1

        corpus.set_event_callback(callback, CdmStatusLevel.WARNING)

        await cdmManifest.file_status_check_async()
        self.assertEqual(1, error_logged)
        self.assertEqual(len(cdmManifest.entities[0].data_partitions), 0)
        # make sure the last check time is still being set
        self.assertIsNotNone(
            cdmManifest.entities[0].data_partition_patterns[0].
            last_file_status_check_time)
Ejemplo n.º 2
0
    def test_load_local_entity_with_data_partition_pattern(self):
        content = TestHelper.get_input_file_content(
            self.test_subpath,
            "test_load_local_entity_with_data_partition_pattern",
            "entities.manifest.cdm.json")
        manifest_content = ManifestContent()
        manifest_content.decode(content)

        cdmManifest = ManifestPersistence.from_object(
            CdmCorpusContext(CdmCorpusDefinition(), None), "entities",
            "testNamespace", "/", manifest_content)
        self.assertEqual(len(cdmManifest.entities), 1)
        self.assertEqual(cdmManifest.entities[0].object_type,
                         CdmObjectType.LOCAL_ENTITY_DECLARATION_DEF)
        entity = cdmManifest.entities[0]
        self.assertEqual(len(entity.data_partition_patterns), 1)
        pattern = entity.data_partition_patterns[0]
        self.assertEqual(pattern.name, "testPattern")
        self.assertEqual(pattern.explanation, "test explanation")
        self.assertEqual(pattern.root_location, "test location")
        self.assertEqual(pattern.regular_expression, "\\s*")
        self.assertEqual(len(pattern.parameters), 2)
        self.assertEqual(pattern.parameters[0], "testParam1")
        self.assertEqual(pattern.parameters[1], "testParam2")
        self.assertEqual(pattern.specialized_schema, "test special schema")
        self.assertEqual(len(pattern.exhibits_traits), 1)
Ejemplo n.º 3
0
    async def test_from_and_to_data(self):
        with open(os.path.join(ROOT_PATH,
                               'default.manifest.cdm.json')) as manifestFile:
            manifestContent = ManifestContent()
            manifestContent.decode(manifestFile.read())

        corpus = CdmCorpusDefinition()
        corpus.storage.default_namespace = 'local'
        corpus.ctx.update_logging_options(level='WARNING')

        corpus.storage.mount('cdm',
                             LocalAdapter(root="../CDM.SchemaDocuments"))
        corpus.storage.mount('local', LocalAdapter(root=ROOT_PATH))
        corpus.storage.mount(
            'remote', RemoteAdapter(hosts={"contoso": "http://contoso.com"}))
        folder = corpus.storage.fetch_root_folder('local')

        manifest = await corpus.fetch_object_async('default.manifest.cdm.json',
                                                   folder)
        data = await ManifestPersistence.to_data(manifest, None, None)

        for entity in manifest.entities:
            entity_def = await corpus.fetch_object_async(
                entity.entity_path, manifest)

        await manifest.save_as_async('test_output/new.manifest.cdm.json',
                                     save_referenced=True)

        self.assertDictEqual(json.loads(manifestContent.encode()),
                             json.loads(data.encode()))
Ejemplo n.º 4
0
    def test_load_local_entity_with_data_partition_pattern(self):
        content = TestHelper.get_input_file_content(
            self.test_subpath,
            'test_load_local_entity_with_data_partition_pattern',
            'entities.manifest.cdm.json')
        manifest_content = ManifestContent()
        manifest_content.decode(content)

        cdm_manifest = ManifestPersistence.from_object(
            CdmCorpusContext(CdmCorpusDefinition(), None), 'entities',
            'testNamespace', '/', manifest_content)
        self.assertEqual(len(cdm_manifest.entities), 2)
        entity1 = cdm_manifest.entities[0]
        self.assertEqual(entity1.object_type,
                         CdmObjectType.LOCAL_ENTITY_DECLARATION_DEF)
        self.assertEqual(len(entity1.data_partition_patterns), 1)
        pattern1 = entity1.data_partition_patterns[0]
        self.assertEqual(pattern1.name, 'testPattern')
        self.assertEqual(pattern1.explanation, 'test explanation')
        self.assertEqual(pattern1.root_location, 'test location')
        self.assertEqual(pattern1.regular_expression, '\\s*')
        self.assertEqual(len(pattern1.parameters), 2)
        self.assertEqual(pattern1.parameters[0], 'testParam1')
        self.assertEqual(pattern1.parameters[1], 'testParam2')
        self.assertEqual(pattern1.specialized_schema, 'test special schema')
        self.assertEqual(len(pattern1.exhibits_traits), 1)

        entity2 = cdm_manifest.entities[1]
        self.assertEqual(entity2.object_type,
                         CdmObjectType.LOCAL_ENTITY_DECLARATION_DEF)
        self.assertEqual(len(entity2.data_partition_patterns), 1)
        pattern2 = entity2.data_partition_patterns[0]
        self.assertEqual(pattern2.name, 'testPattern2')
        self.assertEqual(pattern2.root_location, 'test location2')
        self.assertEqual(pattern2.glob_pattern, '/*.csv')

        manifest_data = ManifestPersistence.to_data(cdm_manifest, None, None)
        self.assertEqual(len(manifest_data.entities), 2)
        entity_data1 = manifest_data.entities[0]
        self.assertEqual(len(entity_data1.dataPartitionPatterns), 1)
        pattern_data1 = entity_data1.dataPartitionPatterns[0]
        self.assertEqual(pattern_data1.name, 'testPattern')
        self.assertEqual(pattern_data1.explanation, 'test explanation')
        self.assertEqual(pattern_data1.rootLocation, 'test location')
        self.assertEqual(pattern_data1.regularExpression, '\\s*')
        self.assertEqual(len(pattern_data1.parameters), 2)
        self.assertEqual(pattern_data1.parameters[0], 'testParam1')
        self.assertEqual(pattern_data1.parameters[1], 'testParam2')
        self.assertEqual(pattern_data1.specializedSchema,
                         'test special schema')
        self.assertEqual(len(pattern_data1.exhibitsTraits), 1)

        pattern_data2 = manifest_data.entities[1].dataPartitionPatterns[0]
        self.assertEqual(pattern_data2.name, 'testPattern2')
        self.assertEqual(pattern_data2.rootLocation, 'test location2')
        self.assertEqual(pattern_data2.globPattern, '/*.csv')
    async def test_pattern_with_non_existing_folder(self):
        corpus = TestHelper.get_local_corpus(self.test_subpath, "test_pattern_with_non_existing_folder")
        content = TestHelper.get_input_file_content(self.test_subpath, "test_pattern_with_non_existing_folder", "entities.manifest.cdm.json")
        manifest_content = ManifestContent()
        manifest_content.decode(content)

        cdmManifest = ManifestPersistence.from_object(CdmCorpusContext(corpus, None), "entities", "local", "/", manifest_content)
        await cdmManifest.file_status_check_async()
        self.assertEqual(len(cdmManifest.entities[0].data_partitions), 0)
        # make sure the last check time is still being set
        self.assertIsNotNone(cdmManifest.entities[0].data_partition_patterns[0].last_file_status_check_time)
Ejemplo n.º 6
0
    async def test_load_local_entitiy_with_data_partition(self):
        content = TestHelper.get_input_file_content(
            self.test_subpath, 'test_load_local_entity_with_data_partition',
            'entities.manifest.cdm.json')
        manifest_content = ManifestContent()
        manifest_content.decode(content)

        cdm_manifest = await ManifestPersistence.from_data(
            CdmCorpusContext(CdmCorpusDefinition()), 'entities',
            'testNamespace', '/', manifest_content)
        self.assertEqual(len(cdm_manifest.entities), 1)
        self.assertEqual(cdm_manifest.entities[0].object_type,
                         CdmObjectType.LOCAL_ENTITY_DECLARATION_DEF)
        entity = cdm_manifest.entities[0]
        self.assertEqual(len(entity.data_partitions), 2)
        relative_partition = entity.data_partitions[0]
        self.assertEqual(relative_partition.name, 'Sample data partition')
        self.assertEqual(relative_partition.location, 'test/location')
        # self.assertEqual(TimeUtils.GetFormattedDateString(relative_partition.LastFileModifiedTime), '2008-09-15T23:53:23.000Z')
        self.assertEqual(len(relative_partition.exhibits_traits), 1)
        self.assertEqual(relative_partition.specialized_schema, 'teststring')

        test_list = relative_partition.arguments['test']
        self.assertEqual(len(test_list), 3)
        self.assertEqual(test_list[0], 'something')
        self.assertEqual(test_list[1], 'somethingelse')
        self.assertEqual(test_list[2], 'anotherthing')

        key_list = relative_partition.arguments['KEY']
        self.assertEqual(len(key_list), 1)
        self.assertEqual(key_list[0], 'VALUE')

        self.assertFalse('wrong' in relative_partition.arguments)

        absolute_partition = entity.data_partitions[1]
        self.assertEqual(absolute_partition.location,
                         "local:/some/test/location")
Ejemplo n.º 7
0
    async def _load_document_from_path_async(self, folder: 'CdmFolderDefinition', doc_name: str,
                                             doc_container: 'CdmDocumentDefinition',
                                             res_opt: Optional[ResolveOptions] = None) \
            -> 'CdmDocumentDefinition':
        #  go get the doc
        doc_content = None  # type: Optional[CdmDocumentDefinition]
        json_data = None
        fs_modified_time = None
        doc_path = folder.folder_path + doc_name
        adapter = self._ctx.corpus.storage.fetch_adapter(
            folder.namespace)  # type: StorageAdapter

        try:
            if adapter.can_read():
                # log message used by navigator, do not change or remove
                logger.debug(self._ctx, self._TAG,
                             self._load_document_from_path_async.__name__,
                             doc_path, 'request file: {}'.format(doc_path))
                json_data = await adapter.read_async(doc_path)
                # log message used by navigator, do not change or remove
                logger.debug(self._ctx, self._TAG,
                             self._load_document_from_path_async.__name__,
                             doc_path, 'received file: {}'.format(doc_path))
            else:
                raise Exception('Storage Adapter is not enabled to read.')
        except Exception as e:
            # log message used by navigator, do not change or remove
            logger.debug(self._ctx, self._TAG,
                         self._load_document_from_path_async.__name__,
                         doc_path, 'fail file: {}'.format(doc_path))

            # when shallow validation is enabled, log messages about being unable to find referenced documents as warnings instead of errors.
            if res_opt and res_opt.shallow_validation:
                logger.warning(
                    self._ctx, self._TAG,
                    PersistenceLayer._load_document_from_path_async.__name__,
                    doc_path, CdmLogCode.WARN_PERSIST_FILE_READ_FAILURE,
                    doc_path, folder.Namespace, e)
            else:
                logger.error(self._ctx, self._TAG,
                             self._load_document_from_path_async.__name__,
                             doc_path,
                             CdmLogCode.ERR_PERSIST_FILE_READ_FAILURE,
                             doc_path, str(folder.namespace), e)
            return None

        try:
            fs_modified_time = await adapter.compute_last_modified_time_async(
                doc_path)
        except Exception as e:
            logger.warning(
                self._ctx, self._TAG,
                PersistenceLayer._load_document_from_path_async.__name__,
                doc_path, CdmLogCode.WARN_PERSIST_FILE_MOD_COMPUTE_FAILED,
                e.Message)

        if not doc_name:
            logger.error(self._ctx, self._TAG,
                         self._load_document_from_path_async.__name__,
                         doc_path, CdmLogCode.ERR_PERSIST_NULL_DOC_NAME)
            return None

        doc_name_lower = doc_name.lower()

        # If loading an model.json file, check that it is named correctly.
        if doc_name_lower.endswith(
                self.MODEL_JSON_EXTENSION
        ) and not doc_name.lower() == self.MODEL_JSON_EXTENSION:
            logger.error(self._ctx, self._TAG,
                         self._load_document_from_path_async.__name__,
                         doc_path,
                         CdmLogCode.ERR_PERSIST_DOC_NAME_LOAD_FAILURE,
                         doc_name, self.MODEL_JSON_EXTENSION)
            return None

        try:
            if doc_name_lower.endswith(PersistenceLayer.MANIFEST_EXTENSION
                                       ) or doc_name_lower.endswith(
                                           PersistenceLayer.FOLIO_EXTENSION):
                from cdm.persistence.cdmfolder import ManifestPersistence
                from cdm.persistence.cdmfolder.types import ManifestContent
                manifest = ManifestContent()
                manifest.decode(json_data)
                doc_content = ManifestPersistence.from_object(
                    self._ctx, doc_name, folder.namespace, folder.folder_path,
                    manifest)
            elif doc_name_lower.endswith(
                    PersistenceLayer.MODEL_JSON_EXTENSION):
                if doc_name_lower != PersistenceLayer.MODEL_JSON_EXTENSION:
                    logger.error(self._ctx, self._TAG,
                                 self._load_document_from_path_async.__name__,
                                 doc_path,
                                 CdmLogCode.ERR_PERSIST_DOC_NAME_LOAD_FAILURE,
                                 doc_name, self.MODEL_JSON_EXTENSION)
                    return None
                from cdm.persistence.modeljson import ManifestPersistence
                from cdm.persistence.modeljson.types import Model
                model = Model()
                model.decode(json_data)
                doc_content = await ManifestPersistence.from_object(
                    self._ctx, model, folder)
            elif doc_name_lower.endswith(PersistenceLayer.CDM_EXTENSION):
                from cdm.persistence.cdmfolder import DocumentPersistence
                from cdm.persistence.cdmfolder.types import DocumentContent
                document = DocumentContent()
                document.decode(json_data)
                doc_content = DocumentPersistence.from_object(
                    self._ctx, doc_name, folder.namespace, folder.folder_path,
                    document)
            else:
                # Could not find a registered persistence class to handle this document type.
                logger.error(self._ctx, self._TAG,
                             self._load_document_from_path_async.__name__,
                             doc_path, CdmLogCode.ERR_PERSIST_CLASS_MISSING,
                             doc_name)
                return None
        except Exception as e:
            logger.error(self._ctx, self._TAG,
                         self._load_document_from_path_async.__name__,
                         doc_path,
                         CdmLogCode.ERR_PERSIST_DOC_CONVERSION_FAILURE,
                         doc_path, e)
            return None

        # add document to the folder, this sets all the folder/path things, caches name to content association and may trigger indexing on content
        if doc_content is not None:
            if doc_container:
                # there are situations where a previously loaded document must be re-loaded.
                # the end of that chain of work is here where the old version of the document has been removed from
                # the corpus and we have created a new document and loaded it from storage and after this call we will probably
                # add it to the corpus and index it, etc.
                # it would be really rude to just kill that old object and replace it with this replicant, especially because
                # the caller has no idea this happened. so... sigh ... instead of returning the new object return the one that
                # was just killed off but make it contain everything the new document loaded.
                doc_content = doc_content.copy(
                    ResolveOptions(wrt_doc=doc_container,
                                   directives=self._ctx.corpus.
                                   default_resolution_directives),
                    doc_container)

            folder.documents.append(doc_content, doc_name)
            doc_content._file_system_modified_time = fs_modified_time
            doc_content._is_dirty = False

        return doc_content
Ejemplo n.º 8
0
    async def _fetch_document_from_folder_path_async(
            self, document_path: str, adapter: 'StorageAdapterBase',
            force_reload: bool) -> 'CdmDocumentDefinition':
        """Gets the document from folder path.

        arguments:
        path: The path.
        adapter: The storage adapter where the document can be found."""
        from .cdm_corpus_def import FOLIO_EXTENSION, MANIFEST_EXTENSION, MODEL_JSON_EXTENSION

        doc_name = None
        first = document_path.find('/')

        if first < 0:
            doc_name = document_path
        else:
            doc_name = document_path[0:first]

        if doc_name in self._document_lookup and not force_reload:
            return self._document_lookup[doc_name]

        is_cdm_folder = doc_name.endswith(
            FOLIO_EXTENSION) or doc_name.endswith(MANIFEST_EXTENSION)
        is_model_json = doc_name.endswith(MODEL_JSON_EXTENSION)

        # got that doc?
        doc = None

        #  go get the doc
        doc_path = '{}{}'.format(self.folder_path, doc_name)
        json_data = None
        fs_modified_time = None

        try:
            if adapter.can_read():
                json_data = await adapter.read_async(doc_path)
                fs_modified_time = await adapter.compute_last_modified_time_async(
                    adapter.create_adapter_path(doc_path))
        except Exception as e:
            self.corpus.ctx.logger.exception(
                'Could not read %s from the \'%s\' namespace.', doc_path,
                self.namespace)
            return None

        if is_cdm_folder:
            from cdm.persistence.cdmfolder import ManifestPersistence
            from cdm.persistence.cdmfolder.types import ManifestContent
            manifest = ManifestContent()
            manifest.decode(json_data)
            doc = await ManifestPersistence.from_data(self.ctx, doc_name,
                                                      self.namespace,
                                                      self.folder_path,
                                                      manifest)
            doc.folder = self
            self.documents.append(doc)
            self.corpus._add_document_objects(self, doc)
            self._document_lookup[doc_name] = doc
        elif is_model_json:
            from cdm.persistence.modeljson import ManifestPersistence
            from cdm.persistence.modeljson.types import Model
            model = Model()
            model.decode(json_data)
            doc = await ManifestPersistence.from_data(self.ctx, model, self)
            doc.folder = self
            doc.folder_path = self.folder_path
        else:
            from cdm.persistence.cdmfolder import DocumentPersistence
            from cdm.persistence.cdmfolder.types import DocumentContent
            document = DocumentContent()
            document.decode(json_data)
            doc = self.documents.append(await DocumentPersistence.from_data(
                self.ctx, doc_name, self.namespace, self.folder_path,
                document))

        doc._file_system_modified_time = fs_modified_time
        doc._is_dirty = False

        return doc
Ejemplo n.º 9
0
async def load_document_from_path_async(
        folder: 'CdmFolderDefinition', doc_name: str,
        doc_container: 'CdmDocumentDefinition') -> 'CdmDocumentDefinition':
    is_cdm_folder = doc_name.lower().endswith(
        CdmCorpusDefinition._FOLIO_EXTENSION) or doc_name.lower().endswith(
            CdmCorpusDefinition._MANIFEST_EXTENSION)
    is_model_json = doc_name.lower().endswith(
        CdmCorpusDefinition._MODEL_JSON_EXTENSION)

    #  go get the doc
    doc_content = None  # type: Optional[CdmDocumentDefinition]
    json_data = None
    fs_modified_time = None
    ctx = folder.ctx
    doc_path = folder.folder_path + doc_name
    adapter = ctx.corpus.storage.fetch_adapter(
        folder.namespace)  # type: StorageAdapter

    try:
        if adapter.can_read():
            json_data = await adapter.read_async(doc_path)
            fs_modified_time = await adapter.compute_last_modified_time_async(
                adapter.create_adapter_path(doc_path))
            ctx.logger.info('read file: {}'.format(doc_path))
    except Exception as e:
        ctx.logger.error(
            'could not read %s from the \'%s\' namespace.\n Reason: \n%s',
            doc_path, folder.namespace, e)
        return None

    try:
        if is_cdm_folder:
            from cdm.persistence.cdmfolder import ManifestPersistence
            from cdm.persistence.cdmfolder.types import ManifestContent
            manifest = ManifestContent()
            manifest.decode(json_data)
            doc_content = await ManifestPersistence.from_data(
                ctx, doc_name, folder.namespace, folder.folder_path, manifest)
        elif is_model_json:
            if doc_name.lower() != CdmCorpusDefinition._MODEL_JSON_EXTENSION:
                ctx.logger.error(
                    'Failed to load \'{}\', as it\'s not an acceptable filename. It must be model.json'
                    .format(doc_name))
                return None
            from cdm.persistence.modeljson import ManifestPersistence
            from cdm.persistence.modeljson.types import Model
            model = Model()
            model.decode(json_data)
            doc_content = await ManifestPersistence.from_data(
                ctx, model, folder)
        else:
            from cdm.persistence.cdmfolder import DocumentPersistence
            from cdm.persistence.cdmfolder.types import DocumentContent
            document = DocumentContent()
            document.decode(json_data)
            doc_content = await DocumentPersistence.from_data(
                ctx, doc_name, folder.namespace, folder.folder_path, document)
    except Exception as e:
        ctx.logger.error('Could not convert \'{}\'. Reason \'{}\''.format(
            doc_path, e))
        return None

    # add document to the folder, this sets all the folder/path things, caches name to content association and may trigger indexing on content
    if doc_content is not None:
        if doc_container:
            # there are situations where a previously loaded document must be re-loaded.
            # the end of that chain of work is here where the old version of the document has been removed from
            # the corpus and we have created a new document and loaded it from storage and after this call we will probably
            # add it to the corpus and index it, etc.
            # it would be really rude to just kill that old object and replace it with this replicant, especially because
            # the caller has no idea this happened. so... sigh ... instead of returning the new object return the one that
            # was just killed off but make it contain everything the new document loaded.
            doc_content = doc_content.copy(
                ResolveOptions(wrt_doc=doc_container), doc_container)

        folder.documents.append(doc_content, doc_name)
        doc_content._file_system_modified_time = fs_modified_time
        doc_content._is_dirty = False

    return doc_content
Ejemplo n.º 10
0
entityJson = entity.encode()
entityCheck = JObject(entityJson)
entityCheckJson = entityCheck.encode()

assert entityJson == entityCheckJson

# Test loading a manifest from a file, and printing it out

manifestJson = ''

with open('tests/cdmfolder/default.manifest.cdm.json') as manifestJsonFile:
    manifestJson = manifestJsonFile.read()

manifestContent = ManifestContent()
manifestContent.decode(manifestJson)

kvprint('name', manifestContent.manifestName, 3, 0)
kvprint('explanation', str(manifestContent.get('explanation')), 3, 0)
kvprint('jsonSchemaSemanticVersion', manifestContent.jsonSchemaSemanticVersion,
        3, 0)

if manifestContent.get('imports'):
    kvprint('imports', '', 6, 0)
    for the_import in manifestContent.get('imports'):
        kvprint('corpusPath', the_import.corpusPath, 3, 4)
        kvprint('moniker', the_import.moniker, 3, 4)

if manifestContent.get('entities'):
    kvprint('entities', '', 6, 0)
    for entity in manifestContent.get('entities'):