pos_update_gui 파일도 동일하게 모니터링하도록 변경
This commit is contained in:
@ -4,15 +4,14 @@ from watchdog.observers import Observer
|
|||||||
from watchdog.events import FileSystemEventHandler
|
from watchdog.events import FileSystemEventHandler
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
# pos_update_bill 모듈에서 main 함수를 가져옵니다.
|
|
||||||
# pos_update_bill.py가 같은 폴더 혹은 PYTHONPATH에 있어야 합니다.
|
|
||||||
import pos_update_bill
|
import pos_update_bill
|
||||||
|
import pos_update_gui # 추가
|
||||||
|
|
||||||
DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../data'))
|
DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../data'))
|
||||||
|
|
||||||
# 감시할 파일 확장자 및 패턴 정의 (pos_update_bill.py 내와 일치해야 함)
|
|
||||||
FILE_EXTENSIONS = ('.xls', '.xlsx')
|
FILE_EXTENSIONS = ('.xls', '.xlsx')
|
||||||
FILE_PREFIX = "영수증별매출상세현황"
|
BILL_PREFIX = "영수증별매출상세현황"
|
||||||
|
GUI_PREFIX = "일자별 (상품별)"
|
||||||
|
|
||||||
class NewFileHandler(FileSystemEventHandler):
|
class NewFileHandler(FileSystemEventHandler):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -25,9 +24,12 @@ class NewFileHandler(FileSystemEventHandler):
|
|||||||
return
|
return
|
||||||
filepath = event.src_path
|
filepath = event.src_path
|
||||||
filename = os.path.basename(filepath)
|
filename = os.path.basename(filepath)
|
||||||
if filename.startswith(FILE_PREFIX) and filename.endswith(FILE_EXTENSIONS):
|
if not filename.endswith(FILE_EXTENSIONS):
|
||||||
|
return
|
||||||
|
|
||||||
|
# 처리 대상 여부 확인
|
||||||
|
if filename.startswith(BILL_PREFIX) or filename.startswith(GUI_PREFIX):
|
||||||
print(f"[WATCHER] 신규 파일 감지: {filename}")
|
print(f"[WATCHER] 신규 파일 감지: {filename}")
|
||||||
# 별도의 스레드에서 처리 (감시 중단 방지)
|
|
||||||
threading.Thread(target=self.process_file, args=(filepath, filename), daemon=True).start()
|
threading.Thread(target=self.process_file, args=(filepath, filename), daemon=True).start()
|
||||||
|
|
||||||
def process_file(self, filepath, filename):
|
def process_file(self, filepath, filename):
|
||||||
@ -38,15 +40,19 @@ class NewFileHandler(FileSystemEventHandler):
|
|||||||
self._processing_files.add(filename)
|
self._processing_files.add(filename)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 파일이 완전히 쓰여질 때까지 대기 (간단히 3초 대기, 필요 시 로직 강화)
|
time.sleep(3) # 파일 쓰기 완료 대기
|
||||||
time.sleep(3)
|
|
||||||
|
|
||||||
print(f"[WATCHER] 파일 처리 시작: {filename}")
|
print(f"[WATCHER] 파일 처리 시작: {filename}")
|
||||||
# pos_update_bill.main() 내부가 파일 리스트를 탐색해서 처리하므로
|
if filename.startswith(BILL_PREFIX):
|
||||||
# 신규 파일이 존재하는 상태에서 호출하면 정상 동작함.
|
pos_update_bill.main()
|
||||||
pos_update_bill.main()
|
elif filename.startswith(GUI_PREFIX):
|
||||||
|
pos_update_gui.main()
|
||||||
|
else:
|
||||||
|
print(f"[WATCHER] 처리 대상이 아님: {filename}")
|
||||||
|
return
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[WATCHER] 파일 처리 중 오류 발생: {filename} / {e}")
|
print(f"[WATCHER] 처리 중 오류 발생: {filename} / {e}")
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
os.remove(filepath)
|
os.remove(filepath)
|
||||||
@ -58,7 +64,7 @@ class NewFileHandler(FileSystemEventHandler):
|
|||||||
self._processing_files.discard(filename)
|
self._processing_files.discard(filename)
|
||||||
|
|
||||||
def start_watching():
|
def start_watching():
|
||||||
print(f"[WATCHER] {DATA_DIR} 폴더 감시 시작")
|
print(f"[WATCHER] '{DATA_DIR}' 폴더 감시 시작")
|
||||||
event_handler = NewFileHandler()
|
event_handler = NewFileHandler()
|
||||||
observer = Observer()
|
observer = Observer()
|
||||||
observer.schedule(event_handler, DATA_DIR, recursive=False)
|
observer.schedule(event_handler, DATA_DIR, recursive=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user