<commit_before>#Copyright 2011 Calxeda, Inc.  All Rights Reserved.

"""This module contains a series of wrappers
   around SEL commands for ipmitool
"""

import re
import tempfile
import string
from pyipmi import Command
from pyipmi.tools.ipmitool import IpmitoolCommandMixIn, str2bool
from pyipmi.sel import SELTimestamp, SELInfo, SELRecord, SELTimestampError


class SELTimeSetCommand(Command, IpmitoolCommandMixIn):
    """Describes the Set SEL Time command"""
    
    name = "Set SEL Time"
    # TODO: get response data from ipmitool

    @property
    def ipmitool_args(self):
        """return args for ipmitool command"""
        return ["sel", "time", "set", self._params["time"].timestamp]

    def handle_command_error(self, resp, err):
        raise SELTimestampError(err)


class SELTimeGetCommand(Command, IpmitoolCommandMixIn):
    """Describes the Get SEL Time command"""
 
    def ipmitool_parse_response(self, resp, err):
        """A helper function to parse a timestamp returned from 
        an 'sel time get' command
        """

        return SELTimestamp(resp.strip())

    name = "Get SEL Time"
    result_type = SELTimestamp
    ipmitool_args = ["sel", "time", "get"]


class SELInfoCommand(Command, IpmitoolCommandMixIn):
    """Describes the Get SEL Info command"""

    def version_parser(string):
        vdict = {}
        vregex = '(?P<number>[12]\.[05]) \((?P<compliant>.+) compliant\)'
        match = re.match(vregex, string)
        vdict['number'] = match.group('number')
        vdict['compliant'] = match.group('compliant').split(', ')
        return vdict

    name = "SEL Info"

    ipmitool_args = ["sel", "info"]
    result_type = SELInfo

    ipmitool_response_fields = {
        'Version' : {'parser': version_parser},
        'Entries' : {'parser': int},
        'Free Space' : {},
        'Percent Used' : {},
        'Last Add Time' : {'parser': lambda ts: SELTimestamp(ts)},
        'Last Del Time' : {'parser': lambda ts: SELTimestamp(ts)},
        'Overflow' : {'parser': str2bool},
        'Supported Cmds' : {'parser': lambda s: re.findall("\w[ \w]+", s)},
        '# of Alloc Units' : {'attr': 'num_alloc_units', 'parser': int},
        'Alloc Unit Size' : {'parser': int},
        '# Free Units' : {'attr': 'num_free_units', 'parser': int},
        'Largest Free Blk' : {'parser': int},
        'Max Record Size' : {'parser': int}
    }


class SELAddCommand(Command, IpmitoolCommandMixIn):
    """Describes the sel add command"""
    
    name = "SEL Add"
    #TODO: get response data from ipmitool

    @property
    def ipmitool_args(self):
        """return args for ipmitool command"""
        tmpfile = sel_entries_to_tmpfile(*self._params['records'])
        # TODO: clean up tmpfile
        return ["sel", "add", tmpfile.name]

    def ipmitool_parse_response(self, response, err):
        print response, err


class SELGetCommand(Command, IpmitoolCommandMixIn):
    """Describes the sel get command"""

    def event_data_parser(string):
        data = int(string, 16)
        return (data >> 16, (data >> 8) & 0xff, data & 0xff)

    def ipmitool_parse_response(self, response, err):
        if err.find("command failed") > 0:
            return None

        entry = self.parse_colon_record(response, err)<commit_msg>sel add: fix error handling<commit_after>#Copyright 2011 Calxeda, Inc.  All Rights Reserved.

"""This module contains a series of wrappers
   around SEL commands for ipmitool
"""

import re
import tempfile
import string
from pyipmi import Command
from pyipmi.tools.ipmitool import IpmitoolCommandMixIn, str2bool
from pyipmi.sel import (SELTimestamp, SELInfo, SELRecord,
                        SELOverflowError, SELTimestampError)


class SELTimeSetCommand(Command, IpmitoolCommandMixIn):
    """Describes the Set SEL Time command"""
    
    name = "Set SEL Time"
    # TODO: get response data from ipmitool

    @property
    def ipmitool_args(self):
        """return args for ipmitool command"""
        return ["sel", "time", "set", self._params["time"].timestamp]

    def handle_command_error(self, resp, err):
        raise SELTimestampError(err)


class SELTimeGetCommand(Command, IpmitoolCommandMixIn):
    """Describes the Get SEL Time command"""
 
    def ipmitool_parse_response(self, resp, err):
        """A helper function to parse a timestamp returned from 
        an 'sel time get' command
        """

        return SELTimestamp(resp.strip())

    name = "Get SEL Time"
    result_type = SELTimestamp
    ipmitool_args = ["sel", "time", "get"]


class SELInfoCommand(Command, IpmitoolCommandMixIn):
    """Describes the Get SEL Info command"""

    def version_parser(string):
        vdict = {}
        vregex = '(?P<number>[12]\.[05]) \((?P<compliant>.+) compliant\)'
        match = re.match(vregex, string)
        vdict['number'] = match.group('number')
        vdict['compliant'] = match.group('compliant').split(', ')
        return vdict

    name = "SEL Info"

    ipmitool_args = ["sel", "info"]
    result_type = SELInfo

    ipmitool_response_fields = {
        'Version' : {'parser': version_parser},
        'Entries' : {'parser': int},
        'Free Space' : {},
        'Percent Used' : {},
        'Last Add Time' : {'parser': lambda ts: SELTimestamp(ts)},
        'Last Del Time' : {'parser': lambda ts: SELTimestamp(ts)},
        'Overflow' : {'parser': str2bool},
        'Supported Cmds' : {'parser': lambda s: re.findall("\w[ \w]+", s)},
        '# of Alloc Units' : {'attr': 'num_alloc_units', 'parser': int},
        'Alloc Unit Size' : {'parser': int},
        '# Free Units' : {'attr': 'num_free_units', 'parser': int},
        'Largest Free Blk' : {'parser': int},
        'Max Record Size' : {'parser': int}
    }


class SELAddCommand(Command, IpmitoolCommandMixIn):
    """Describes the sel add command"""
    
    name = "SEL Add"
    #TODO: get response data from ipmitool

    @property
    def ipmitool_args(self):
        """return args for ipmitool command"""
        tmpfile = sel_entries_to_tmpfile(*self._params['records'])
        # TODO: clean up tmpfile
        return ["sel", "add", tmpfile.name]

    def handle_command_error(self, resp, err):
        if err.find('Out of space') > 0:
            raise SELOverflowError(err)

        raise IpmiError(err)


class SELGetCommand(Command, IpmitoolCommandMixIn):
    """Describes the sel get command"""

    def event_data_parser(string):
        data = int(string, 16)
        return (data >> 16, (data >> 8) & 0xff, data & 0xff)

    def ipmitool_parse_response(self, response, err):
        if err.find("command failed") > 0:
            return None

        entry = self.parse_colon_record(response, err)
{'fraction_non_alphanumeric': 0.13275656324582338, 'fraction_numerical': 0.00551909307875895, 'mean_word_length': 3.027027027027027, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, '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': '202858', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2047, 'n_words': 592}