Spaces:
Sleeping
Sleeping
Create dora_ict_asset_register.py
Browse files- tools/dora_ict_asset_register.py +20 -24
tools/dora_ict_asset_register.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
#
|
| 2 |
import datetime
|
| 3 |
import re
|
| 4 |
from fpdf import FPDF
|
|
@@ -18,7 +18,7 @@ def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
|
|
| 18 |
|
| 19 |
pdf.set_font("Arial", 'B', 16)
|
| 20 |
pdf.set_text_color(0, 51, 102)
|
| 21 |
-
title = "ICT Asset Register - DORA" if language == "en" else "Registre des
|
| 22 |
pdf.cell(0, 15, title, ln=True, align='C')
|
| 23 |
pdf.ln(10)
|
| 24 |
|
|
@@ -33,6 +33,7 @@ def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
|
|
| 33 |
pdf.set_font("Arial", '', 12)
|
| 34 |
pdf.set_text_color(0, 0, 0)
|
| 35 |
for line in text.strip().split('\n'):
|
|
|
|
| 36 |
if line.startswith("## "):
|
| 37 |
section = line.replace("## ", "").strip()
|
| 38 |
pdf.set_font("Arial", 'B', 13)
|
|
@@ -44,27 +45,27 @@ def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
|
|
| 44 |
elif line.startswith("- **"):
|
| 45 |
match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
|
| 46 |
if match:
|
| 47 |
-
label,
|
| 48 |
pdf.set_font("Arial", 'B', 12)
|
| 49 |
pdf.cell(0, 10, f"{label}:", ln=True)
|
| 50 |
pdf.set_font("Arial", '', 12)
|
| 51 |
-
pdf.multi_cell(0, 10,
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
pdf.multi_cell(0, 10, line)
|
| 54 |
pdf.output(output_path)
|
| 55 |
return output_path
|
| 56 |
|
| 57 |
-
|
| 58 |
# === Questions ===
|
| 59 |
QUESTIONS = prepend_metadata_questions([
|
| 60 |
-
("
|
| 61 |
-
("
|
| 62 |
-
("location", "Where
|
| 63 |
-
("
|
| 64 |
-
("
|
| 65 |
-
("
|
| 66 |
-
("maintenance", "What is the maintenance or update policy?"),
|
| 67 |
-
("retirement", "What is the asset's planned retirement date or process?")
|
| 68 |
])
|
| 69 |
|
| 70 |
|
|
@@ -88,13 +89,6 @@ def run_tool():
|
|
| 88 |
state["step"] += 1
|
| 89 |
return next_question, state, None
|
| 90 |
|
| 91 |
-
# Safe language detection
|
| 92 |
-
try:
|
| 93 |
-
content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
|
| 94 |
-
detected_lang = detect(content) if len(content.strip()) > 10 else "en"
|
| 95 |
-
except:
|
| 96 |
-
detected_lang = "en"
|
| 97 |
-
|
| 98 |
metadata = {
|
| 99 |
"user_name": answers.get("user_name", "N/A"),
|
| 100 |
"user_role": answers.get("user_role", "N/A"),
|
|
@@ -102,11 +96,13 @@ def run_tool():
|
|
| 102 |
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 103 |
}
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
with gr.Blocks(title="DORA
|
| 109 |
-
chatbot = gr.Chatbot(label="
|
| 110 |
msg = gr.Textbox(label="Your answer")
|
| 111 |
state_var = gr.State(state)
|
| 112 |
file_output = gr.File(label="Download PDF")
|
|
|
|
| 1 |
+
# dora_ict_asset_register.py
|
| 2 |
import datetime
|
| 3 |
import re
|
| 4 |
from fpdf import FPDF
|
|
|
|
| 18 |
|
| 19 |
pdf.set_font("Arial", 'B', 16)
|
| 20 |
pdf.set_text_color(0, 51, 102)
|
| 21 |
+
title = "ICT Asset Register - DORA" if language == "en" else "Registre des Actifs TIC - DORA"
|
| 22 |
pdf.cell(0, 15, title, ln=True, align='C')
|
| 23 |
pdf.ln(10)
|
| 24 |
|
|
|
|
| 33 |
pdf.set_font("Arial", '', 12)
|
| 34 |
pdf.set_text_color(0, 0, 0)
|
| 35 |
for line in text.strip().split('\n'):
|
| 36 |
+
line = line.strip()
|
| 37 |
if line.startswith("## "):
|
| 38 |
section = line.replace("## ", "").strip()
|
| 39 |
pdf.set_font("Arial", 'B', 13)
|
|
|
|
| 45 |
elif line.startswith("- **"):
|
| 46 |
match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
|
| 47 |
if match:
|
| 48 |
+
label, value = match.groups()
|
| 49 |
pdf.set_font("Arial", 'B', 12)
|
| 50 |
pdf.cell(0, 10, f"{label}:", ln=True)
|
| 51 |
pdf.set_font("Arial", '', 12)
|
| 52 |
+
pdf.multi_cell(0, 10, value)
|
| 53 |
+
elif line == "---":
|
| 54 |
+
pdf.line(10, pdf.get_y(), 200, pdf.get_y())
|
| 55 |
+
pdf.ln(5)
|
| 56 |
else:
|
| 57 |
pdf.multi_cell(0, 10, line)
|
| 58 |
pdf.output(output_path)
|
| 59 |
return output_path
|
| 60 |
|
|
|
|
| 61 |
# === Questions ===
|
| 62 |
QUESTIONS = prepend_metadata_questions([
|
| 63 |
+
("ict_assets", "List all ICT assets supporting critical or important functions."),
|
| 64 |
+
("ownership", "Who owns and manages each asset?"),
|
| 65 |
+
("location", "Where are the assets physically or virtually located?"),
|
| 66 |
+
("configuration", "Provide key configuration or setup details."),
|
| 67 |
+
("maintenance_schedule", "What is the maintenance or review schedule?"),
|
| 68 |
+
("security_measures", "What security controls are in place for each asset?")
|
|
|
|
|
|
|
| 69 |
])
|
| 70 |
|
| 71 |
|
|
|
|
| 89 |
state["step"] += 1
|
| 90 |
return next_question, state, None
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
metadata = {
|
| 93 |
"user_name": answers.get("user_name", "N/A"),
|
| 94 |
"user_role": answers.get("user_role", "N/A"),
|
|
|
|
| 96 |
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 97 |
}
|
| 98 |
|
| 99 |
+
content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
|
| 100 |
+
lang = detect(content if len(content.strip()) > 3 else "This is a placeholder text.")
|
| 101 |
+
pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
|
| 102 |
+
return "✅ Register created successfully. Download your PDF below.", {"done": True}, pdf_path
|
| 103 |
|
| 104 |
+
with gr.Blocks(title="DORA ICT Asset Register") as demo:
|
| 105 |
+
chatbot = gr.Chatbot(label="💻 ICT Asset Register Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
|
| 106 |
msg = gr.Textbox(label="Your answer")
|
| 107 |
state_var = gr.State(state)
|
| 108 |
file_output = gr.File(label="Download PDF")
|