QIDNLF commited on
Commit
fffddf5
ยท
verified ยท
1 Parent(s): e4c9153

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -61
app.py CHANGED
@@ -17,7 +17,106 @@ if not os.path.exists(UPLOAD_DIR):
17
  db_registry = []
18
 
19
  # --------------------------
20
- # ์œ ํ‹ธ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # --------------------------
22
  def blob_to_base64_html(blob_data):
23
  if blob_data is None or pd.isna(blob_data):
@@ -204,9 +303,12 @@ def display_data(standard, version, selection):
204
  return pd.DataFrame({"Info": ["์„ ํƒ ํ•„์š”"]})
205
 
206
  try:
207
- target = next(d for d in db_registry if d["standard"] == standard and d["version"] == version)
208
- conn = sqlite3.connect(target["path"])
209
-
 
 
 
210
  conn.text_factory = safe_text_factory
211
 
212
  tables = pd.read_sql(
@@ -219,7 +321,7 @@ def display_data(standard, version, selection):
219
  main_t = tables[0]
220
 
221
  # --------------------------
222
- # ๐Ÿ”น ํ…Œ์ด๋ธ” reverse lookup
223
  # --------------------------
224
  full_table_name = None
225
  pattern = re.compile(f"^{standard}[_\\s-]*{version}[_\\s-]*", re.IGNORECASE)
@@ -234,51 +336,26 @@ def display_data(standard, version, selection):
234
  break
235
 
236
  # ==========================================
237
- # ๐Ÿ”น TableA/B/C ์„ ํƒ (ํŠน์ˆ˜ ๊ตฌ์กฐ ํ…Œ์ด๋ธ” ์ฒ˜๋ฆฌ ์ถ”๊ฐ€)
 
238
  # ==========================================
239
- if full_table_name:
240
- cursor = conn.cursor()
241
- cursor.execute(f"SELECT * FROM [{full_table_name}]")
242
- rows = cursor.fetchall()
243
- cols = [desc[0] for desc in cursor.description]
244
- df = pd.DataFrame(rows, columns=cols)
245
- df.columns = [c.strip() for c in df.columns]
246
- df = df.drop(columns=[c for c in df.columns if c.lower() == "version"], errors="ignore")
247
 
248
- # 1. ์ด๋ฏธ์ง€๋ฅผ ๋จผ์ € ๋งˆํฌ๋‹ค์šด/HTML ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
 
 
 
 
249
  for col in df.columns:
250
  df[col] = df[col].apply(blob_to_base64_html)
251
 
252
- orig_cols = df.columns.tolist()
253
- lower_cols = [c.lower() for c in orig_cols]
254
-
255
- # 2. Section/Description ๋™์  ์กฐ๋ฆฝ ๋กœ์ง
256
- if 'section' in lower_cols and 'description' in lower_cols:
257
- # ์ด๋ฏธ ๊ทœ๊ฒฉ์— ๋งž๋Š” ๊ฒฝ์šฐ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฆ„๋งŒ ๋งž์ถค
258
- df.columns = lower_cols
259
- df = df[['section', 'description']]
260
- else:
261
- # TableB_C ์ฒ˜๋Ÿผ ๊ณ ์œ  ์ปฌ๋Ÿผ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ๊ฒฝ์šฐ์˜ ๋™์  ๋ณ‘ํ•ฉ ์ฒ˜๋ฆฌ
262
- if len(orig_cols) >= 2:
263
- first_col = orig_cols[0] # ์ฒซ ๋ฒˆ์งธ ์—ด (์˜ˆ: Threat Code)๊ฐ€ section์ด ๋จ
264
- other_cols = orig_cols[1:] # ๋‚˜๋จธ์ง€ ์—ด๋“ค์€ description์œผ๋กœ ํ•ฉ์ณ์ง
265
-
266
- def build_combined_desc(row):
267
- parts = []
268
- for col in other_cols:
269
- val = "" if pd.isna(row[col]) else str(row[col])
270
- parts.append(f"**[{col}]**<br>{val}")
271
- return "<br><br>".join(parts)
272
-
273
- df['description'] = df.apply(build_combined_desc, axis=1)
274
- df = df.rename(columns={first_col: 'section'})
275
- df = df[['section', 'description']]
276
-
277
  conn.close()
278
  return df
279
 
280
  # --------------------------
281
- # ๐Ÿ”น ๋ฉ”์ธ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ
 
282
  # --------------------------
283
  cursor = conn.cursor()
284
  cursor.execute(f"PRAGMA table_info([{main_t}])")
@@ -288,20 +365,11 @@ def display_data(standard, version, selection):
288
 
289
  real_ch, real_cat = None, None
290
  for c, lc in zip(cols, lower):
291
- if lc == "chapter":
292
- real_ch = c
293
- elif lc == "category":
294
- real_cat = c
295
 
296
- # --------------------------
297
- # ๐Ÿ”น ALL ์„ ํƒ
298
- # --------------------------
299
  if selection == "ALL":
300
  cursor.execute(f"SELECT * FROM [{main_t}]")
301
-
302
- # --------------------------
303
- # ๐Ÿ”น chapter.category
304
- # --------------------------
305
  elif "." in selection and real_ch and real_cat:
306
  ch, ca = selection.split(".", 1)
307
  query = f"""
@@ -310,21 +378,14 @@ def display_data(standard, version, selection):
310
  AND REPLACE(TRIM(CAST([{real_cat}] AS TEXT)), ' ', '') = ?
311
  """
312
  cursor.execute(query, (ch.replace(" ", ""), ca.replace(" ", "")))
313
-
314
- # --------------------------
315
- # ๐Ÿ”น fallback
316
- # --------------------------
317
  else:
318
  cursor.execute(f"SELECT * FROM [{main_t}]")
319
 
320
  rows = cursor.fetchall()
321
  df = pd.DataFrame(rows, columns=cols)
322
 
323
- # --------------------------
324
- # ๐Ÿ”น ํ›„์ฒ˜๋ฆฌ
325
- # --------------------------
326
  df.columns = [c.lower().strip() for c in df.columns]
327
-
328
  if 'section' in df.columns and 'description' in df.columns:
329
  df = df[['section', 'description']]
330
 
@@ -335,7 +396,6 @@ def display_data(standard, version, selection):
335
  return df
336
 
337
  except Exception as e:
338
- print(f"[Error in display_data]")
339
  traceback.print_exc()
340
  return pd.DataFrame({"Error": [str(e)]})
341
 
 
17
  db_registry = []
18
 
19
  # --------------------------
20
+ # ์œ ํ‹ธdef display_data(standard, version, selection):
21
+ if not all([standard, version, selection]):
22
+ return pd.DataFrame({"Info": ["์„ ํƒ ํ•„์š”"]})
23
+
24
+ try:
25
+ # ์‹ค์‹œ๊ฐ„ ๊ฒฝ๋กœ ํ™•์ธ์„ ์œ„ํ•ด ์ง์ ‘ ๊ฒฝ๋กœ ์ƒ์„ฑ
26
+ db_path = os.path.join(UPLOAD_DIR, f"{standard}_{version}.db")
27
+ if not os.path.exists(db_path):
28
+ return pd.DataFrame({"Error": [f"ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: {db_path}"]})
29
+
30
+ conn = sqlite3.connect(db_path)
31
+ conn.text_factory = safe_text_factory
32
+
33
+ tables = pd.read_sql(
34
+ "SELECT name FROM sqlite_master WHERE type='table';",
35
+ conn
36
+ )['name'].tolist()
37
+
38
+ main_t = f"{standard}_{version}"
39
+ if main_t not in tables:
40
+ main_t = tables[0]
41
+
42
+ # --------------------------
43
+ # ๐Ÿ”น ํ…Œ์ด๋ธ” ๋ช…์นญ ๋งค์นญ (selection์ด ํ…Œ์ด๋ธ” ๋ช…์นญ์ธ์ง€ ํ™•์ธ)
44
+ # --------------------------
45
+ full_table_name = None
46
+ pattern = re.compile(f"^{standard}[_\\s-]*{version}[_\\s-]*", re.IGNORECASE)
47
+
48
+ for t in tables:
49
+ if t == selection:
50
+ full_table_name = t
51
+ break
52
+ short = pattern.sub("", t).strip(" _")
53
+ if short == selection:
54
+ full_table_name = t
55
+ break
56
+
57
+ # ==========================================
58
+ # ๐ŸŒŸ ๋ถ€์† ํ…Œ์ด๋ธ” ์„ ํƒ ์‹œ (TableA, TableB_C ๋“ฑ)
59
+ # ์›๋ณธ ์—ด ๊ตฌ์กฐ(4์—ด, 5์—ด ๋“ฑ)๋ฅผ ๊ทธ๋Œ€๋กœ ์œ ์ง€
60
+ # ==========================================
61
+ if full_table_name and full_table_name != main_t:
62
+ df = pd.read_sql(f"SELECT * FROM [{full_table_name}]", conn)
63
+
64
+ # version ์—ด ์ œ์™ธ ๋ฐ ์ปฌ๋Ÿผ๋ช… ์ •๋ฆฌ
65
+ df = df.drop(columns=[c for c in df.columns if c.lower() == "version"], errors="ignore")
66
+ df.columns = [c.replace("_", " ").title() for c in df.columns]
67
+
68
+ # ๋ชจ๋“  ์…€์— ๋Œ€ํ•ด ์ด๋ฏธ์ง€ ๋ Œ๋”๋ง(BLOB ๋ณ€ํ™˜) ์ ์šฉ
69
+ for col in df.columns:
70
+ df[col] = df[col].apply(blob_to_base64_html)
71
+
72
+ conn.close()
73
+ return df
74
+
75
+ # --------------------------
76
+ # ๐Ÿ”น ๋ฉ”์ธ ํ…Œ์ด๋ธ” (ALL ๋˜๋Š” ํŠน์ • Category)
77
+ # ๊ธฐ์กด ๊ทœ๊ฒฉ(section, description) ์œ ์ง€
78
+ # --------------------------
79
+ cursor = conn.cursor()
80
+ cursor.execute(f"PRAGMA table_info([{main_t}])")
81
+ cols_info = cursor.fetchall()
82
+ cols = [c[1] for c in cols_info]
83
+ lower = [c.lower().strip() for c in cols]
84
+
85
+ real_ch, real_cat = None, None
86
+ for c, lc in zip(cols, lower):
87
+ if lc == "chapter": real_ch = c
88
+ elif lc == "category": real_cat = c
89
+
90
+ if selection == "ALL":
91
+ cursor.execute(f"SELECT * FROM [{main_t}]")
92
+ elif "." in selection and real_ch and real_cat:
93
+ ch, ca = selection.split(".", 1)
94
+ query = f"""
95
+ SELECT * FROM [{main_t}]
96
+ WHERE REPLACE(TRIM(CAST([{real_ch}] AS TEXT)), ' ', '') = ?
97
+ AND REPLACE(TRIM(CAST([{real_cat}] AS TEXT)), ' ', '') = ?
98
+ """
99
+ cursor.execute(query, (ch.replace(" ", ""), ca.replace(" ", "")))
100
+ else:
101
+ cursor.execute(f"SELECT * FROM [{main_t}]")
102
+
103
+ rows = cursor.fetchall()
104
+ df = pd.DataFrame(rows, columns=cols)
105
+
106
+ # ํ›„์ฒ˜๋ฆฌ: ๋ฉ”์ธ ํ…Œ์ด๋ธ”์€ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด 2์—ด(section, description)๋กœ ์ œํ•œ
107
+ df.columns = [c.lower().strip() for c in df.columns]
108
+ if 'section' in df.columns and 'description' in df.columns:
109
+ df = df[['section', 'description']]
110
+
111
+ for col in df.columns:
112
+ df[col] = df[col].apply(blob_to_base64_html)
113
+
114
+ conn.close()
115
+ return df
116
+
117
+ except Exception as e:
118
+ traceback.print_exc()
119
+ return pd.DataFrame({"Error": [str(e)]})
120
  # --------------------------
121
  def blob_to_base64_html(blob_data):
122
  if blob_data is None or pd.isna(blob_data):
 
303
  return pd.DataFrame({"Info": ["์„ ํƒ ํ•„์š”"]})
304
 
305
  try:
306
+ # ์‹ค์‹œ๊ฐ„ ๊ฒฝ๋กœ ํ™•์ธ์„ ์œ„ํ•ด ์ง์ ‘ ๊ฒฝ๋กœ ์ƒ์„ฑ
307
+ db_path = os.path.join(UPLOAD_DIR, f"{standard}_{version}.db")
308
+ if not os.path.exists(db_path):
309
+ return pd.DataFrame({"Error": [f"ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: {db_path}"]})
310
+
311
+ conn = sqlite3.connect(db_path)
312
  conn.text_factory = safe_text_factory
313
 
314
  tables = pd.read_sql(
 
321
  main_t = tables[0]
322
 
323
  # --------------------------
324
+ # ๐Ÿ”น ๏ฟฝ๏ฟฝ๏ฟฝ์ด๋ธ” ๋ช…์นญ ๋งค์นญ (selection์ด ํ…Œ์ด๋ธ” ๋ช…์นญ์ธ์ง€ ํ™•์ธ)
325
  # --------------------------
326
  full_table_name = None
327
  pattern = re.compile(f"^{standard}[_\\s-]*{version}[_\\s-]*", re.IGNORECASE)
 
336
  break
337
 
338
  # ==========================================
339
+ # ๐ŸŒŸ ๋ถ€์† ํ…Œ์ด๋ธ” ์„ ํƒ ์‹œ (TableA, TableB_C ๋“ฑ)
340
+ # ์›๋ณธ ์—ด ๊ตฌ์กฐ(4์—ด, 5์—ด ๋“ฑ)๋ฅผ ๊ทธ๋Œ€๋กœ ์œ ์ง€
341
  # ==========================================
342
+ if full_table_name and full_table_name != main_t:
343
+ df = pd.read_sql(f"SELECT * FROM [{full_table_name}]", conn)
 
 
 
 
 
 
344
 
345
+ # version ์—ด ์ œ์™ธ ๋ฐ ์ปฌ๋Ÿผ๋ช… ์ •๋ฆฌ
346
+ df = df.drop(columns=[c for c in df.columns if c.lower() == "version"], errors="ignore")
347
+ df.columns = [c.replace("_", " ").title() for c in df.columns]
348
+
349
+ # ๋ชจ๋“  ์…€์— ๋Œ€ํ•ด ์ด๋ฏธ์ง€ ๋ Œ๋”๋ง(BLOB ๋ณ€ํ™˜) ์ ์šฉ
350
  for col in df.columns:
351
  df[col] = df[col].apply(blob_to_base64_html)
352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
  conn.close()
354
  return df
355
 
356
  # --------------------------
357
+ # ๐Ÿ”น ๋ฉ”์ธ ํ…Œ์ด๋ธ” (ALL ๋˜๋Š” ํŠน์ • Category)
358
+ # ๊ธฐ์กด ๊ทœ๊ฒฉ(section, description) ์œ ์ง€
359
  # --------------------------
360
  cursor = conn.cursor()
361
  cursor.execute(f"PRAGMA table_info([{main_t}])")
 
365
 
366
  real_ch, real_cat = None, None
367
  for c, lc in zip(cols, lower):
368
+ if lc == "chapter": real_ch = c
369
+ elif lc == "category": real_cat = c
 
 
370
 
 
 
 
371
  if selection == "ALL":
372
  cursor.execute(f"SELECT * FROM [{main_t}]")
 
 
 
 
373
  elif "." in selection and real_ch and real_cat:
374
  ch, ca = selection.split(".", 1)
375
  query = f"""
 
378
  AND REPLACE(TRIM(CAST([{real_cat}] AS TEXT)), ' ', '') = ?
379
  """
380
  cursor.execute(query, (ch.replace(" ", ""), ca.replace(" ", "")))
 
 
 
 
381
  else:
382
  cursor.execute(f"SELECT * FROM [{main_t}]")
383
 
384
  rows = cursor.fetchall()
385
  df = pd.DataFrame(rows, columns=cols)
386
 
387
+ # ํ›„์ฒ˜๋ฆฌ: ๋ฉ”์ธ ํ…Œ์ด๋ธ”์€ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด 2์—ด(section, description)๋กœ ์ œํ•œ
 
 
388
  df.columns = [c.lower().strip() for c in df.columns]
 
389
  if 'section' in df.columns and 'description' in df.columns:
390
  df = df[['section', 'description']]
391
 
 
396
  return df
397
 
398
  except Exception as e:
 
399
  traceback.print_exc()
400
  return pd.DataFrame({"Error": [str(e)]})
401