class ShadowRegsXlsToXml(): "Class to convert XLSX to XML with shadow register description" def __init__(self, xls_file: str, xml_file: str = "", xls_type: int=1) -> None: self.registers = Registers("Unknown") self.xls_type = xls_type self.header_cells = {} self.xml_file_name = xml_file if xml_file != "" else xls_file.replace(".xlsx", ".xml") self.wb = None print(os.path.dirname(os.path.realpath(__file__))) self.wb = openpyxl.load_workbook(xls_file) print(f"Loaded XLSX file ({xls_file})") self.convert() self.registers.write_xml(self.xml_file_name) print(f"Written XML file ({self.xml_file_name})") print(str(self.registers)) def convert(self) -> None: raise NotImplementedError def _get_worksheet(self) -> Any: """Find the valid worksheet with the fuse map.""" raise NotImplementedError def _get_header(self) -> None: """Returns the dictionary with cells of header.""" raise NotImplementedError def _get_registers(self) -> None: """Function finds all registers in XLS sheet and store them.""" raise NotImplementedError def __del__(self) -> None: """Just close all open files.""" if self.wb: self.wb.close()
def test_basic_regs(tmpdir): """Basic test of registers class.""" regs = Registers(TEST_DEVICE_NAME) assert regs.dev_name == TEST_DEVICE_NAME reg1 = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) with pytest.raises(SPSDKRegsErrorRegisterNotFound): regs.find_reg("NonExisting") # The Registers MUST return empty array assert regs.get_reg_names() == [] with pytest.raises(SPSDKError): regs.remove_register("String") with pytest.raises(ValueError): regs.remove_register(reg1) # Now we could do tests with a register added to list regs.add_register(reg1) regs.remove_register_by_name(["String"]) assert TEST_REG_NAME in regs.get_reg_names() regt = regs.find_reg(TEST_REG_NAME) assert regt == reg1 with pytest.raises(SPSDKError): regs.add_register("Invalid Parameter") regt.set_value(TEST_REG_VALUE) assert reg1.get_value() == TEST_REG_VALUE filename = os.path.join(tmpdir, TEST_XML_FILE) regs.write_xml(filename) assert os.path.isfile(filename) printed_str = str(regs) assert TEST_DEVICE_NAME in printed_str assert TEST_REG_NAME in printed_str regs.remove_register_by_name([TEST_REG_NAME]) with pytest.raises(SPSDKRegsErrorRegisterNotFound): regs.find_reg(TEST_REG_NAME) assert False
def test_registers_xml(data_dir, tmpdir): regs = Registers(TEST_DEVICE_NAME) with use_working_directory(data_dir): regs.load_registers_from_xml("registers.xml") with use_working_directory(tmpdir): regs.write_xml("registers.xml") regs2 = Registers(TEST_DEVICE_NAME) with use_working_directory(tmpdir): regs2.load_registers_from_xml("registers.xml") assert str(regs) == str(regs2)
def test_registers_xml_hidden(data_dir, tmpdir): """Test registers XML support.""" regs = Registers(TEST_DEVICE_NAME) with use_working_directory(data_dir): regs.load_registers_from_xml("registers_reserved.xml") assert len(regs.get_registers()[0].get_bitfields()) == 1 assert regs.get_registers()[0].get_bitfields()[0].get_value() == 0xA assert regs.get_registers()[0].get_value() == 0x550A00 with use_working_directory(tmpdir): regs.write_xml("registers_reserved.xml") regs2 = Registers(TEST_DEVICE_NAME) with use_working_directory(tmpdir): regs2.load_registers_from_xml("registers_reserved.xml") assert str(regs) == str(regs2)
def test_registers_corrupted_xml(data_dir, tmpdir): regs = Registers(TEST_DEVICE_NAME) with use_working_directory(data_dir): regs.load_registers_from_xml("registers_corr.xml") with use_working_directory(tmpdir): regs.write_xml("registers_corr.xml") assert not filecmp.cmp(os.path.join(data_dir, "registers_corr.xml"), os.path.join(tmpdir, "registers_corr.xml")) regs.clear() with use_working_directory(tmpdir): regs.load_registers_from_xml("registers_corr.xml") regs.write_xml("registers_corr1.xml") assert filecmp.cmp(os.path.join(tmpdir, "registers_corr.xml"), os.path.join(tmpdir, "registers_corr1.xml")) # Without clear - Cannot add register with same name as is already added with use_working_directory(tmpdir): regs.load_registers_from_xml("registers_corr.xml") regs.write_xml("registers_corr1.xml") assert filecmp.cmp(os.path.join(tmpdir, "registers_corr.xml"), os.path.join(tmpdir, "registers_corr1.xml"))