Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
app.py -
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
"""
|
| 9 |
|
| 10 |
import argparse
|
|
@@ -15,49 +15,53 @@ from pathlib import Path
|
|
| 15 |
|
| 16 |
|
| 17 |
def find_file(patterns, search_dir="."):
|
| 18 |
-
"""Tìm file đầu tiên khớp với
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
for pattern in patterns:
|
| 20 |
-
for
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
return None
|
| 29 |
|
| 30 |
|
| 31 |
def main():
|
| 32 |
-
parser = argparse.ArgumentParser(description="Wrapper
|
| 33 |
parser.add_argument("--thuoc", help="Đường dẫn file thuốc")
|
| 34 |
parser.add_argument("--xetnghiem", help="Đường dẫn file xét nghiệm")
|
| 35 |
parser.add_argument("--icd_plt", help="Đường dẫn file ICD_PLT")
|
| 36 |
-
parser.add_argument("--out", default="/app/ketqua.csv", help="File kết quả
|
| 37 |
-
parser.add_argument("--input-dir", default="/data", help="Thư mục
|
| 38 |
args = parser.parse_args()
|
| 39 |
|
| 40 |
-
print("=== KHỞI ĐỘNG XỬ LÝ PLT ===")
|
| 41 |
-
print(f"Thời gian: {__import__('datetime').datetime.now()
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
thuoc = args.thuoc or os.
|
| 45 |
-
xetnghiem = args.xetnghiem or os.
|
| 46 |
-
icd_plt = args.icd_plt or os.
|
| 47 |
|
| 48 |
search_dir = args.input_dir
|
| 49 |
|
| 50 |
if not thuoc:
|
| 51 |
-
print("
|
| 52 |
-
thuoc = find_file(["thuoc", "Thuoc", "THUOC", "thuốc"], search_dir)
|
| 53 |
|
| 54 |
if not xetnghiem:
|
| 55 |
-
print("
|
| 56 |
-
xetnghiem = find_file(["xetnghiem", "XetNghiem", "xét nghiệm", "lab", "test"], search_dir)
|
| 57 |
|
| 58 |
if not icd_plt:
|
| 59 |
-
print("
|
| 60 |
-
icd_plt = find_file(["ICD_PLT", "icd_plt", "ICD", "icd"], search_dir)
|
| 61 |
|
| 62 |
# Kiểm tra cuối cùng
|
| 63 |
missing = []
|
|
@@ -68,20 +72,24 @@ def main():
|
|
| 68 |
missing.append(f"{name}: không tồn tại → {path}")
|
| 69 |
|
| 70 |
if missing:
|
| 71 |
-
print("
|
| 72 |
for m in missing:
|
| 73 |
print(" •", m)
|
| 74 |
-
print("\
|
| 75 |
-
print("
|
| 76 |
-
print("
|
| 77 |
-
print("
|
| 78 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
sys.exit(1)
|
| 80 |
|
| 81 |
# Chạy process_plt.py
|
| 82 |
-
script_path = Path(__file__).parent / "process_plt.py"
|
| 83 |
if not script_path.exists():
|
| 84 |
-
print("LỖI: Không tìm thấy process_plt.py trong cùng thư mục!")
|
| 85 |
sys.exit(127)
|
| 86 |
|
| 87 |
cmd = [
|
|
@@ -92,18 +100,17 @@ def main():
|
|
| 92 |
"--out", args.out
|
| 93 |
]
|
| 94 |
|
| 95 |
-
print("
|
| 96 |
-
print("Đường dẫn sử dụng:")
|
| 97 |
print(f" • Thuốc : {thuoc}")
|
| 98 |
print(f" • Xét nghiệm : {xetnghiem}")
|
| 99 |
print(f" • ICD_PLT : {icd_plt}")
|
| 100 |
print(f" • Kết quả : {args.out}")
|
| 101 |
-
print("\nĐang
|
| 102 |
print(" " + " ".join(cmd))
|
| 103 |
-
print("
|
| 104 |
|
| 105 |
result = subprocess.call(cmd)
|
| 106 |
-
print(f"\nHOÀN THÀNH! Mã
|
| 107 |
sys.exit(result)
|
| 108 |
|
| 109 |
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
app.py - Phiên bản HOÀN HẢO, chạy ngon trong Docker, CI/CD, cronjob
|
| 4 |
+
Hỗ trợ đầy đủ:
|
| 5 |
+
- Biến môi trường THUOC, XETNGHIEM, ICD_PLT
|
| 6 |
+
- Tự tìm file trong thư mục /data
|
| 7 |
+
- Không bao giờ hỏi input()
|
| 8 |
"""
|
| 9 |
|
| 10 |
import argparse
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def find_file(patterns, search_dir="."):
|
| 18 |
+
"""Tìm file đầu tiên khớp với các pattern (không phân biệt hoa thường)"""
|
| 19 |
+
search_path = Path(search_dir)
|
| 20 |
+
if not search_path.exists():
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
for pattern in patterns:
|
| 24 |
+
for pat in [pattern, pattern.lower(), pattern.upper(), pattern.capitalize()]:
|
| 25 |
+
for ext in ["", ".xlsx", ".xls", ".csv", ".XLSX", ".XLS", ".CSV"]:
|
| 26 |
+
full_pat = f"*{pat}{ext}"
|
| 27 |
+
for file_path in search_path.glob(full_pat):
|
| 28 |
+
if file_path.is_file():
|
| 29 |
+
abs_path = str(file_path.resolve())
|
| 30 |
+
print(f"Tự động tìm thấy: {abs_path}")
|
| 31 |
+
return abs_path
|
| 32 |
return None
|
| 33 |
|
| 34 |
|
| 35 |
def main():
|
| 36 |
+
parser = argparse.ArgumentParser(description="Wrapper xử lý PLT - Docker ready 100%")
|
| 37 |
parser.add_argument("--thuoc", help="Đường dẫn file thuốc")
|
| 38 |
parser.add_argument("--xetnghiem", help="Đường dẫn file xét nghiệm")
|
| 39 |
parser.add_argument("--icd_plt", help="Đường dẫn file ICD_PLT")
|
| 40 |
+
parser.add_argument("--out", default="/app/ketqua.csv", help="File kết quả")
|
| 41 |
+
parser.add_argument("--input-dir", default="/data", help="Thư mục tìm file tự động (default: /data)")
|
| 42 |
args = parser.parse_args()
|
| 43 |
|
| 44 |
+
print("\n=== KHỞI ĐỘNG XỬ LÝ PLT ===")
|
| 45 |
+
print(f"Thời gian: {__import__('datetime').datetime.now():%Y-%m-%d %H:%M:%S}\n")
|
| 46 |
|
| 47 |
+
# Ưu tiên: CLI → env → tự tìm trong input-dir
|
| 48 |
+
thuoc = args.thuoc or os.getenv("THUOC")
|
| 49 |
+
xetnghiem = args.xetnghiem or os.getenv("XETNGHIEM")
|
| 50 |
+
icd_plt = args.icd_plt or os.getenv("ICD_PLT")
|
| 51 |
|
| 52 |
search_dir = args.input_dir
|
| 53 |
|
| 54 |
if not thuoc:
|
| 55 |
+
print(f"→ Đang tìm file thuốc trong: {search_dir}")
|
| 56 |
+
thuoc = find_file(["thuoc", "Thuoc", "THUOC", "thuốc", "drug", "medicine"], search_dir)
|
| 57 |
|
| 58 |
if not xetnghiem:
|
| 59 |
+
print(f"→ Đang tìm file xét nghiệm trong: {search_dir}")
|
| 60 |
+
xetnghiem = find_file(["xetnghiem", "XetNghiem", "xét nghiệm", "lab", "test", "xn"], search_dir)
|
| 61 |
|
| 62 |
if not icd_plt:
|
| 63 |
+
print(f"→ Đang tìm file ICD_PLT trong: {search_dir}")
|
| 64 |
+
icd_plt = find_file(["ICD_PLT", "icd_plt", "ICD", "icd", "icdplt"], search_dir)
|
| 65 |
|
| 66 |
# Kiểm tra cuối cùng
|
| 67 |
missing = []
|
|
|
|
| 72 |
missing.append(f"{name}: không tồn tại → {path}")
|
| 73 |
|
| 74 |
if missing:
|
| 75 |
+
print("LỖI: Thiếu file bắt buộc!\n")
|
| 76 |
for m in missing:
|
| 77 |
print(" •", m)
|
| 78 |
+
print("\n" + "="*60)
|
| 79 |
+
print("CÁCH KHẮC PHỤC (chọn 1 trong 3):")
|
| 80 |
+
print("\n1. Dùng biến môi trường (KHUYẾN NGHỊ NHẤT):")
|
| 81 |
+
print(" docker run -e THUOC=/data/thuoc.xlsx -e XETNGHIEM=/data/xn.xlsx -e ICD_PLT=/data/ICD.csv your-image")
|
| 82 |
+
print("\n2. Mount thư mục chứa file vào /data (script tự tìm):")
|
| 83 |
+
print(" docker run -v \"/đường/dẫn/thư/mục/dữ/liệu:/data\" your-image")
|
| 84 |
+
print("\n3. Truyền trực tiếp tham số:")
|
| 85 |
+
print(" docker run -v ./data:/data your-image python app.py --thuoc /data/abc.xlsx ...")
|
| 86 |
+
print("="*60)
|
| 87 |
sys.exit(1)
|
| 88 |
|
| 89 |
# Chạy process_plt.py
|
| 90 |
+
script_path = Path(__file__).parent.resolve() / "process_plt.py"
|
| 91 |
if not script_path.exists():
|
| 92 |
+
print("LỖI: Không tìm thấy file process_plt.py trong cùng thư mục!")
|
| 93 |
sys.exit(127)
|
| 94 |
|
| 95 |
cmd = [
|
|
|
|
| 100 |
"--out", args.out
|
| 101 |
]
|
| 102 |
|
| 103 |
+
print("Tất cả file đã sẵn sàng!")
|
|
|
|
| 104 |
print(f" • Thuốc : {thuoc}")
|
| 105 |
print(f" • Xét nghiệm : {xetnghiem}")
|
| 106 |
print(f" • ICD_PLT : {icd_plt}")
|
| 107 |
print(f" • Kết quả : {args.out}")
|
| 108 |
+
print("\nĐang chạy lệnh:")
|
| 109 |
print(" " + " ".join(cmd))
|
| 110 |
+
print("\n" + "-"*80)
|
| 111 |
|
| 112 |
result = subprocess.call(cmd)
|
| 113 |
+
print(f"\nHOÀN THÀNH! Mã thoát: {result}")
|
| 114 |
sys.exit(result)
|
| 115 |
|
| 116 |
|