File size: 11,373 Bytes
25d2b3e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
وحدة المساعدة المركزية للنظام
تحتوي على دوال مساعدة مشتركة تستخدم في جميع أنحاء التطبيق
"""
import os
import sys
import streamlit as st
import pandas as pd
import numpy as np
import json
import re
import time
import tempfile
from datetime import datetime, timedelta
import random
import secrets
import shutil
import base64
import logging
from pathlib import Path
# تكوين التسجيلات
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger("wahbi-ai")
def create_directory_if_not_exists(directory_path):
"""إنشاء مسار إذا لم يكن موجوداً"""
try:
if not os.path.exists(directory_path):
os.makedirs(directory_path)
logger.info(f"تم إنشاء المجلد: {directory_path}")
return True
except Exception as e:
logger.error(f"خطأ في إنشاء المجلد {directory_path}: {e}")
return False
def get_data_folder():
"""الحصول على مسار مجلد البيانات الرئيسي"""
# مسار بيانات النظام الرئيسي
data_folder = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data")
create_directory_if_not_exists(data_folder)
return data_folder
def load_config():
"""تحميل إعدادات التكوين من ملف config.json"""
config_path = os.path.join(get_data_folder(), "config.json")
# إذا لم يكن ملف التكوين موجوداً، قم بإنشاء ملف افتراضي
if not os.path.exists(config_path):
default_config = {
"system": {
"version": "1.0.0",
"release_date": "2025-03-30",
"company_name": "شركة شبه الجزيرة للمقاولات",
"company_logo": "",
"language": "ar",
"theme": "light",
"debug_mode": False
},
"ai_models": {
"default_model": "claude-3-7-sonnet-20250219",
"openai_model": "gpt-4o",
"huggingface_model": "mistralai/Mistral-7B-Instruct-v0.2"
},
"notifications": {
"enable_email": False,
"enable_browser": True,
"check_interval": 60
}
}
with open(config_path, 'w', encoding='utf-8') as f:
json.dump(default_config, f, ensure_ascii=False, indent=2)
return default_config
# تحميل ملف التكوين الموجود
try:
with open(config_path, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
logger.error(f"خطأ في تحميل ملف التكوين: {e}")
return {}
def save_config(config):
"""حفظ إعدادات التكوين إلى ملف config.json"""
config_path = os.path.join(get_data_folder(), "config.json")
try:
with open(config_path, 'w', encoding='utf-8') as f:
json.dump(config, f, ensure_ascii=False, indent=2)
return True
except Exception as e:
logger.error(f"خطأ في حفظ ملف التكوين: {e}")
return False
def format_time(timestamp=None, format_str="%Y-%m-%d %H:%M:%S"):
"""تنسيق الوقت إلى صيغة معينة"""
if timestamp is None:
timestamp = datetime.now()
elif isinstance(timestamp, (int, float)):
timestamp = datetime.fromtimestamp(timestamp)
return timestamp.strftime(format_str)
def get_user_info():
"""الحصول على معلومات المستخدم الحالي"""
# في التطبيق الفعلي، يمكن استرداد معلومات المستخدم من قاعدة البيانات أو من حالة الجلسة
if "user_info" in st.session_state:
return st.session_state.user_info
# معلومات افتراضية للتطوير
return {
"id": 1,
"username": "admin",
"full_name": "مدير النظام",
"email": "[email protected]",
"role": "مدير",
"department": "الإدارة",
"last_login": format_time()
}
def get_current_project():
"""الحصول على معلومات المشروع الحالي"""
if "current_project" in st.session_state:
return st.session_state.current_project
# في حالة عدم وجود مشروع محدد
return None
def load_icons():
"""تحميل الأيقونات المستخدمة في النظام"""
icons_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "assets", "icons")
icons = {}
# التأكد من وجود مجلد الأيقونات
if not os.path.exists(icons_path):
create_directory_if_not_exists(icons_path)
return icons
# تحميل جميع الأيقونات
for icon_file in os.listdir(icons_path):
if icon_file.endswith(('.png', '.svg', '.jpg')):
icon_name = os.path.splitext(icon_file)[0]
icon_path = os.path.join(icons_path, icon_file)
try:
with open(icon_path, "rb") as f:
icons[icon_name] = base64.b64encode(f.read()).decode()
except Exception as e:
logger.error(f"خطأ في تحميل الأيقونة {icon_name}: {e}")
return icons
def get_random_id(length=8):
"""إنشاء معرف عشوائي بطول محدد"""
return secrets.token_hex(length // 2)
def compress_text(text, max_length=10000):
"""اختصار النص إلى حد أقصى محدد مع الحفاظ على المعنى"""
if not text or len(text) <= max_length:
return text
# تقسيم النص إلى جمل
sentences = re.split(r'(?<=[.!?])\s+', text)
# حساب متوسط طول الجملة
avg_sentence_length = len(text) / len(sentences)
# حساب عدد الجمل التي يمكن تضمينها
num_sentences_to_keep = int(max_length / avg_sentence_length)
# الاحتفاظ بالجمل الأولى والأخيرة للحفاظ على السياق
keep_first = num_sentences_to_keep // 2
keep_last = num_sentences_to_keep - keep_first
# دمج الجمل المختارة
compressed_text = ' '.join(sentences[:keep_first] + sentences[-keep_last:])
# إضافة إشارة إلى أن النص تم اختصاره
if len(compressed_text) < len(text):
compressed_text += " [...المزيد من النص تم اختصاره...]"
return compressed_text
def str_to_bool(text):
"""تحويل النص إلى قيمة منطقية"""
return text.lower() in ('yes', 'true', 'y', 't', '1', 'نعم', 'صحيح')
def handle_arabic_text(text):
"""معالجة النص العربي للعرض بشكل صحيح"""
if not text:
return ""
# إضافة علامة RTL لضمان عرض النص العربي بشكل صحيح
return f"<div dir='rtl'>{text}</div>"
def render_credits():
"""عرض معلومات النظام وحقوق الملكية"""
st.markdown("---")
config = load_config()
system_info = config.get("system", {})
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
st.markdown(
f"""
<div style="text-align: center; color: #666;">
<p>{system_info.get('company_name', 'شركة شبه الجزيرة للمقاولات')}</p>
<p>الإصدار: {system_info.get('version', '1.0.0')}</p>
<p>© جميع الحقوق محفوظة 2025</p>
</div>
""",
unsafe_allow_html=True
)
# دالة للحصول على اتصال قاعدة البيانات
def get_connection():
"""
دالة للحصول على اتصال بقاعدة البيانات
ملاحظة: ينبغي أن تكون مستبدلة بالدالة من db_connector.py في البيئة الإنتاجية
"""
try:
# استيراد المستوى الفعلي للاتصال بقاعدة البيانات
from database.db_connector import get_connection as get_db_connection
return get_db_connection()
except ImportError:
# إذا كان الاتصال بقاعدة البيانات غير متاح
logger.warning("لم يتم العثور على وحدة اتصال قاعدة البيانات. استخدام مخزن بيانات مؤقت.")
# إرجاع None للإشارة إلى عدم وجود اتصال
return None
def load_css(file_name=None):
"""تحميل ملف CSS مخصص"""
try:
if file_name:
css_file = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "assets", "css", file_name)
if os.path.exists(css_file):
with open(css_file, "r", encoding="utf-8") as f:
css_content = f.read()
else:
logger.warning(f"ملف CSS غير موجود: {css_file}")
return
else:
# CSS افتراضي
css_content = """
.sidebar .sidebar-content {
direction: rtl;
text-align: right;
}
div[data-testid="stForm"] {
border: 1px solid #ddd;
padding: 10px;
border-radius: 10px;
}
.module-title {
color: #1E88E5;
text-align: center;
font-size: 1.8rem;
margin-bottom: 1rem;
}
.instructions {
background-color: #f8f9fa;
border-right: 3px solid #4CAF50;
padding: 10px;
margin-bottom: 15px;
}
.results-container {
background-color: #f5f5f5;
padding: 15px;
border-radius: 5px;
margin-top: 20px;
}
.risk-high {
color: #d32f2f;
font-weight: bold;
}
.risk-medium {
color: #f57c00;
font-weight: bold;
}
.risk-low {
color: #388e3c;
font-weight: bold;
}
.form-container {
background-color: #f9f9f9;
padding: 20px;
border-radius: 10px;
margin-bottom: 20px;
}
.section-header {
color: #2196F3;
font-size: 1.2rem;
font-weight: bold;
margin-top: 20px;
margin-bottom: 10px;
border-bottom: 1px solid #eee;
padding-bottom: 5px;
}
"""
st.markdown(f"<style>{css_content}</style>", unsafe_allow_html=True)
except Exception as e:
logger.error(f"خطأ في تحميل ملف CSS: {e}") |