Update .gitignore, DataClass.py, and 19 more files...
오랜만에 서버 정리하고 커밋. 파일 위치를 정리했다. 캘리버 DB 를 열고 정보를 열람. Pupil 을 통해 다운받은 정보를 관리하기 위해 새로운 클래스 추가
This commit is contained in:
249
Util_XLS.py
Normal file
249
Util_XLS.py
Normal file
@@ -0,0 +1,249 @@
|
||||
import os
|
||||
import openpyxl as opyxl
|
||||
|
||||
import DataClass as stManga
|
||||
import UtilPack as util
|
||||
|
||||
class DBXLStorage:
|
||||
xls_name = "mangaDB.xlsx"
|
||||
xls_path = ""
|
||||
m_wb = None
|
||||
|
||||
sheetName_Mangainfo = "MangaInfo"
|
||||
sheetName_Artists = "Artists"
|
||||
sheetName_Tags = "Tags"
|
||||
|
||||
strMngSht = "MngInfo"
|
||||
strArtSht = "ArtInfo"
|
||||
strTagSht = "TagInfo"
|
||||
strSeriesSht = "SeriesInfo"
|
||||
strTypeSht = "TypeInfo"
|
||||
|
||||
m_openedXLS = ""
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
|
||||
def __enter__(self):
|
||||
self.DBXLSOpen(self.path)
|
||||
|
||||
def __exit__(self, ex_type, ex_value, traceback):
|
||||
self.DBXLSClose()
|
||||
|
||||
def DBXLSOpen(self):
|
||||
print(self.path)
|
||||
xls_path = self.GetXLSPath(self.path)
|
||||
util.DbgOut(xls_path)
|
||||
|
||||
try:
|
||||
self.m_wb = opyxl.load_workbook(xls_path)
|
||||
util.DbgOut("xls Open Successed")
|
||||
except FileNotFoundError:
|
||||
self.m_wb = opyxl.Workbook()
|
||||
|
||||
ws = self.m_wb.active
|
||||
ws.title = "DBInfo"
|
||||
ws.cell(row=1,column=1,value="MangaDB_XLS")
|
||||
|
||||
self.m_wb.create_sheet(self.strMngSht)
|
||||
self.m_wb.create_sheet(self.strArtSht)
|
||||
self.m_wb.create_sheet(self.strTagSht)
|
||||
self.m_wb.create_sheet(self.strSeriesSht)
|
||||
self.m_wb.create_sheet(self.strTypeSht)
|
||||
|
||||
self.m_wb.save(xls_path)
|
||||
util.DbgOut(f"{xls_path} Created", True)
|
||||
|
||||
self.m_openedXLS = xls_path
|
||||
|
||||
if self.m_wb is None:
|
||||
util.DbgOut("XLS Open Something Wrong...", True)
|
||||
self.m_openedXLS = ""
|
||||
self.m_wb = None
|
||||
return
|
||||
|
||||
def DBXLSClose(self):
|
||||
if self.m_wb is None or self.m_openedXLS is None:
|
||||
util.DbgOut("XLS Close something wrong...", True)
|
||||
return
|
||||
|
||||
self.m_wb.save(self.m_openedXLS)
|
||||
self.m_wb.close()
|
||||
util.DbgOut(f"Close : {self.m_openedXLS} Saved")
|
||||
|
||||
self.m_wb = None
|
||||
|
||||
#
|
||||
def WriteMangaInfos(self, *listInfos):
|
||||
if False == isinstance(listInfos, list):
|
||||
return
|
||||
|
||||
ws_mng = self.getSheet(self.strMngSht)
|
||||
if None == ws_mng:
|
||||
return
|
||||
|
||||
#for item in listInfos:
|
||||
# 클래스 타잎을 확인해야 하지만.. 생략.
|
||||
# ttist, group, series(parady), type, tags, hitomi ID, hitomi file, eh ID, eh tor
|
||||
|
||||
|
||||
# TAG 정보를 TAG 시트에 추가한다.
|
||||
# 태그는 분류:단어, 이런 식이다.
|
||||
# 분류에는 artist, tag, male, female, other 등이 있다.
|
||||
def AddTagInfo(self, strTag, strUrl):
|
||||
nID = self.AddInfotoSheet(self.strTagSht, strTag, strUrl)
|
||||
util.DbgOut(f"AddTagInfo : {self.strTagSht}:{nID},{strTag}")
|
||||
return nID
|
||||
|
||||
def AddArtistInfo(self, ArtName, strUrl):
|
||||
shtArt = self.getSheet(self.strArtSht)
|
||||
if shtArt is None:
|
||||
util.DbgOut(f"AddArtist : {self.strArtSht} Get Failed...", True)
|
||||
return
|
||||
|
||||
# ArtName 이 리스트인 경우, String 인 경우로 구분한다. 그 외는 몰라.
|
||||
# String 인 경우
|
||||
if isinstance(ArtName, str):
|
||||
nID = self.SearchIDFromName(shtArt, ArtName)
|
||||
if 0 > nID:
|
||||
util.DbgOut(f"AddArtist : {self.sheetArtist} Get Failed...", True)
|
||||
elif 0 < nID:
|
||||
util.DbgOut(f"AddArtist : {self.sheetArtist}:{nID},{ArtName} is exist.")
|
||||
else:
|
||||
last_row = shtArt.max_row
|
||||
if shtArt.cell(row=last_row, column=1).value :
|
||||
last_row += 1
|
||||
|
||||
shtArt.cell(row=last_row, column=1, value=last_row)
|
||||
shtArt.cell(row=last_row, column=2, value=strUrl)
|
||||
shtArt.cell(row=last_row, column=3, value=ArtName)
|
||||
# list 인 경우
|
||||
elif isinstance(ArtName, list):
|
||||
nID = self.SearchIDFromName(shtArt, ArtName)
|
||||
if 0 > nID:
|
||||
util.DbgOut(f"AddArtist : {self.sheetArtist} Get Failed...", True)
|
||||
elif 0 < nID:
|
||||
util.DbgOut(f"AddArtist : {self.sheetArtist}:{nID},{ArtName[0]} is exist.")
|
||||
else:
|
||||
last_row = shtArt.max_row
|
||||
if shtArt.cell(row=last_row, column=1).value :
|
||||
last_row += 1
|
||||
|
||||
shtArt.cell(row=last_row, column=1, value=last_row)
|
||||
shtArt.cell(row=last_row, column=2, value=strUrl)
|
||||
|
||||
nCol = 3
|
||||
for strArt in ArtName:
|
||||
shtArt.cell(row=last_row, column=nCol, value=strArt)
|
||||
nCol += 1
|
||||
|
||||
nID = last_row
|
||||
|
||||
return nID
|
||||
|
||||
|
||||
def AddSeriesInfo(self, strSerires, strUrl):
|
||||
nID = self.AddInfotoSheet(self.strSeriesSht, strSerires, strUrl)
|
||||
util.DbgOut(f"AddSeriesInfo : {self.strSeriesSht}:{nID},{strSerires}")
|
||||
return nID
|
||||
|
||||
|
||||
def AddTypeInfo(self, strType, strUrl):
|
||||
nID = self.AddInfotoSheet(self.strTypeSht, strType, strUrl)
|
||||
util.DbgOut(f"AddTypeInfo : {self.strTypeSht}:{nID},{strType}")
|
||||
return nID
|
||||
|
||||
|
||||
# 1 , 2 , 3
|
||||
# Index, URL , Value
|
||||
def AddInfotoSheet(self, strSheet, strValue, strUrl):
|
||||
sheetTag = self.getSheet(strSheet)
|
||||
if sheetTag is None:
|
||||
util.DbgOut(f"AddTag : {strSheet} Get Failed...", True)
|
||||
return -1
|
||||
|
||||
nIndexID = self.SearchIDFromName(sheetTag, strValue)
|
||||
if 0 > nIndexID:
|
||||
util.DbgOut(f"AddTag : {strSheet} Get Failed...", True)
|
||||
elif 0 < nIndexID:
|
||||
util.DbgOut(f"AddTag : {strSheet}:{nIndexID},{strValue} is exist.")
|
||||
else:
|
||||
last_row = sheetTag.max_row
|
||||
if sheetTag.cell(row=last_row, column=1).value :
|
||||
last_row += 1
|
||||
|
||||
sheetTag.cell(row=last_row, column=1, value=last_row)
|
||||
sheetTag.cell(row=last_row, column=2, value=strUrl)
|
||||
sheetTag.cell(row=last_row, column=3, value=strValue)
|
||||
util.DbgOut(f"AddTag : {strSheet}:{nIndexID},{strValue} added.")
|
||||
nIndexID = last_row
|
||||
|
||||
return nIndexID
|
||||
|
||||
# return Value :
|
||||
# -1 : 시트가 뭔가 잘못되었다.
|
||||
# 0 : 추가할 값이 이미 들어있다.
|
||||
def SearchIDFromName(self, sheet, strValue, nTrgCol=3):
|
||||
if sheet is None :
|
||||
return -1
|
||||
|
||||
nRetID = 0
|
||||
for nRow in range(1, sheet.max_row + 1):
|
||||
valTemp = sheet.cell(row=nRow, column=nTrgCol).value
|
||||
if None == valTemp :
|
||||
continue
|
||||
|
||||
if valTemp.casefold() == strValue.casefold() :
|
||||
nRetID = sheet.cell(row=nRow, column=1).value
|
||||
break;
|
||||
|
||||
return nRetID
|
||||
|
||||
|
||||
def SearchIDFromList(self, sheet, listValue, nStartCol=3):
|
||||
if sheet is None :
|
||||
return -1
|
||||
|
||||
nRetID = 0
|
||||
for nRow in range(1, sheet.max_row + 1):
|
||||
for nCol in range(0, len(listValue)):
|
||||
valTemp = sheet.cell(row=nRow, column=nStartCol + nCol).value
|
||||
if None == valTemp :
|
||||
continue
|
||||
|
||||
strValue = listValue[nCol]
|
||||
if valTemp.casefold() == strValue.casefold() :
|
||||
nRetID = sheet.cell(row=nRow, column=1).value
|
||||
break;
|
||||
|
||||
return nRetID
|
||||
|
||||
|
||||
# 시트를 가져온다. 엑셀 파일이 안 열려 있으면 None, 있으면 반환하고, 없으면 만들어서.
|
||||
def getSheet(self, sheetName):
|
||||
retSheet = None
|
||||
|
||||
if self.m_wb is None:
|
||||
util.DbgOut("XLS not opened", True)
|
||||
return None
|
||||
|
||||
try:
|
||||
retSheet = self.m_wb[sheetName]
|
||||
except KeyError:
|
||||
retSheet = self.m_wb.create_sheet(title=sheetName)
|
||||
util.DbgOut(f"GetSheet : {sheetName} is Created", True)
|
||||
|
||||
return retSheet
|
||||
|
||||
|
||||
# 데이터베이스용 엑셀 파일의 전체 경로를 얻어온다.
|
||||
def GetXLSPath(self, path):
|
||||
retPath = os.path.abspath(path)
|
||||
|
||||
if False == os.path.exists(retPath):
|
||||
abspath = os.path.dirname(__file__)
|
||||
retPath = os.path.join(abspath, self.xls_name)
|
||||
|
||||
return retPath
|
||||
|
||||
|
||||
Reference in New Issue
Block a user