Refactor: typing cleanup, 경로 하드코딩 제거, GPS 데이터 Lon/Lat 분리, 불필요 모듈 삭제
This commit is contained in:
66
UtilPack.py
66
UtilPack.py
@@ -2,22 +2,25 @@ import os
|
||||
import re
|
||||
import math
|
||||
import time
|
||||
import rarfile
|
||||
#import rarfile
|
||||
import zipfile
|
||||
import shutil
|
||||
import difflib
|
||||
#import shutil
|
||||
#import difflib
|
||||
import subprocess
|
||||
import UtilPack as util
|
||||
from pathlib import Path
|
||||
|
||||
m_dbgLevel = 0
|
||||
listDbgStr = []
|
||||
listDbgStr:list[str] = []
|
||||
|
||||
#
|
||||
def IsEmptyStr(string):
|
||||
def IsEmptyStr(string:str) -> bool:
|
||||
if type(string) is not str:
|
||||
return True
|
||||
|
||||
return 0 == len(string.strip())
|
||||
|
||||
#
|
||||
def GetCurrentTime():
|
||||
def GetCurrentTime() -> str:
|
||||
# 현재 시간을 구하고 구조체로 변환
|
||||
current_time_struct = time.localtime()
|
||||
|
||||
@@ -34,7 +37,7 @@ 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)
|
||||
|
||||
@@ -46,7 +49,7 @@ def printDbgMessages():
|
||||
print(line)
|
||||
|
||||
# 입력된 패스에서 부모 폴더를 찾는다. 0 은 자기자신 1은 부모, 숫자대로 위로.
|
||||
def GetParentDirName(FullPath, nUp):
|
||||
def GetParentDirName(FullPath:str, nUp:int) -> str:
|
||||
parts = FullPath.split(os.sep)
|
||||
|
||||
nTrgIdx = 0
|
||||
@@ -60,7 +63,7 @@ def GetParentDirName(FullPath, nUp):
|
||||
return parts[nTrgIdx]
|
||||
|
||||
# os 모듈을 사용하여 자식 폴더를 반환
|
||||
def GetSubDirectories(folder_path):
|
||||
def GetSubDirectories(folder_path:str)-> list[str]:
|
||||
subdirectories = [
|
||||
d for d in os.listdir(folder_path)
|
||||
if os.path.isdir(os.path.join(folder_path, d))]
|
||||
@@ -69,8 +72,8 @@ def GetSubDirectories(folder_path):
|
||||
|
||||
# 입력된 경로의 자식 폴더를 찾아 반환한다.
|
||||
# 반환하는 리스트는 리커시브 - 손자, 증손자 폴더까지 전부 포함한다
|
||||
def GetAllSubDirectories(root_dir):
|
||||
subdirectories = []
|
||||
def GetAllSubDirectories(root_dir:str)-> list[str]:
|
||||
subdirectories:list[str] = []
|
||||
|
||||
# root_dir에서 하위 디렉토리 및 파일 목록을 얻음
|
||||
for dirpath, dirnames, filenames in os.walk(root_dir):
|
||||
@@ -83,8 +86,8 @@ def GetAllSubDirectories(root_dir):
|
||||
|
||||
return subdirectories
|
||||
|
||||
def ListFileExtRcr(pathTrg, strExt):
|
||||
listRet= []
|
||||
def ListFileExtRcr(pathTrg:str, strExt:str) -> list[str]:
|
||||
listRet:list[str] = []
|
||||
|
||||
# pathTrg의 하위 디렉토리 및 파일 목록을 얻음
|
||||
for dirpath, dirnames, filenames in os.walk(pathTrg):
|
||||
@@ -97,7 +100,7 @@ def ListFileExtRcr(pathTrg, strExt):
|
||||
|
||||
# 입력된 경로가 자식 폴더를 가지고 있는지 판단한다.- 최종 폴더인지 여부
|
||||
# 자식이 없으면 True, 자식이 있으면 False
|
||||
def IsFinalFolder(path):
|
||||
def IsFinalFolder(path:str) -> bool:
|
||||
bRet = True
|
||||
|
||||
contents = os.listdir(path)
|
||||
@@ -109,12 +112,12 @@ def IsFinalFolder(path):
|
||||
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
|
||||
|
||||
@@ -131,7 +134,7 @@ def FindFileFromExt(path, ext):
|
||||
|
||||
|
||||
# 파일 이름에서 확장자를 뽑아낸다. True : '.' 을 포함한다.
|
||||
def GetExtStr(file_path, bDot = True):
|
||||
def GetExtStr(file_path:str, bDot:bool = True) -> str:
|
||||
retStr = ""
|
||||
# 파일 경로에서 마지막 점을 찾아 확장자를 추출
|
||||
last_dot_index = file_path.rfind('.')
|
||||
@@ -147,7 +150,7 @@ def GetExtStr(file_path, bDot = True):
|
||||
|
||||
|
||||
# 문자열에 포함된 단어를 지운다.
|
||||
def RmvSubString(mainString, subString):
|
||||
def RmvSubString(mainString:str, subString:str) -> str:
|
||||
# 문자열에서 부분 문자열의 인덱스를 찾습니다.
|
||||
strIdx = mainString.find(subString)
|
||||
if strIdx == -1: # 부분 문자열이 존재하지 않으면 그대로 반환합니다.
|
||||
@@ -158,12 +161,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):
|
||||
with zipfile.ZipFile(output_zip, 'w') as zf:
|
||||
for file in files:
|
||||
pathTemp = os.path.join('root', os.path.basename(file))
|
||||
@@ -176,7 +179,7 @@ def CreateZIP(output_zip, *files):
|
||||
return bRet
|
||||
|
||||
# 파일 리스트에 들어있는 파일만 골라서 압축을 합니다. 상대경로를 제거하는게 기본값.
|
||||
def CreateZIPShell(zipName, *files, bRmvRPath = True):
|
||||
def CreateZIPShell(zipName:str, *files, bRmvRPath:bool = True)-> bool:
|
||||
command = "zip "
|
||||
|
||||
if True == bRmvRPath:
|
||||
@@ -206,7 +209,7 @@ def CreateZIPShell(zipName, *files, bRmvRPath = True):
|
||||
return bRet
|
||||
|
||||
# 특정 확장자만 쉘을 이용해서 압축한다
|
||||
def CreateZIPShExt(zipName, TrgExt):
|
||||
def CreateZIPShExt(zipName:str, TrgExt:str):
|
||||
command = f"zip -j {zipName} *.{TrgExt}"
|
||||
|
||||
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
||||
@@ -218,7 +221,7 @@ def CreateZIPShExt(zipName, TrgExt):
|
||||
return bRet
|
||||
|
||||
# JSON 을 트리 구조로 출력한다.
|
||||
def PrintJSONTree(data, indent=0):
|
||||
def PrintJSONTree(data, indent:int=0):
|
||||
if isinstance(data, dict):
|
||||
for key, value in data.items():
|
||||
print(' ' * indent + str(key))
|
||||
@@ -230,7 +233,7 @@ def PrintJSONTree(data, indent=0):
|
||||
print(' ' * indent + str(data))
|
||||
|
||||
# 세로 크기를 가로 비율대로 줄여서 반환 (가로를 기준으로 세로 길이)
|
||||
def GetRSzHeight(nWidth, nHeight, nTrgW):
|
||||
def GetRSzHeight(nWidth:int, nHeight:int, nTrgW:int) -> int:
|
||||
if nWidth <= 0 or nTrgW <= 0:
|
||||
return 0;
|
||||
|
||||
@@ -240,7 +243,7 @@ def GetRSzHeight(nWidth, nHeight, nTrgW):
|
||||
return nTrgH
|
||||
|
||||
# 가로 크기를 세로 비율대로 줄여서 반환 (세로를 기준으로 가로 길이)
|
||||
def GetRSzWidth(nWidth, nHeight, nTrgH):
|
||||
def GetRSzWidth(nWidth:int, nHeight:int, nTrgH:int) -> int:
|
||||
if nHeight <= 0 or nTrgH <= 0:
|
||||
return 0;
|
||||
|
||||
@@ -252,15 +255,15 @@ def GetRSzWidth(nWidth, nHeight, nTrgH):
|
||||
# 엑셀 열 너비는 약 7.5픽셀 단위
|
||||
# 엑셀 행 높이는 약 25픽셀 단위
|
||||
# 이대로는 사이즈가 안 맞아서 적당히 곱해줌
|
||||
def GetCellSize(nImgW, nImgH, DPI = 96):
|
||||
def GetCellSize(nImgW:int, nImgH:int, DPI:int = 96) -> tuple[float, float]:
|
||||
column_width = (nImgW / DPI) * 7.5 * 2
|
||||
row_height = (nImgH / DPI) * 25 * 3
|
||||
|
||||
return column_width, row_height
|
||||
|
||||
def GetSheetInfoValue(text):
|
||||
if text is None:
|
||||
return "", ""
|
||||
def GetSheetInfoValue(text:str) -> tuple[str, list[str]]:
|
||||
if IsEmptyStr(text):
|
||||
return "", []
|
||||
|
||||
pattern = r'<(.*?)>'
|
||||
matches = re.findall(pattern, text)
|
||||
@@ -269,7 +272,7 @@ def GetSheetInfoValue(text):
|
||||
return cleaned, matches
|
||||
|
||||
# 두 GPS 좌표 간 거리 구하기 (미터)
|
||||
def GetDistanceGPS(lat1, lon1, lat2, lon2):
|
||||
def GetDistanceGPS(lat1:float, lon1:float, lat2:float, lon2:float) -> float:
|
||||
# 지구의 반지름 (단위: 미터)
|
||||
R = 6371008.8
|
||||
|
||||
@@ -295,4 +298,5 @@ def GetDistanceGPS(lat1, lon1, lat2, lon2):
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user