| 80 | |
| 81 | |
| 82 | class VocabString(stix.Entity): |
| 83 | _binding = stix_common_binding |
| 84 | _binding_class = stix_common_binding.ControlledVocabularyStringType |
| 85 | _namespace = 'http://docs.oasis-open.org/cti/ns/stix/common-1' |
| 86 | |
| 87 | # All subclasses should override these |
| 88 | _XSI_TYPE = None |
| 89 | _ALLOWED_VALUES = None |
| 90 | |
| 91 | value = fields.TypedField("valueOf_", key_name="value", preset_hook=validate_value) |
| 92 | vocab_name = fields.TypedField("vocab_name") |
| 93 | vocab_reference = fields.TypedField("vocab_reference") |
| 94 | xsi_type = fields.TypedField("xsi_type", key_name="xsi:type") |
| 95 | |
| 96 | def __init__(self, value=None): |
| 97 | super(VocabString, self).__init__() |
| 98 | self.value = value |
| 99 | self.xsi_type = self._XSI_TYPE |
| 100 | |
| 101 | def __str__(self): |
| 102 | return str(self.value) |
| 103 | |
| 104 | def __eq__(self, other): |
| 105 | # Check to make sure the values are identical. |
| 106 | if isinstance(other, VocabString): |
| 107 | other = other.value |
| 108 | |
| 109 | return other == self.value |
| 110 | |
| 111 | def is_plain(self): |
| 112 | """Whether the VocabString can be represented as a single value.""" |
| 113 | return ( |
| 114 | self.xsi_type is None and |
| 115 | self.vocab_name is None and |
| 116 | self.vocab_reference is None |
| 117 | ) |
| 118 | |
| 119 | def to_dict(self): |
| 120 | if self.is_plain(): |
| 121 | return self.value |
| 122 | return super(VocabString, self).to_dict() |
| 123 | |
| 124 | |
| 125 | @classmethod |
| 126 | def from_dict(cls, cls_dict): |
| 127 | if not cls_dict: |
| 128 | vocab = None |
| 129 | elif not isinstance(cls_dict, dict): |
| 130 | vocab = cls() |
| 131 | vocab.value = cls_dict |
| 132 | else: |
| 133 | vocab = super(VocabString, cls).from_dict(cls_dict) |
| 134 | |
| 135 | return vocab |
| 136 | |
| 137 | |
| 138 | def _get_terms(vocab_class): |