Ejemplo n.º 1
0
    async def from_data(ctx: 'CdmCorpusContext', document_folder: 'CdmFolderDefinition', data: 'LocalEntity',
                        extension_trait_def_list: List['CdmTraitDefinition'], manifest: 'CdmManifestDefinition') -> 'CdmLocalEntityDeclarationDefinition':
        local_entity_dec = ctx.corpus.make_object(CdmObjectType.LOCAL_ENTITY_DECLARATION_DEF, data.name)

        local_extension_trait_def_list = []  # type: List[CdmTraitDefinition]
        entity_doc = await DocumentPersistence.from_data(ctx, data, extension_trait_def_list, local_extension_trait_def_list)

        if not entity_doc:
            logger.error(_TAG, ctx, 'There was an error while trying to fetch the entity doc from local entity declaration persistence.')
            return None

        document_folder.documents.append(entity_doc)

        # Entity schema path is the path to the doc containing the entity definition.
        local_entity_dec.entity_path = ctx.corpus.storage.create_relative_corpus_path('{}/{}'.format(entity_doc.at_corpus_path, data.name), manifest)

        local_entity_dec.explanation = data.get('description')

        if data.get('lastFileStatusCheckTime'):
            local_entity_dec.last_file_status_check_time = dateutil.parser.parse(data.get('lastFileStatusCheckTime'))

        if data.get('lastFileModifiedTime'):
            local_entity_dec.last_file_modified_time = dateutil.parser.parse(data.get('lastFileModifiedTime'))

        if data.get('lastChildFileModifiedTime'):
            local_entity_dec.last_child_file_modified_time = dateutil.parser.parse(data.get('lastChildFileModifiedTime'))

        if data.get('isHidden'):
            is_hidden_trait = ctx.corpus.make_object(CdmObjectType.TRAIT_REF, 'is.hidden', True)
            local_entity_dec.exhibits_traits.append(is_hidden_trait)

        # Add traits for schema entity info.
        if data.get('schemas'):
            t2pm = TraitToPropertyMap(local_entity_dec)
            t2pm.update_property_value('cdmSchemas', data.get('schemas'))

        # Data partitions are part of the local entity, add them here.
        for element in (data.get('partitions') or []):
            data_partition = await DataPartitionPersistence.from_data(ctx, element, extension_trait_def_list, local_extension_trait_def_list, document_folder)
            if data_partition is not None:
                local_entity_dec.data_partitions.append(data_partition)
            else:
                logger.error(_TAG, ctx, 'There was an error while trying to convert model.json partition to cdm local data partition.')
                return None

        import_docs = await extension_helper.standard_import_detection(ctx, extension_trait_def_list, local_extension_trait_def_list)  # type: List[CdmImport]
        extension_helper.add_import_docs_to_manifest(ctx, import_docs, entity_doc)

        return local_entity_dec
Ejemplo n.º 2
0
    def test_update_and_fetch_list_lookup(self):
        """Test update and fetch list lookup default value without attributeValue and displayOrder."""
        corpus = CdmCorpusDefinition()
        cdm_attribute = CdmTypeAttributeDefinition(corpus.ctx, 'SomeAttribute')
        trait_to_property_map = TraitToPropertyMap(cdm_attribute)

        constant_values = [{'languageTag': 'en', 'displayText': 'Fax'}]

        trait_to_property_map.update_property_value('defaultValue',
                                                    constant_values)
        result = trait_to_property_map.fetch_property_value('defaultValue')

        self.assertEqual(1, len(result))
        self.assertEqual('en', result[0].get('languageTag'))
        self.assertEqual('Fax', result[0].get('displayText'))
        self.assertIsNone(result[0].get('attributeValue'))
        self.assertIsNone(result[0].get('displayOrder'))
Ejemplo n.º 3
0
class CdmDataPartitionDefinition(CdmObjectDefinition, CdmFileStatus):
    def __init__(self, ctx: 'CdmCorpusContext', name: str) -> None:
        super().__init__(ctx)

        # The name of a data partition.
        self.name = name  # type: str

        # The corpus path for the data file location.
        self.location = None  # type: Optional[str]

        # Indicates whether this partition is inferred.
        self.inferred = False  # type: bool

        # The list of key value pairs to give names for the replacement values from the RegEx.
        self.arguments = {}  # type: Dict[str, List[str]]

        # The path of a specialized schema to use specifically for the partitions generated.
        self.specialized_schema = None  # type: Optional[str]

        # The refresh time of the partition.
        self.refresh_time = None  # type: Optional[datetime]

        self.last_child_file_modified_time = None  # type: Optional[datetime]

        self.last_file_modified_time = None  # type: Optional[datetime]

        self.last_file_status_check_time = None  # type: Optional[datetime]

        # --- Internal ---

        self._ttpm = TraitToPropertyMap(self)

    @property
    def object_type(self) -> 'CdmObjectType':
        return CdmObjectType.DATA_PARTITION_DEF

    @property
    def description(self) -> str:
        return cast(str, self._ttpm.fetch_property_value('description'))

    @description.setter
    def description(self, val: str) -> None:
        self._ttpm.update_property_value('description', val)

    def copy(
        self,
        res_opt: Optional['ResolveOptions'] = None,
        host: Optional['CdmDataPartitionDefinition'] = None
    ) -> 'CdmDataPartitionDefinition':
        if not res_opt:
            res_opt = ResolveOptions(wrt_doc=self)

        if not host:
            copy = CdmDataPartitionDefinition(self.ctx, self.name)
        else:
            copy = host
            copy.ctx = self.ctx
            copy.name = self.name

        copy.description = self.description
        copy.location = self.location
        copy.last_file_status_check_time = self.last_file_status_check_time
        copy.last_file_modified_time = self.last_file_modified_time
        copy.inferred = self.inferred
        copy.arguments = self.arguments
        copy.specialized_schema = self.specialized_schema
        self._copy_def(res_opt, copy)

        return copy

    async def file_status_check_async(self) -> None:
        """Check the modified time for this object and any children."""
        namespace = self.in_document.namespace
        full_path = self.location if ':' in self.location else (namespace +
                                                                ':' +
                                                                self.location)
        modified_time = await (
            cast('CdmCorpusDefinition', self.ctx.corpus)
        )._fetch_last_modified_time_from_partition_path_async(full_path)
        # Update modified times.
        self.last_file_modified_time = time_utils.max_time(
            modified_time, self.last_file_modified_time)
        await self.report_most_recent_time_async(self.last_file_modified_time)

    def get_name(self) -> str:
        return self.name

    def is_derived_from(self,
                        base: str,
                        res_opt: Optional['ResolveOptions'] = None) -> bool:
        return False

    async def report_most_recent_time_async(self,
                                            child_time: datetime) -> None:
        """Report most recent modified time (of current or children objects) to the parent object."""
        if cast(CdmFileStatus,
                self.owner).report_most_recent_time_async and child_time:
            await cast(CdmFileStatus,
                       self.owner).report_most_recent_time_async(child_time)

    def validate(self) -> bool:
        return bool(self.location)

    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 = '{}{}'.format(path_from, (self.get_name() or 'UNNAMED'))
                self._declared_path = path

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

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

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

        return False