<commit_before>import re

from vusion.error import InvalidField, MissingField
from vusion_model import VusionModel

class UnattachMessage(VusionModel):
    
    MODEL_TYPE = 'unattach_message'
    MODEL_VERSION = '3'
    
    fields = []
    
    UNATTACH_MESSAGE_FIELDS = {
        'name' : lambda v: v is not None,
        'send-to-type': lambda v: v in ['all', 'match'],
        'content': lambda v: v is not None,
        'type-schedule': lambda v: v in ['fixed-time', 'immediately'],
        'fixed-time': lambda v: re.match(re.compile('^(\d{4})-0?(\d+)-0?(\d+)T0?(\d+):0?(\d+)(:0?(\d+))$'), v)
    }
    
    tag_regex = re.compile('^[a-zA-Z0-9\s]+$')
    label_regex = re.compile('^[a-zA-Z0-9\s]+:[a-zA-Z0-9\s]+$')    

    SEND_TO_FIELDS = {
        'all': {},
        'match': {
            'send-to-match-operator': lambda v: v in ['all', 'any'],
            'send-to-match-conditions': lambda v: isinstance(v, list)
            },
        }
    
    OPERATORS = {
        'all': '$and',
        'any': '$or'
        }

    def validate_fields(self):
        for field, check in self.UNATTACH_MESSAGE_FIELDS.items():
            self.assert_field_present(field)
            if not check(self[field]):
                raise InvalidField(field)
        for field, check in self.SEND_TO_FIELDS[self['send-to-type']].items():
            self.assert_field_present(field)
            if not check(self[field]):
                raise InvalidField(field)
    
    def upgrade(self, **kwargs):
        if kwargs['model-version'] in ['1', None]:
            kwargs['to'] = ['all-participants']
            kwargs['model-version'] = '2'
            return self.upgrade(**kwargs)
        elif kwargs['model-version'] == '2':
            if 'all-participants' in kwargs['to']:    
                kwargs['send-to-type'] = 'all'
            else:
                kwargs['send-to-type'] = 'match'
                kwargs['send-to-match-operator'] = 'any'
                kwargs['send-to-match-conditions'] = kwargs['to']
            kwargs.pop('to')
            kwargs['model-version'] = '3'
        return kwargs

    def get_selector_as_query(self):
        query = None
        if self['send-to-type'] == 'all':
            return {}
        elif self['send-to-type'] == 'match':
            operator = self.OPERATORS[self['send-to-match-operator']]
            for selector in self['send-to-match-conditions']:
                if re.match(self.tag_regex, selector):
                    tmp_query = {'tags': selector}
                if re.match(self.label_regex, selector):
                    profile = selector.split(':')
                    tmp_query = {'profile': {'$elemMatch': {'label': profile[0], 'value': profile[1]}}}
                if query is None:
                    query = tmp_query
                else:
                    if operator in query:
                        query[operator].append(tmp_query)
                    else:<commit_msg>modified unattach_message model<commit_after>import re

from vusion.error import InvalidField, MissingField
from vusion_model import VusionModel

class UnattachMessage(VusionModel):
    
    MODEL_TYPE = 'unattach_message'
    MODEL_VERSION = '4'
    
    fields = []
    
    UNATTACH_MESSAGE_FIELDS = {
        'name' : lambda v: v is not None,
        'send-to-type': lambda v: v in ['all', 'match'],
        'content': lambda v: v is not None,
        'type-schedule': lambda v: v in ['fixed-time', 'immediately'],
        'fixed-time': lambda v: re.match(re.compile('^(\d{4})-0?(\d+)-0?(\d+)T0?(\d+):0?(\d+)(:0?(\d+))$'), v)
    }
    
    tag_regex = re.compile('^[a-zA-Z0-9\s]+$')
    label_regex = re.compile('^[a-zA-Z0-9\s]+:[a-zA-Z0-9\s]+$')    

    SEND_TO_FIELDS = {
        'all': {},
        'match': {
            'send-to-match-operator': lambda v: v in ['all', 'any'],
            'send-to-match-conditions': lambda v: isinstance(v, list)
            },
        }
    
    OPERATORS = {
        'all': '$and',
        'any': '$or'
        }

    def validate_fields(self):
        for field, check in self.UNATTACH_MESSAGE_FIELDS.items():
            self.assert_field_present(field)
            if not check(self[field]):
                raise InvalidField(field)
        for field, check in self.SEND_TO_FIELDS[self['send-to-type']].items():
            self.assert_field_present(field)
            if not check(self[field]):
                raise InvalidField(field)
    
    def upgrade(self, **kwargs):
        if kwargs['model-version'] in ['1', None]:
            kwargs['to'] = ['all-participants']
            kwargs['model-version'] = '2'
            return self.upgrade(**kwargs)
        elif kwargs['model-version'] == '2':
            if 'all-participants' in kwargs['to']:    
                kwargs['send-to-type'] = 'all'
            else:
                kwargs['send-to-type'] = 'match'
                kwargs['send-to-match-operator'] = 'any'
                kwargs['send-to-match-conditions'] = kwargs['to']
            kwargs.pop('to')
            kwargs['model-version'] = '3'
            return self.upgrade(**kwargs)
        elif kwargs['model-version'] == '3':
            kwargs['created-by'] = kwargs['created-by'] if 'created-by' in kwargs else None
            kwargs['model-version'] = '4' 
        return kwargs

    def get_selector_as_query(self):
        query = None
        if self['send-to-type'] == 'all':
            return {}
        elif self['send-to-type'] == 'match':
            operator = self.OPERATORS[self['send-to-match-operator']]
            for selector in self['send-to-match-conditions']:
                if re.match(self.tag_regex, selector):
                    tmp_query = {'tags': selector}
                if re.match(self.label_regex, selector):
                    profile = selector.split(':')
                    tmp_query = {'profile': {'$elemMatch': {'label': profile[0], 'value': profile[1]}}}
                if query is None:
                    query = tmp_query
                else:
                    if operator in query:
                        query[operator].append(tmp_query)
                    else:
{'fraction_non_alphanumeric': 0.1574074074074074, 'fraction_numerical': 0.00649772579597141, 'mean_word_length': 1.8399446494464944, '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': '5918196', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1859, 'n_words': 382}