<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Misc tool functions """

import pythonzimbra
import pythonzimbra.tools.xmlserializer

import re
import hmac
import hashlib
from xml.dom import minidom

re_zuuid = re.compile(r'[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}')
def is_zuuid(s):
    """ Is it a zimbraUUID ?

    example zimbra UUID : d78fd9c9-f000-440b-bce6-ea938d40fa2d
    """
    return re_zuuid.match(s)

def build_preauth_str(preauth_key, account_name, timestamp, expires, admin=False):
    """ Builds the preauthentification string and hmac it, following the zimbra spec.

    spec and examples are here http://wiki.zimbra.com/wiki/Preauth
    """
    if admin:
        s = '{0}|1|name|{1}|{2}'.format(account_name, expires, timestamp)
    else:
        s = '{0}|name|{1}|{2}'.format(account_name, expires, timestamp)

    return hmac.new(preauth_key,s,hashlib.sha1).hexdigest()

def wrap_in_cdata(s):
    return "<![CDATA[{0}]]>".format(s)

def auto_type(s):
    """ Get a XML response and tries to convert it to Python base object
    """
    if s == 'TRUE':
        return True
    elif s == 'FALSE':
        return False
    else:
        try:
            try:
                return int(s)
            except ValueError:
                return float(s)

        except ValueError:
            return s

def auto_untype(arg):
    """ The oposite of auto_type : takes a python base object and tries to
    convert it to XML typed string.
    """
    if arg == True:
        return 'TRUE'
    elif arg == False:
        return 'FALSE'
    else:
        return arg

def xml_str_to_dict(s):
    """ Transforms an XML string it to python-zimbra dict format

    For format, see:
      https://github.com/Zimbra-Community/python-zimbra/blob/master/README.md

    :param: a string, containing XML
    :returns: a dict, with python-zimbra format
    """
    xml = minidom.parseString(s)
    return pythonzimbra.tools.xmlserializer.dom_to_dict(xml.firstChild)<commit_msg>give access to localconfig.xml informations<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Misc tool functions """

import re
import hmac
import hashlib
from xml.dom import minidom
import xml.etree.ElementTree as ET

import pythonzimbra
import pythonzimbra.tools.xmlserializer


re_zuuid = re.compile(r'[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}')
def is_zuuid(s):
    """ Is it a zimbraUUID ?

    example zimbra UUID : d78fd9c9-f000-440b-bce6-ea938d40fa2d
    """
    return re_zuuid.match(s)

def build_preauth_str(preauth_key, account_name, timestamp, expires, admin=False):
    """ Builds the preauthentification string and hmac it, following the zimbra spec.

    spec and examples are here http://wiki.zimbra.com/wiki/Preauth
    """
    if admin:
        s = '{0}|1|name|{1}|{2}'.format(account_name, expires, timestamp)
    else:
        s = '{0}|name|{1}|{2}'.format(account_name, expires, timestamp)

    return hmac.new(preauth_key,s,hashlib.sha1).hexdigest()

def wrap_in_cdata(s):
    return "<![CDATA[{0}]]>".format(s)

def auto_type(s):
    """ Get a XML response and tries to convert it to Python base object
    """
    if s == 'TRUE':
        return True
    elif s == 'FALSE':
        return False
    else:
        try:
            try:
                return int(s)
            except ValueError:
                return float(s)

        except ValueError:
            return s

def auto_untype(arg):
    """ The oposite of auto_type : takes a python base object and tries to
    convert it to XML typed string.
    """
    if arg == True:
        return 'TRUE'
    elif arg == False:
        return 'FALSE'
    else:
        return arg

def xml_str_to_dict(s):
    """ Transforms an XML string it to python-zimbra dict format

    For format, see:
      https://github.com/Zimbra-Community/python-zimbra/blob/master/README.md

    :param: a string, containing XML
    :returns: a dict, with python-zimbra format
    """
    xml = minidom.parseString(s)
    return pythonzimbra.tools.xmlserializer.dom_to_dict(xml.firstChild)


def init_localconfig():
    """ Read localconfig.xml and return a dist with keys/values

    :return: a dict
    """
    root = ET.fromstring(open("/opt/zimbra/conf/localconfig.xml", "r").read())
    values = {}
    for child in root:
        values.update({child.attrib['name']: child.getchildren()[0].text})
    return values
{'fraction_non_alphanumeric': 0.12716499544211485, 'fraction_numerical': 0.01754785779398359, 'mean_word_length': 2.773860705073087, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, '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': '759264', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1474, 'n_words': 384}