openpyxlを使ってみた②

こちらは本体。入力データと出力データそれぞれ、選ばれた時用と選ばれてない時用の2種類、計4種類(2×2)を準備する。

その後、1つのテストの設定にする。

#test.py

import openpyxl
from common_function import get_position_from_ws
from common_function import get_port_data_list
from common_function import peer_to_peer

hsize = 320 
vsize = 240 

base_port_row    =0  
base_port_column =0

in_select_list     = []
in_no_select_list  = []
in_color_list      = []
out_select_list    = []
out_no_select_list = []
out_color_list     = []

wb = openpyxl.load_workbook(filename = 'sample_1.xlsx',data_only=True)
ws = wb.active

base_port_row,base_port_column     = get_position_from_ws(ws,target_chr="port")

#                     [port name,sel_number,color,file name,  bit width,reserve,hsize,vsize]
# in_select_list    : [port_name,sel_number,1,    file_name,  bit_width,1,      hsize,vsize]
# in_no_select_list : [port_name,sel_number,0,    "empty.bin",bit_width,1,      0,    0    ]
in_select_list, in_no_select_list  = get_port_data_list("in", ws,base_port_row,base_port_column,hsize,vsize)

#                      [port name,reg name,color,file name,bit width,reserve,hsize,vsize]
# out_select_list    : [port_name,reg_name,1,    file_name,bit_width,1,      hsize,vsize]
# out_no_select_list : [port_name,reg_name,0,    file_name,bit_width,1,      0,    0    ]
out_select_list, out_no_select_list = get_port_data_list("out",ws,base_port_row,base_port_column,hsize,vsize)

#peer to peer
data_testlist,color_testlist = peer_to_peer(ws,base_port_row,base_port_column,in_no_select_list,in_select_list,out_no_select_list,out_select_list)
print (data_testlist)
print (color_testlist)
#common_function.py

def to_bit_width(char_bit_width_raw):
    data = char_bit_width_raw.replace('[','').replace(']','')
    f_pos = data.find(':')
    return int(data[:f_pos])+1

def get_position_from_ws(ws,target_chr):
    for test in ws: 
        for data in test:
            if data.value == str(target_chr) :
               return data.row, data.column

def get_port_data_list(port_is,ws,base_port_row,base_port_column,hsize,vsize):
    select_list    = []
    no_select_list = []
    if (port_is == "in"):
        for i in range(base_port_row+1,ws.max_row+1):
            sel_number =              ws.cell(i,base_port_column-2).value
            bit_width  = to_bit_width(ws.cell(i,base_port_column-1).value)
            port_name  =              ws.cell(i,base_port_column-0).value
            file_name  = port_name + ".bin"
                         #port name,sel_number,color,file name,  bit width,reserve,hsize,vsize
            select     = [port_name,sel_number,1,    file_name,  bit_width,1,      hsize,vsize]
            no_select  = [port_name,sel_number,0,    "empty.bin",bit_width,1,      0,    0    ]   
            select_list.append    (select)
            no_select_list.append (no_select)

    if (port_is == "out"):
        for i in range(base_port_column+1,ws.max_column+1):
            reg_name  =              ws.cell(base_port_row-2,i).value
            bit_width = to_bit_width(ws.cell(base_port_row-1,i).value)
            port_name =              ws.cell(base_port_row-0,i).value
            file_name = port_name + ".bin"
                        #port name,reg name,color,file name,bit width,reserve,hsize,vsize
            select    = [port_name,reg_name,1,    file_name,bit_width,1,      hsize,vsize]
            no_select = [port_name,reg_name,0,    file_name,bit_width,1,      0,    0    ]   
            select_list.append    (select)
            no_select_list.append (no_select)
    return select_list, no_select_list
def peer_to_peer(ws,base_port_row, base_port_column, in_no_select_list, in_select_list, out_no_select_list, out_select_list):
    data_testlist  = []
    color_testlist = []
    test_num = 0
    in_num = 0
    for i in range(base_port_row+1,ws.max_row):
        out_num = 0
        temp_in_list          = in_no_select_list.copy()
        temp_in_list[in_num]  = in_select_list[in_num]
        in_color_list         = len(temp_in_list) * [len(temp_in_list[0])* [0]]
        in_color_list[in_num] = [0,0,0,1,1]
        sel_number            = temp_in_list[in_num][1]

        for j in range(base_port_column+1,ws.max_column):
            if ws.cell(i,j).value is not None:
                temp_out_list           = out_no_select_list.copy()
                temp_out_list[out_num]  = out_select_list[out_num]
                out_color_list          = len(temp_out_list) * [len(temp_out_list[0])* [0]]
                out_color_list[out_num] = [0,0,0,1,1]
                temp_reg_list           = len(temp_out_list) * [0]
                temp_reg_list[out_num]  = sel_number
                reg_color_list          = len(temp_reg_list) * [0]
                reg_color_list[out_num] = 1

                in_name_for_testlist  = [data[0] for data in temp_in_list ]
                out_name_for_testlist = [data[0] for data in temp_out_list]
                reg_name_for_testlist = [data[1] for data in temp_out_list]
                in_data_for_testlist  = [data for test in temp_in_list  for data in test[3:8]]
                out_data_for_testlist = [data for test in temp_out_list for data in test[3:8]]
                reg_data_for_testlist = temp_reg_list
                data_testlist.append(in_data_for_testlist + out_data_for_testlist + [""] + reg_data_for_testlist)

                in_color_for_testlist  = [x for color in in_color_list  for x in color]
                out_color_for_testlist = [x for color in out_color_list for x in color]
                reg_color_for_testlist = reg_color_list
                color_testlist.append(in_color_for_testlist + out_color_for_testlist + [""] + reg_color_for_testlist)
                test_num += 1
            out_num += 1 
    return data_testlist, color_testlist