def _AddFromConfigHelper(self, document, context):
        """Helper method that reads a single yaml document and adds all fields found.

    Also adds any findings to the field.

    Args:
      document: yaml document
      context: config file context
    """
        if 'literals' not in document:
            self.AddFinding(
                findings_lib.UnrecognizedFormatError(document, context))
            return
        field_list = document['literals']
        if field_list is None:
            self.AddFinding(findings_lib.EmptyBlockWarning(document, context))
            return
        for field_spec in field_list:
            field_name = ''
            states = None
            if isinstance(field_spec, dict):
                # If the field has a list of states, field_spec must be a dict with
                # a single entry.
                try:
                    [(field_name, states)] = field_spec.items()
                except ValueError:
                    self.AddFinding(
                        findings_lib.InvalidFieldFormatError(
                            document, context))
                    continue
            else:
                field_name = field_spec
            field = Field(field_name, states, context)
            self.AddField(field)
Beispiel #2
0
    def _ConstructType(self, type_name, type_contents, filepath):
        """Reads a entity type config block and generates an EntityType object."""

        description = ''
        parents = None
        local_field_names = None
        opt_local_field_names = None
        is_abstract = False
        is_canonical = False
        uid = None

        expected_keys = set([
            'description', 'implements', 'uses', 'opt_uses', 'is_abstract',
            'id', 'is_canonical'
        ])

        if 'description' in type_contents:
            description = type_contents['description']
        if 'implements' in type_contents:
            parents = type_contents['implements']
        if 'uses' in type_contents:
            local_field_names = type_contents['uses']
        if 'opt_uses' in type_contents:
            opt_local_field_names = type_contents['opt_uses']
        if 'is_abstract' in type_contents:
            is_abstract = type_contents['is_abstract']
        if 'is_canonical' in type_contents:
            is_canonical = type_contents['is_canonical']
        if 'id' in type_contents:
            uid = type_contents['id']

        # Generate tuples to represent each field
        fq_lfn = []
        if local_field_names:
            self._ConstructField(local_field_names, False, fq_lfn)
        if opt_local_field_names:
            self._ConstructField(opt_local_field_names, True, fq_lfn)

        entity_type = EntityType(filepath=filepath,
                                 typename=type_name,
                                 description=description,
                                 parents=parents,
                                 local_field_tuples=fq_lfn,
                                 is_abstract=is_abstract,
                                 inherited_fields_expanded=False,
                                 is_canonical=is_canonical,
                                 uid=uid,
                                 namespace=self.local_namespace)

        # Add errors to type if there's anything extra in the block.  We add to the
        # entity type because an extra key here is likely a typo in a real key name
        # that would result in information being lost from the type.
        for key in type_contents:
            if key not in expected_keys:
                entity_type.AddFinding(
                    findings_lib.UnrecognizedFormatError(
                        key, entity_type.file_context))

        return entity_type