Skip to content

Commit

Permalink
Merge pull request #29 from zhangyiant/zhangyiant/fix
Browse files Browse the repository at this point in the history
Fixed the empty field validation function.
  • Loading branch information
zhangyiant authored Apr 16, 2022
2 parents 079815f + f76fa70 commit 32a51c0
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 13 deletions.
2 changes: 1 addition & 1 deletion register_printer/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.12
1.1.13
24 changes: 12 additions & 12 deletions register_printer/parser/register_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ def is_field_row(row):
return False


def validate_register_row_empty_field(row, previous_context):
def validate_register_row_empty_field(row, register_table_column_mapping, previous_context):
"""
row can be obtained by xlrd sheet.row() method.
It's a sequence of cell objects.
"""
context = previous_context.copy()
field_map = [
(2, "msb"),
(3, "lsb"),
(4, "field"),
(5, "access"),
(6, "default")
fields = [
"msb",
"lsb",
"field name",
"access",
"default"
]
for (col, field_name) in field_map:
context.column = col
if row[col].value != "":
msg = "Field '%s' must be emtpy." % field_name
for field in fields:
context.column = register_table_column_mapping[field]
if row[context.column].value != "":
msg = "Field '%s' must be emtpy." % field
raise ExcelParseException(msg, context)
return

Expand All @@ -49,7 +49,7 @@ def parse_register_row(row, register_table_column_mapping, previous_context):
a sequence of cells.
"""
context = previous_context.copy()
validate_register_row_empty_field(row, context)
validate_register_row_empty_field(row, register_table_column_mapping, context)

context.column = register_table_column_mapping["offset"]
try:
Expand Down
Binary file added register_printer/tests/dataset/dataset3/abc.xlsx
Binary file not shown.
Binary file not shown.
255 changes: 255 additions & 0 deletions register_printer/tests/dataset/dataset3/output/register_printer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
{
"name": "Top_Module",
"addressWidth": 12,
"dataWidth": 32,
"version": "2.1.234",
"author": "Yi Zhang",
"blockInstances": [
{
"blockType": "Type2",
"name": "Instance2",
"baseAddress": 65536,
"blockSize": 16,
"addressWidth": 24,
"dataWidth": 64
},
{
"blockType": "Type1",
"name": "Instance3",
"baseAddress": 131072,
"blockSize": 65536,
"addressWidth": null,
"dataWidth": null
}
],
"blockTemplates": [
{
"blockType": "Type2",
"arrayTemplates": [],
"registerTemplates": [
{
"name": "reg1",
"offset": 0,
"description": "register 1",
"fieldTemplates": [
{
"name": "Field1",
"msb": 7,
"lsb": 1,
"defaultValue": 1,
"access": "RW",
"description": "Register1-Field1"
},
{
"name": "Field2",
"msb": 10,
"lsb": 8,
"defaultValue": 1,
"access": "RW",
"description": "Register1-Field2"
}
]
},
{
"name": "reg2",
"offset": 8,
"description": "register 2",
"fieldTemplates": [
{
"name": "Field2",
"msb": 7,
"lsb": 1,
"defaultValue": 1,
"access": "RW",
"description": "Register2-Field1"
},
{
"name": "Field3",
"msb": 12,
"lsb": 9,
"defaultValue": 1,
"access": "RW",
"description": "Register2-Field3"
}
]
}
]
},
{
"blockType": "Type1",
"arrayTemplates": [
{
"name": "reg_array1",
"length": 16,
"offset": 4,
"startAddress": 16,
"endAddress": 16,
"description": ""
},
{
"name": "reg_array2",
"length": 4,
"offset": 32,
"startAddress": 256,
"endAddress": 284,
"description": ""
}
],
"registerTemplates": [
{
"name": "reg1",
"offset": 0,
"description": "register 1",
"fieldTemplates": [
{
"name": "Field1",
"msb": 7,
"lsb": 1,
"defaultValue": 1,
"access": "RW",
"description": "Register1-Field1"
},
{
"name": "Field2",
"msb": 10,
"lsb": 8,
"defaultValue": 1,
"access": "RW",
"description": "Register1-Field2"
}
]
},
{
"name": "reg2",
"offset": 4,
"description": "register 2",
"fieldTemplates": [
{
"name": "Field2",
"msb": 7,
"lsb": 1,
"defaultValue": 1,
"access": "RW",
"description": "Register2-Field1"
},
{
"name": "Field3",
"msb": 12,
"lsb": 9,
"defaultValue": 1,
"access": "RW",
"description": "Register2-Field3"
}
]
},
{
"name": "reg_a1",
"offset": 16,
"description": "register aa",
"fieldTemplates": [
{
"name": "Field4",
"msb": 3,
"lsb": 0,
"defaultValue": 1,
"access": "RO",
"description": "registeraa_field4"
},
{
"name": "Field5",
"msb": 14,
"lsb": 4,
"defaultValue": 2,
"access": "RW",
"description": "registeraa_field5"
}
]
},
{
"name": "reg_a2",
"offset": 256,
"description": "register a2",
"fieldTemplates": [
{
"name": "Field6",
"msb": 10,
"lsb": 0,
"defaultValue": 4,
"access": "RW",
"description": "registera2_field6"
},
{
"name": "Field7",
"msb": 31,
"lsb": 11,
"defaultValue": 4,
"access": "RW",
"description": "registera2_field7"
}
]
},
{
"name": "reg_a3",
"offset": 280,
"description": "register a3",
"fieldTemplates": [
{
"name": "Field8",
"msb": 11,
"lsb": 0,
"defaultValue": 5,
"access": "RW",
"description": "registera3_field8"
},
{
"name": "Field9",
"msb": 15,
"lsb": 12,
"defaultValue": 7,
"access": "RW",
"description": "registera3_field9"
}
]
},
{
"name": "reg_a2",
"offset": 288,
"description": "register a2",
"fieldTemplates": [
{
"name": "Field6",
"msb": 10,
"lsb": 0,
"defaultValue": 5,
"access": "RW",
"description": "registera2_field6"
},
{
"name": "Field7",
"msb": 31,
"lsb": 11,
"defaultValue": 6,
"access": "RW",
"description": "registera2_field7"
}
]
},
{
"name": "regtt",
"offset": 416,
"description": "",
"fieldTemplates": [
{
"name": "Field9",
"msb": 10,
"lsb": 0,
"defaultValue": 5,
"access": "RW",
"description": "registera2_field6"
}
]
}
]
}
]
}
54 changes: 54 additions & 0 deletions register_printer/tests/test_register_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,57 @@ def test_json_input_and_output(self):
"JSON file content is not correct"
)
return


class TestRegisterPrinterWithExtraField(TestCase):

DATASET_PATH = os.path.join(DATASET_ROOT_PATH, "dataset3")

def setUp(self):
self.config_file = os.path.join(
TestRegisterPrinterWithExtraField.DATASET_PATH,
"abc.xlsx"
)
self.excel_path = os.path.join(
TestRegisterPrinterWithExtraField.DATASET_PATH,
"excels"
)
return

def test_extra_field_in_registers(self):
with TemporaryDirectory() as tmp_dir:
output_path = tmp_dir
register_printer = RegisterPrinter(
config_file=self.config_file,
excel_path=self.excel_path,
output_path=output_path
)
register_printer.generate_json()

json_file_path = tmp_dir
baseline_json_file_path = os.path.join(
TestRegisterPrinterWithExtraField.DATASET_PATH,
"output"
)

json_filename = os.path.join(
json_file_path,
"register_printer.json"
)
baseline_json_filename = os.path.join(
baseline_json_file_path,
"register_printer.json"
)
self.assertTrue(
os.path.exists(json_filename),
"JSON file is not generated."
)
self.assertTrue(
filecmp.cmp(
json_filename,
baseline_json_filename
),
"JSON file content is not correct"
)

return

0 comments on commit 32a51c0

Please sign in to comment.