<commit_before># Copyright (c) 2014, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

import stix
import stix.utils
import stix.indicator.test_mechanism
from stix.common import EncodedCDATA
from stix.indicator.test_mechanism import _BaseTestMechanism
import stix.bindings.extensions.test_mechanism.yara as yara_tm_binding

class YaraTestMechanism(_BaseTestMechanism):
    _namespace = "http://stix.mitre.org/extensions/TestMechanism#YARA-1"
    _binding = yara_tm_binding
    _binding_class = _binding.YaraTestMechanismType
    _XSI_TYPE = "yaraTM:YaraTestMechanismType"
    
    def __init__(self, id_=None, idref=None):
        super(YaraTestMechanism, self).__init__(id_=id_, idref=idref)
        self.version = None
        self.rule = None
    
    @property
    def rule(self):
        return self._rule
    
    @rule.setter
    def rule(self, value):
        if not value:
            self._rule = None
        if isinstance(value, EncodedCDATA):
            self._rule = value
        else:
            self._rule = EncodedCDATA(value=value)
    
    @classmethod
    def from_obj(cls, obj, return_obj=None):
        if not obj:
            return None
        if not return_obj:
            return_obj = cls()
        
        super(YaraTestMechanism, cls).from_obj(obj, return_obj)
        return_obj.version = obj.Version 
        return_obj.rule = EncodedCDATA.from_obj(obj.Rule)
        
        return return_obj
    
    def to_obj(self, return_obj=None, ns_info=None):
        if not return_obj:
            return_obj = self._binding_class()
            
        super(YaraTestMechanism, self).to_obj(return_obj=return_obj, ns_info=ns_info)
        return_obj.Version = self.version
        return_obj.Rule = self.rule.to_obj(ns_info=ns_info)
        
        return return_obj
    
    @classmethod
    def from_dict(cls, d, return_obj=None):
        if not d:
            return None
        if not return_obj:
            return_obj = cls()
            
        super(YaraTestMechanism, cls).from_dict(d, return_obj)
        return_obj.version = d.get('version')
        return_obj.rule = EncodedCDATA.from_dict(d.get('rule'))
        
        return return_obj
    
    def to_dict(self):
        d = super(YaraTestMechanism, self).to_dict()
        
        if self.version:
            d['version'] = self.version
        if self.rule:
            d['rule'] = self.rule.to_dict()
        
        return d
    
stix.indicator.test_mechanism.add_extension(YaraTestMechanism)
<commit_msg>Added conditions to to_obj() in yara test mechanism<commit_after># Copyright (c) 2014, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

import stix
import stix.utils
import stix.indicator.test_mechanism
from stix.common import EncodedCDATA
from stix.indicator.test_mechanism import _BaseTestMechanism
import stix.bindings.extensions.test_mechanism.yara as yara_tm_binding

class YaraTestMechanism(_BaseTestMechanism):
    _namespace = "http://stix.mitre.org/extensions/TestMechanism#YARA-1"
    _binding = yara_tm_binding
    _binding_class = _binding.YaraTestMechanismType
    _XSI_TYPE = "yaraTM:YaraTestMechanismType"
    
    def __init__(self, id_=None, idref=None):
        super(YaraTestMechanism, self).__init__(id_=id_, idref=idref)
        self.version = None
        self.rule = None
    
    @property
    def rule(self):
        return self._rule
    
    @rule.setter
    def rule(self, value):
        if not value:
            self._rule = None
        if isinstance(value, EncodedCDATA):
            self._rule = value
        else:
            self._rule = EncodedCDATA(value=value)
    
    @classmethod
    def from_obj(cls, obj, return_obj=None):
        if not obj:
            return None
        if not return_obj:
            return_obj = cls()
        
        super(YaraTestMechanism, cls).from_obj(obj, return_obj)
        return_obj.version = obj.Version 
        return_obj.rule = EncodedCDATA.from_obj(obj.Rule)
        
        return return_obj
    
    def to_obj(self, return_obj=None, ns_info=None):
        if not return_obj:
            return_obj = self._binding_class()
            
        super(YaraTestMechanism, self).to_obj(return_obj=return_obj, ns_info=ns_info)

        if self.version:
            return_obj.Version = self.version
        if self.rule:
            return_obj.Rule = self.rule.to_obj(ns_info=ns_info)
        
        return return_obj
    
    @classmethod
    def from_dict(cls, d, return_obj=None):
        if not d:
            return None
        if not return_obj:
            return_obj = cls()
            
        super(YaraTestMechanism, cls).from_dict(d, return_obj)
        return_obj.version = d.get('version')
        return_obj.rule = EncodedCDATA.from_dict(d.get('rule'))
        
        return return_obj
    
    def to_dict(self):
        d = super(YaraTestMechanism, self).to_dict()
        
        if self.version:
            d['version'] = self.version
        if self.rule:
            d['rule'] = self.rule.to_dict()
        
        return d
    
stix.indicator.test_mechanism.add_extension(YaraTestMechanism)

{'fraction_non_alphanumeric': 0.0856812041682748, 'fraction_numerical': 0.0019297568506368198, 'mean_word_length': 2.3033779477374123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5708628', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1655, 'n_words': 327}