<commit_before>import collectd

from base64 import b64encode
from httplib import HTTPConnection
from xml.etree import ElementTree

TED5000_HOST = "ted5000"
TED5000_USERNAME = "admin"
TED5000_PASSWORD = "<PASSWORD>"
VERBOSE = False
SKIP_ZEROES = True


def configure_callback(conf):
    global TED5000_HOST, TED5000_USERNAME, TED5000_PASSWORD, VERBOSE, SKIP_ZEROES
    for node in conf.children:
        if node.key == 'Host':
            TED5000_HOST = node.values[0]
        elif node.key == 'User':
            TED5000_USERNAME = node.values[0]
        elif node.key == 'Password':
            TED5000_PASSWORD = <PASSWORD>.<PASSWORD>[0]
        elif node.key == 'Verbose':
            VERBOSE = bool(node.values[0])
        elif node.key == 'SkipZeroes':
            SKIP_ZEROES = bool(node.values[0])
        else:
            collectd.warning('ted5000 plugin: Unknown config key: %s.' % node.key)
    if VERBOSE:
        collectd.info(
            'ted5000 plugin: Configured with host=%s, user=%s' % (TED5000_HOST, TED5000_USERNAME))


def read_callback():
    user_and_pass = b64encode(b"%s:%s" % (TED5000_USERNAME, TED5000_PASSWORD)).decode("ascii")
    http = HTTPConnection(TED5000_HOST)
    headers = {'Authorization': 'Basic %s' % user_and_pass}

    try:
        http.request('GET', '/api/LiveData.xml', headers=headers)
        response = http.getresponse()
        xml = response.read()
    except Exception as e:
        collectd.error('ted5000 plugin: Could not talk to TED 5000: %s' % e.message)
        return

    root = ElementTree.fromstring(xml)

    mtus = int(root.find(".//*/NumberMTU").text)

    for mtu in range(1, mtus + 1):
        power = int(root.find(".//*/MTU%d/PowerNow" % mtu).text)
        voltage = int(root.find(".//*/MTU%d/VoltageNow" % mtu).text)

        val = collectd.Values(plugin='python.ted5000', type='gauge')
        if not SKIP_ZEROES or power != 0:
            val.type_instance = 'power.mtu%d' % mtu<commit_msg>Closing HTTP connections properly.<commit_after>import collectd

from base64 import b64encode
from httplib import HTTPConnection
from xml.etree import ElementTree

TED5000_HOST = "ted5000"
TED5000_USERNAME = "admin"
TED5000_PASSWORD = "<PASSWORD>"
VERBOSE = False
SKIP_ZEROES = True


def configure_callback(conf):
    global TED5000_HOST, TED5000_USERNAME, TED5000_PASSWORD, VERBOSE, SKIP_ZEROES
    for node in conf.children:
        if node.key == 'Host':
            TED5000_HOST = node.values[0]
        elif node.key == 'User':
            TED5000_USERNAME = node.values[0]
        elif node.key == 'Password':
            TED5000_PASSWORD = node.values[0]
        elif node.key == 'Verbose':
            VERBOSE = bool(node.values[0])
        elif node.key == 'SkipZeroes':
            SKIP_ZEROES = bool(node.values[0])
        else:
            collectd.warning('ted5000 plugin: Unknown config key: %s.' % node.key)
    if VERBOSE:
        collectd.info(
            'ted5000 plugin: Configured with host=%s, user=%s' % (TED5000_HOST, TED5000_USERNAME))


def read_callback():
    user_and_pass = b64encode(b"%s:%s" % (TED5000_USERNAME, TED5000_PASSWORD)).decode("ascii")
    http = HTTPConnection(TED5000_HOST)
    headers = {'Authorization': 'Basic %s' % user_and_pass}

    try:
        http.request('GET', '/api/LiveData.xml', headers=headers)
        response = http.getresponse()
        xml = response.read()
        http.close()
    except Exception as e:
        collectd.error('ted5000 plugin: Could not talk to TED 5000: %s' % e.message)
        return

    root = ElementTree.fromstring(xml)

    mtus = int(root.find(".//*/NumberMTU").text)

    for mtu in range(1, mtus + 1):
        power = int(root.find(".//*/MTU%d/PowerNow" % mtu).text)
        voltage = int(root.find(".//*/MTU%d/VoltageNow" % mtu).text)

        val = collectd.Values(plugin='python.ted5000', type='gauge')
        if not SKIP_ZEROES or power != 0:
            val.type_instance = 'power.mtu%d' % mtu
{'fraction_non_alphanumeric': 0.1297979797979798, 'fraction_numerical': 0.047474747474747475, 'mean_word_length': 2.841901066925315, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '5948619', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1318, 'n_words': 291}