<commit_before># python 2.7

import getpass

from pythonzimbra.tools import auth
from pythonzimbra.request_xml import RequestXml
from pythonzimbra.response_xml import ResponseXml
from pythonzimbra.communication import Communication


def display_rule(rule):
    print '// ' + rule['name']
    if rule['active'] == '0':
        print '// inactive'
    print 'if',
    display_test(rule['filterTests'])
    print '{'
    display_actions(rule['filterActions'])
    print '}'


def display_test(test):
    print test['condition'] + ' ('
    print ',\n'.join(transform_tests(test))
    print ')',


def transform_tests(tests):
    new_tests = []
    for key in ['headerTest', 'sizeTest']:
        if key in tests:
            t = tests[key]
            if not isinstance(t, list):
                t = [t]
            for tt in t:
                tt['test'] = key[:-4]
            new_tests.extend(t)
    return map(show_test, new_tests)


def show_test(test):
    show = '   '
    if test.get('negative') == '1':
        show += 'not '
    if test['test'] == 'header':
        show += 'header :' + test['stringComparison']
        if test.get('caseSensitive') == '1':
            show += ' :comparator "i;ascii-casemap"'
        show += ' ["' + test['header'] + '"] ["' + test['value'] + '"]'
        return show
    if test['test'] == 'address':
        show += 'address :' + test['stringComparison'] + ' :' + test['part']
        if test.get('caseSensitive') == '1':
            show += ' :comparator "i;ascii-casemap"'
        show += ' ["' + test['header'] + '"] ["' + test['value'] + '"]'
        return show
    if test['test'] == 'size':
        show += 'size :' + test['numberComparison'] + ' ' + test['s']


def display_actions(actions):
    a = actions.items()
    a.sort(key=lambda (_, x): x.get('index'))
    for action in a:
        print '  ',
        display_action(action)


def display_action(action):
    if action[0] == 'actionFileInto':
        print 'fileinto "' + action[1]['folderPath'] + '";'
        return
    if action[0] == 'actionStop':
        print 'stop;'
        return
    if action[0] == 'actionRedirect':
        print 'redirect "' + action[1]['a'] + '";'
        return
    if action[0] == 'actionKeep':
        print 'keep;'
        return
    if action[0] == 'actionDiscard':
        print 'discard;'
        return
    # Zimbra specific
    if action[0] == 'actionFlag':
        print 'flag "' + action[1]['flagName'] + '";'
        return
    if action[0] == 'actionTag':
        print 'tag "' + action[1]['tagName'] + '";'
        return
    # reply and notify not taken into account


def main():
    login = '<EMAIL>'
    passwd = <PASSWORD>()
    url = 'https://zimbra.inria.fr/service/soap/'

    token = auth.authenticate(
        url,
        login,
        passwd,
        use_password=True)

    request = RequestXml()
    request.set_auth_token(token)
    request.add_request('GetFilterRulesRequest', {}, 'urn:zimbraMail')

    response = ResponseXml()
    comm = Communication(url)
    comm.send_request(request, response)

    if not response.is_fault():
        rules = response.get_response()
        rules = rules['GetFilterRulesResponse']['filterRules']['filterRule']
        for rule in rules:
            display_rule(rule)
            print

if __name__ == '__main__':
    main()
<commit_msg>handling match against more than one header<commit_after># python 2.7

import getpass

from pythonzimbra.tools import auth
from pythonzimbra.request_xml import RequestXml
from pythonzimbra.response_xml import ResponseXml
from pythonzimbra.communication import Communication


def display_rule(rule):
    print '// ' + rule['name']
    if rule['active'] == '0':
        print '// inactive'
    print 'if',
    display_test(rule['filterTests'])
    print '{'
    display_actions(rule['filterActions'])
    print '}'


def display_test(test):
    print test['condition'] + ' ('
    print ',\n'.join(transform_tests(test))
    print ')',


def transform_tests(tests):
    new_tests = []
    for key in ['headerTest', 'sizeTest']:
        if key in tests:
            t = tests[key]
            if not isinstance(t, list):
                t = [t]
            for tt in t:
                tt['test'] = key[:-4]
            new_tests.extend(t)
    return map(show_test, new_tests)


def show_test(test):
    show = '   '
    if test.get('negative') == '1':
        show += 'not '
    if test['test'] == 'size':
        show += 'size :' + test['numberComparison'] + ' ' + test['s']
        return show
    if test['test'] == 'header':
        show += 'header :' + test['stringComparison']
    if test['test'] == 'address':
        show += 'address :' + test['stringComparison'] + ' :' + test['part']
    if test.get('caseSensitive') == '1':
        show += ' :comparator "i;ascii-casemap"'
    show += ' ["' + '", "'.join(test['header'].split(',')) + \
        '"] ["' + test['value'] + '"]'
    return show


def display_actions(actions):
    a = actions.items()
    a.sort(key=lambda (_, x): x.get('index'))
    for action in a:
        print '  ',
        display_action(action)


def display_action(action):
    if action[0] == 'actionFileInto':
        print 'fileinto "' + action[1]['folderPath'] + '";'
        return
    if action[0] == 'actionStop':
        print 'stop;'
        return
    if action[0] == 'actionRedirect':
        print 'redirect "' + action[1]['a'] + '";'
        return
    if action[0] == 'actionKeep':
        print 'keep;'
        return
    if action[0] == 'actionDiscard':
        print 'discard;'
        return
    # Zimbra specific
    if action[0] == 'actionFlag':
        print 'flag "' + action[1]['flagName'] + '";'
        return
    if action[0] == 'actionTag':
        print 'tag "' + action[1]['tagName'] + '";'
        return
    # reply and notify not taken into account


def main():
    login = '<EMAIL>'
    passwd = <PASSWORD>()
    url = 'https://zimbra.inria.fr/service/soap/'

    token = auth.authenticate(
        url,
        login,
        passwd,
        use_password=True)

    request = RequestXml()
    request.set_auth_token(token)
    request.add_request('GetFilterRulesRequest', {}, 'urn:zimbraMail')

    response = ResponseXml()
    comm = Communication(url)
    comm.send_request(request, response)

    if not response.is_fault():
        rules = response.get_response()
        rules = rules['GetFilterRulesResponse']['filterRules']['filterRule']
        for rule in rules:
            display_rule(rule)
            print

if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.16189896530736458, 'fraction_numerical': 0.0053256238587948874, 'mean_word_length': 2.448583420776495, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3887328', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2028, 'n_words': 467}