Update .gitignore, DataClass.py, and 19 more files...
오랜만에 서버 정리하고 커밋. 파일 위치를 정리했다. 캘리버 DB 를 열고 정보를 열람. Pupil 을 통해 다운받은 정보를 관리하기 위해 새로운 클래스 추가
This commit is contained in:
170
UtilPack.py
170
UtilPack.py
@@ -7,19 +7,22 @@ import zipfile
|
||||
import shutil
|
||||
import difflib
|
||||
import subprocess
|
||||
import hashlib
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
m_dbgLevel = 0
|
||||
listDbgStr = []
|
||||
listDbgStr: list[str] = []
|
||||
|
||||
|
||||
#
|
||||
def IsEmptyStr(string):
|
||||
def IsEmptyStr(string: str) -> bool:
|
||||
temp = f"{string}"
|
||||
return 0 == len(temp.strip())
|
||||
|
||||
|
||||
#
|
||||
def GetCurrentTime():
|
||||
def GetCurrentTime() -> str:
|
||||
# 현재 시간을 구하고 구조체로 변환
|
||||
current_time_struct = time.localtime()
|
||||
|
||||
@@ -35,21 +38,24 @@ def GetCurrentTime():
|
||||
|
||||
return strRet
|
||||
|
||||
|
||||
#for debug
|
||||
def DbgOut(strInput, bPrint = False):
|
||||
def DbgOut(strInput:str, bPrint:bool = False):
|
||||
strMsg = (f"{GetCurrentTime()} : {strInput}")
|
||||
listDbgStr.append(strMsg)
|
||||
|
||||
if True == bPrint:
|
||||
print(strMsg)
|
||||
|
||||
|
||||
#
|
||||
def printDbgMessages():
|
||||
for line in listDbgStr:
|
||||
print(line)
|
||||
|
||||
|
||||
#
|
||||
def SaveDbgMessages(Path):
|
||||
def SaveDbgMessages(Path: str):
|
||||
try:
|
||||
with open(Path, 'w') as file:
|
||||
for line in listDbgStr:
|
||||
@@ -57,10 +63,11 @@ def SaveDbgMessages(Path):
|
||||
except IOError:
|
||||
DbgOut(f"Error: Could not write to the file at {Path}.", True)
|
||||
|
||||
|
||||
# 입력된 경로의 자식 폴더를 찾아 반환한다.
|
||||
# 반환하는 리스트는 리커시브 - 손자, 증손자 폴더까지 전부 포함한다
|
||||
def ListSubDirectories(root_dir):
|
||||
subdirectories = []
|
||||
def ListSubDirectories(root_dir:str)-> list[str]:
|
||||
subdirectories: list[str] = []
|
||||
|
||||
# root_dir에서 하위 디렉토리 및 파일 목록을 얻음
|
||||
for dirpath, dirnames, filenames in os.walk(root_dir):
|
||||
@@ -74,32 +81,36 @@ def ListSubDirectories(root_dir):
|
||||
return subdirectories
|
||||
|
||||
# 자식 폴더를 구해온다. 직계 자식만
|
||||
def ListChildDirectories(pathDir):
|
||||
listRet = []
|
||||
for name in os.listdir(pathDir):
|
||||
def ListChildDirectories(pathDir:str, bVisibleOnly: bool = True) -> list[str]:
|
||||
listRet:list[str] = []
|
||||
listTemp = os.listdir(pathDir)
|
||||
for name in listTemp:
|
||||
pathChild = os.path.join(pathDir, name)
|
||||
if os.path.isdir(pathChild):
|
||||
listRet.append(pathChild)
|
||||
|
||||
if True == bVisibleOnly and name.startswith('.'):
|
||||
continue
|
||||
|
||||
listRet.append(name)
|
||||
|
||||
return listRet
|
||||
|
||||
# 파일목록만 구해온다. 자식 폴더에 있는건 무시.
|
||||
def ListContainFiles(pathDir):
|
||||
listRet = []
|
||||
for name in os.listdir(pathDir):
|
||||
# PathDir 에 지정된 폴더의 파일목록만 구해온다. 자식 폴더에 있는건 무시.
|
||||
def ListContainFiles(pathDir:str, bVisibleOnly:bool = True)-> list[str]:
|
||||
listRet:list[str] = []
|
||||
listTemp = os.listdir(pathDir)
|
||||
for name in listTemp:
|
||||
pathChild = os.path.join(pathDir, name)
|
||||
if not os.path.isdir(pathChild):
|
||||
listRet.append(pathChild)
|
||||
|
||||
if os.path.isfile(pathChild):
|
||||
if True == bVisibleOnly and name.startswith('.'):
|
||||
continue
|
||||
|
||||
listRet.append(name)
|
||||
|
||||
return listRet
|
||||
|
||||
#
|
||||
def ListFileExtRcr(pathTrg, listExt):
|
||||
listRet= []
|
||||
|
||||
if False == isinstance(listExt, list):
|
||||
print("ext must list")
|
||||
return
|
||||
# 리스트에 담긴 확장자와 같은 확장자를 가진 파일을 찾아서 리스트로 반환
|
||||
def ListFileExtRcr(pathTrg: str, listExt: list[str]) -> list[str]:
|
||||
listRet:list[str] = []
|
||||
|
||||
# pathTrg의 하위 디렉토리 및 파일 목록을 얻음
|
||||
for dirpath, dirnames, filenames in os.walk(pathTrg):
|
||||
@@ -111,7 +122,7 @@ def ListFileExtRcr(pathTrg, listExt):
|
||||
return listRet
|
||||
|
||||
# 입력된 경로에서 부모 폴더를 찾는다. 0 은 자기자신 1은 부모, 숫자대로 위로.
|
||||
def GetParentDirName(FullPath, nUp):
|
||||
def GetParentDirName(FullPath : str, nUp : int)->str:
|
||||
parts = FullPath.split(os.sep)
|
||||
|
||||
nTrgIdx = 0
|
||||
@@ -126,24 +137,24 @@ def GetParentDirName(FullPath, nUp):
|
||||
|
||||
# 입력된 경로가 자식 폴더를 가지고 있는지 판단한다.- 최종 폴더인지 여부
|
||||
# 자식이 없으면 True, 자식이 있으면 False
|
||||
def IsFinalFolder(path):
|
||||
def IsFinalFolder(path : str) -> bool:
|
||||
bRet = True
|
||||
|
||||
contents = os.listdir(path)
|
||||
for item in contents:
|
||||
if True == os.path.isdir(item):
|
||||
bRt = False
|
||||
bRet = False
|
||||
break
|
||||
|
||||
return bRet;
|
||||
|
||||
# 어떤 경로 안에서 특정 확장자의 파일을 뽑아내어 그 리스트를 반환한다.
|
||||
def FindFileFromExt(path, ext):
|
||||
def FindFileFromExt(path: str, ext: str)-> list[str]:
|
||||
bDot = False
|
||||
if 0 <= ext.find('.'):
|
||||
bDot = True
|
||||
|
||||
listRet = []
|
||||
listRet:list[str] = []
|
||||
if False == os.path.exists(path):
|
||||
return listRet
|
||||
|
||||
@@ -159,7 +170,7 @@ def FindFileFromExt(path, ext):
|
||||
return listRet
|
||||
|
||||
# 파일 이름에서 확장자를 뽑아낸다. True : '.' 을 포함한다.
|
||||
def GetExtStr(file_path, bDot = True):
|
||||
def GetExtStr(file_path: str, bDot: bool = True)-> str:
|
||||
retStr = ""
|
||||
# 파일 경로에서 마지막 점을 찾아 확장자를 추출
|
||||
last_dot_index = file_path.rfind('.')
|
||||
@@ -174,7 +185,7 @@ def GetExtStr(file_path, bDot = True):
|
||||
return retStr
|
||||
|
||||
# 문자열에 포함된 단어를 지운다.
|
||||
def RmvSubString(mainString, subString):
|
||||
def RmvSubString(mainString: str, subString: str)-> str:
|
||||
# 문자열에서 부분 문자열의 인덱스를 찾습니다.
|
||||
strIdx = mainString.find(subString)
|
||||
if strIdx == -1: # 부분 문자열이 존재하지 않으면 그대로 반환합니다.
|
||||
@@ -186,12 +197,12 @@ def RmvSubString(mainString, subString):
|
||||
return mainString[:strIdx] + mainString[endIdx:]
|
||||
|
||||
#
|
||||
def ExtractZIP(zip_file, extract_to):
|
||||
def ExtractZIP(zip_file: str, extract_to: str):
|
||||
with zipfile.ZipFile(zip_file, 'r') as zf:
|
||||
zf.extractall(extract_to)
|
||||
|
||||
#
|
||||
def CreateZIP(output_zip, *files):
|
||||
def CreateZIP(output_zip: str, files: list[str]) -> bool:
|
||||
with zipfile.ZipFile(output_zip, 'w') as zf:
|
||||
for file in files:
|
||||
pathTemp = os.path.join('root', os.path.basename(file))
|
||||
@@ -204,7 +215,7 @@ def CreateZIP(output_zip, *files):
|
||||
return bRet
|
||||
|
||||
# 파일 리스트에 들어있는 파일만 골라서 압축을 합니다. 상대경로를 제거하는게 기본값.
|
||||
def CreateZIPShell(zipName, *files, bRmvRPath = True):
|
||||
def CreateZIPShell(zipName: str, files: list[str], bRmvRPath: bool = True) -> bool:
|
||||
command = "zip "
|
||||
|
||||
if True == bRmvRPath:
|
||||
@@ -214,16 +225,7 @@ def CreateZIPShell(zipName, *files, bRmvRPath = True):
|
||||
|
||||
# 이중 리스트인 이유를 모르겠다.
|
||||
for file in files:
|
||||
strTemp = ""
|
||||
if isinstance(file, list):
|
||||
strTemp = ' '.join(file)
|
||||
else:
|
||||
strTemp = f"\"{file}\" "
|
||||
|
||||
command += strTemp
|
||||
|
||||
# for item in file:
|
||||
# command += f"\"{item}\" "
|
||||
command += f"\"{file}\" "
|
||||
|
||||
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
||||
|
||||
@@ -234,7 +236,7 @@ def CreateZIPShell(zipName, *files, bRmvRPath = True):
|
||||
return bRet
|
||||
|
||||
# 특정 확장자만 쉘을 이용해서 압축한다
|
||||
def CreateZIPShExt(zipName, TrgExt):
|
||||
def CreateZIPShExt(zipName: str, TrgExt: str)-> bool:
|
||||
command = f"zip -j {zipName} *.{TrgExt}"
|
||||
|
||||
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
||||
@@ -246,8 +248,8 @@ def CreateZIPShExt(zipName, TrgExt):
|
||||
return bRet
|
||||
|
||||
# 압축 파일 내의 모든 파일 및 디렉토리 목록 가져오기
|
||||
def GetZipContentList(path):
|
||||
if None == path or not os.path.isfile(path):
|
||||
def GetZipContentList(path: str) -> list[str]:
|
||||
if True == IsEmptyStr(path) or not os.path.isfile(path):
|
||||
return []
|
||||
|
||||
listRet = []
|
||||
@@ -257,22 +259,18 @@ def GetZipContentList(path):
|
||||
return listRet
|
||||
|
||||
#
|
||||
def GetZippedFileByte(pathZip, FileName):
|
||||
if None == pathZip or not os.path.isfile(pathZip):
|
||||
return None
|
||||
|
||||
retBytes = None
|
||||
with zipfile.ZipFile( pathZip , 'r') as zip_file:
|
||||
if not FileName in zip_file.namelist():
|
||||
return None
|
||||
|
||||
with zip_file.open(FileName) as file:
|
||||
retBytes = file.read()
|
||||
def GetZippedFileByte(pathZip: str, FileName: str) -> bytes:
|
||||
retBytes:bytes = bytes()
|
||||
if True == os.path.isfile(pathZip):
|
||||
with zipfile.ZipFile( pathZip , 'r') as zip_file:
|
||||
# 압축 파일 내의 특정 파일을 읽기
|
||||
with zip_file.open(FileName) as file:
|
||||
retBytes = file.read()
|
||||
|
||||
return retBytes
|
||||
|
||||
# JSON 을 트리 구조로 출력한다.
|
||||
def PrintJSONTree(data, indent=0):
|
||||
def PrintJSONTree(data, indent: int=0 ) -> None:
|
||||
if isinstance(data, dict):
|
||||
for key, value in data.items():
|
||||
print(' ' * indent + str(key))
|
||||
@@ -295,10 +293,54 @@ def UUIDGenRandom():
|
||||
return random_uuid
|
||||
|
||||
# 이름을 기반으로 UUID 생성
|
||||
def UUIDGenName(SeedName):
|
||||
def UUIDGenName(SeedName:str):
|
||||
namespace_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, SeedName)
|
||||
return namespace_uuid
|
||||
|
||||
#
|
||||
def GetTextInBrakets(text):
|
||||
return re.findall(r'\[(.*?)\]', text)
|
||||
def GetTextInBrakets(text:str)-> list[str]:
|
||||
return re.findall(r'\[(.*?)\]', text)
|
||||
|
||||
#파일의 해시를 계산하는 함수.
|
||||
#Args:
|
||||
# strFilePath (str): 파일 경로
|
||||
# method (str): 사용할 해시 알고리즘 ('md5', 'sha1', 'sha256' 등)
|
||||
#Returns:
|
||||
# str: 계산된 해시값 (16진수 문자열)
|
||||
def CalculateFileHash(strFilePath: str, method: str="sha256")-> str:
|
||||
funcHash = getattr(hashlib, method)()
|
||||
|
||||
with open(strFilePath, "rb") as f:
|
||||
while True:
|
||||
chunk = f.read(4096)
|
||||
if not chunk:
|
||||
break
|
||||
|
||||
funcHash.update(chunk)
|
||||
|
||||
return funcHash.hexdigest()
|
||||
|
||||
#파일의 해시를 비교하여 무결성 검증.
|
||||
#Args:
|
||||
# file_path (str): 파일 경로
|
||||
# expected_hash (str): 기대하는 해시값
|
||||
# method (str): 사용할 해시 알고리즘
|
||||
#Returns:
|
||||
# bool: 파일이 정상인지 여부
|
||||
def VerifyIsValidFile(strPath: str, strCompHash: str, strMethod: str="sha256")->bool:
|
||||
Hash_Calcd = CalculateFileHash(strPath, strMethod)
|
||||
|
||||
return strCompHash.lower() == Hash_Calcd.lower()
|
||||
|
||||
"""
|
||||
# 사용 예시
|
||||
if __name__ == "__main__":
|
||||
file_to_check = "example.txt"
|
||||
known_good_hash = "5d41402abc4b2a76b9719d911017c592" # 예시 (MD5)
|
||||
|
||||
is_valid = verify_file(file_to_check, known_good_hash, method='md5')
|
||||
if is_valid:
|
||||
print("파일이 정상입니다!")
|
||||
else:
|
||||
print("파일이 손상되었거나 다릅니다!")
|
||||
"""
|
||||
Reference in New Issue
Block a user