# Path: pyipmi/fru.py
# class FruData(object):
#     def __init__(self, data=None):
#         if data:
#             if isinstance(data, str):
#                 data = [ord(c) for c in data]
#             self.data = data
#             if hasattr(self, '_from_data'):
#                 self._from_data(data)
# 
# class FruPicmgPowerModuleCapabilityRecord(FruPicmgRecord):
#     def _from_data(self, data):
#         if len(data) < 12:
#             raise DecodingError('data too short')
#         FruPicmgRecord._from_data(self, data)
#         maximum_current_output = data[10] | data[11] << 8
#         self.maximum_current_output = float(maximum_current_output/10)
# 
# class InventoryCommonHeader(FruData):
#     def _from_data(self, data):
#         if len(data) != 8:
#             raise DecodingError('InventoryCommonHeader length != 8')
#         self.format_version = data[0] & 0x0f
#         self.internal_use_area_offset = data[1] * 8 or None
#         self.chassis_info_area_offset = data[2] * 8 or None
#         self.board_info_area_offset = data[3] * 8 or None
#         self.product_info_area_offset = data[4] * 8 or None
#         self.multirecord_area_offset = data[5] * 8 or None
#         if sum(data) % 256 != 0:
#             raise DecodingError('InventoryCommonHeader checksum failed')
# 
# def get_fru_inventory_from_file(filename):
#     try:
#         file = open(filename, "rb")
#     except IOError:
#         print('Error open file "%s"' % filename)
# 
#     ################################
#     # get file size
#     file_size = os.stat(filename).st_size
#     file_data = file.read(file_size)
#     data = array.array('B', file_data)
#     file.close()
#     return FruInventory(data)


import os
import nose
from nose.tools import eq_, ok_
from pyipmi.fru import (FruData, FruPicmgPowerModuleCapabilityRecord,
                        InventoryCommonHeader, get_fru_inventory_from_file)
    if not os.path.isfile(fru_file):
        raise nose.SkipTest("FRU file '%s' is missing." % (fru_file))
    fru = get_fru_inventory_from_file(fru_file)

    board_area = fru.board_info_area
    eq_(board_area.manufacturer.value, 'Kontron')
    eq_(board_area.product_name.value, 'AM4010')
    eq_(board_area.serial_number.value, '0023721003')
    eq_(board_area.part_number.value, '35943')


def test_product_area():
    path = os.path.dirname(os.path.abspath(__file__))
    fru_file = os.path.join(path, 'fru_bin/kontron_am4010.bin')
    if not os.path.isfile(fru_file):
        raise nose.SkipTest("FRU file '%s' is missing." % (fru_file))
    fru = get_fru_inventory_from_file(fru_file)

    product_area = fru.product_info_area
    eq_(product_area.manufacturer.value, 'Kontron')
    eq_(product_area.name.value, 'AM4010')
    eq_(product_area.serial_number.value, '0000000000000000000000000')
    eq_(product_area.part_number.value, '0012')


def test_multirecord_with_power_module_capability_record():
    path = os.path.dirname(os.path.abspath(__file__))
    fru_file = os.path.join(path, 'fru_bin/vadatech_utc017.bin')
    if not os.path.isfile(fru_file):
        raise nose.SkipTest("FRU file '%s' is missing." % (fru_file))

# Path: pyipmi/fru.py
# class FruData(object):
#     def __init__(self, data=None):
#         if data:
#             if isinstance(data, str):
#                 data = [ord(c) for c in data]
#             self.data = data
#             if hasattr(self, '_from_data'):
#                 self._from_data(data)
# 
# class FruPicmgPowerModuleCapabilityRecord(FruPicmgRecord):
#     def _from_data(self, data):
#         if len(data) < 12:
#             raise DecodingError('data too short')
#         FruPicmgRecord._from_data(self, data)
#         maximum_current_output = data[10] | data[11] << 8
#         self.maximum_current_output = float(maximum_current_output/10)
# 
# class InventoryCommonHeader(FruData):
#     def _from_data(self, data):
#         if len(data) != 8:
#             raise DecodingError('InventoryCommonHeader length != 8')
#         self.format_version = data[0] & 0x0f
#         self.internal_use_area_offset = data[1] * 8 or None
#         self.chassis_info_area_offset = data[2] * 8 or None
#         self.board_info_area_offset = data[3] * 8 or None
#         self.product_info_area_offset = data[4] * 8 or None
#         self.multirecord_area_offset = data[5] * 8 or None
#         if sum(data) % 256 != 0:
#             raise DecodingError('InventoryCommonHeader checksum failed')
# 
# def get_fru_inventory_from_file(filename):
#     try:
#         file = open(filename, "rb")
#     except IOError:
#         print('Error open file "%s"' % filename)
# 
#     ################################
#     # get file size
#     file_size = os.stat(filename).st_size
#     file_data = file.read(file_size)
#     data = array.array('B', file_data)
#     file.close()
#     return FruInventory(data)


# Path: tests/test_fru.py
import os
import nose
from nose.tools import eq_, ok_
from pyipmi.fru import (FruData, FruPicmgPowerModuleCapabilityRecord,
                        InventoryCommonHeader, get_fru_inventory_from_file)

    if not os.path.isfile(fru_file):
        raise nose.SkipTest("FRU file '%s' is missing." % (fru_file))
    fru = get_fru_inventory_from_file(fru_file)

    board_area = fru.board_info_area
    eq_(board_area.manufacturer.value, 'Kontron')
    eq_(board_area.product_name.value, 'AM4010')
    eq_(board_area.serial_number.value, '0023721003')
    eq_(board_area.part_number.value, '35943')


def test_product_area():
    path = os.path.dirname(os.path.abspath(__file__))
    fru_file = os.path.join(path, 'fru_bin/kontron_am4010.bin')
    if not os.path.isfile(fru_file):
        raise nose.SkipTest("FRU file '%s' is missing." % (fru_file))
    fru = get_fru_inventory_from_file(fru_file)

    product_area = fru.product_info_area
    eq_(product_area.manufacturer.value, 'Kontron')
    eq_(product_area.name.value, 'AM4010')
    eq_(product_area.serial_number.value, '0000000000000000000000000')
    eq_(product_area.part_number.value, '0012')


def test_multirecord_with_power_module_capability_record():
    path = os.path.dirname(os.path.abspath(__file__))
    fru_file = os.path.join(path, 'fru_bin/vadatech_utc017.bin')
    if not os.path.isfile(fru_file):
        raise nose.SkipTest("FRU file '%s' is missing." % (fru_file))

    fru = get_fru_inventory_from_file(fru_file)