Repository navigation
Expand file tree
/
Copy pathexcel_loader.py
More file actions
executable file
·75 lines (64 loc) · 2.65 KB
/
Copy pathexcel_loader.py
File metadata and controls
executable file
·75 lines (64 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import atexit
import os
from pathlib import Path
import pandas as pd
import xlwings as xw
from .models import ChannelConfigInfo
class ExcelConfigRepository:
def __init__(self, base_dir=None, workbook_name="SMC_TZConfig.xlsx"):
self.base_dir = Path(base_dir) if base_dir else Path(__file__).resolve().parent.parent
self.file_path = self.base_dir / workbook_name
self.app = xw.App(visible=False)
self._wb_chconfig = xw.Book(self.file_path)
self._df_chconfig = self._load_chconfig_df()
atexit.register(self.close)
def close(self):
try:
if self.app is not None:
print("Exiting Python program - Excel app will be closed.")
self.app.quit()
self.app = None
except Exception:
self.app = None
def _load_chconfig_df(self):
ws_chconfig = self._wb_chconfig.sheets["CHConfig"]
rng_chconfig = ws_chconfig.range("A1").expand().value
df_chconfig = pd.DataFrame(rng_chconfig[0:])
df_chconfig = df_chconfig.fillna("0x0")
df_chconfig = df_chconfig.T
df_chconfig.columns = df_chconfig.iloc[0]
df_chconfig = df_chconfig[1:]
return df_chconfig
def get_project_list(self):
return self._df_chconfig["Project_Code"].tolist()
def make_project_df(self, project_name):
wb = xw.Book(self.file_path)
ws = wb.sheets[project_name]
rng = ws.range("A1").expand().value
df = pd.DataFrame(rng[0:])
df.set_index(df.columns[0], inplace=True)
df = df.fillna("0x0")
return df
def get_channel_config_info(self, project_code):
row = self._df_chconfig[self._df_chconfig["Project_Code"] == project_code]
def get_value(column_name, default="0x0"):
if column_name not in row.columns:
return default
value = row[column_name].iloc[0]
if value in (None, "", " "):
return default
return value
return ChannelConfigInfo(
Project_Code=row["Project_Code"].iloc[0],
Project_Name=row["Project_Name"].iloc[0],
CHASYM=int(row["CHASYM"].iloc[0]),
CH_num=int(row["CH_num"].iloc[0]),
CH_Bit2Hash=get_value("CH_Bit2Hash"),
CH_Bit1Hash=get_value("CH_Bit1Hash"),
CH_Bit0Hash=get_value("CH_Bit0Hash"),
SubChHashBitEn=get_value("SubChHashBitEn"),
Bank3HashBitEn=get_value("Bank3HashBitEn"),
Bank2HashBitEn=get_value("Bank2HashBitEn"),
Bank1HashBitEn=get_value("Bank1HashBitEn"),
Bank0HashBitEn=get_value("Bank0HashBitEn"),
)