utils module
פונקציות עזר כלליות לבוט שומר קבצי קוד General Utility Functions for Code Keeper Bot
- class utils.CodeErrorLogger[מקור]
Bases:
objectמערכת לוגים ייעודית לשגיאות עיבוד קוד
- log_code_processing_error(user_id, error_type, error_message, context=None)[מקור]
רישום שגיאות עיבוד קוד
- class utils.TimeUtils[מקור]
Bases:
objectכלים לעבודה עם זמן ותאריכים
- class utils.TextUtils[מקור]
Bases:
objectכלים לעבודה עם טקסט
- class utils.SecurityUtils[מקור]
Bases:
objectכלים אמינות ובטיחות
- class utils.TelegramUtils[מקור]
Bases:
objectכלים לעבודה עם Telegram
- async static send_typing_action(update, context)[מקור]
שליחת אקשן »כותב…«
- פרמטרים:
update (telegram.Update)
context (telegram.ext.ContextTypes.DEFAULT_TYPE)
- async static send_document_action(update, context)[מקור]
שליחת אקשן »שולח מסמך…«
- פרמטרים:
update (telegram.Update)
context (telegram.ext.ContextTypes.DEFAULT_TYPE)
- async static safe_answer(query, text=None, show_alert=False, cache_time=None)[מקור]
מענה בטוח ל-CallbackQuery: מתעלם משגיאות »Query is too old«/«query_id_invalid«.
- static get_user_mention(user)[מקור]
קבלת מנשן למשתמש
- Return type:
- פרמטרים:
user (telegram.User)
- async static safe_edit_message_text(query, text, reply_markup=None, parse_mode=None)[מקור]
עריכת טקסט הודעה בבטיחות: מתעלם משגיאת »Message is not modified«.
תומך גם במימושי בדיקות שבהם edit_message_text היא פונקציה סינכרונית שמחזירה None (לא awaitable), וגם במימושים אסינכרוניים רגילים.
- async static safe_edit_message_reply_markup(query, reply_markup=None)[מקור]
עריכת מקלדת הודעה בבטיחות: מתעלם משגיאת »Message is not modified«.
- Return type:
- static extract_message_text_preserve_markdown(message, *, reconstruct_from_entities=True)[מקור]
שחזור טקסט ההודעה תוך ניסיון להחזיר את מה שהמשתמש התכוון מבחינת תווי Markdown.
עקרונות:
ברירת מחדל: נשתמש ב-
text/caption(התוכן הגולמי כפי שנשלח לשרת לאחר עיבוד Markdown). זאת כדי לא לשמור מחרוזת ”מרונדרת“ (למשל*_name_*), שאינה משקפת קלט משתמש.במידה ו-
reconstruct_from_entities=Trueויש ישויות עיצוב (bold/italic), ננסה לשחזר תווי Markdown שהיוו כנראה את מקור העיצוב ע“י הוספת תחיליות/סיומות סביב הטקסט שסומן. מיפוי פשוט:bold→"__",italic→"_". שאר ישויות נשמרות כפי שהן.אם יש כיתוב (caption), נשתמש במקבילות
caption_entities.
- Return type:
- פרמטרים:
message (telegram.Message)
reconstruct_from_entities (bool)
- class utils.CallbackQueryGuard[מקור]
Bases:
objectGuard גורף ללחיצות כפולות על כפתורי CallbackQuery.
מבוסס על טביעת אצבע של המשתמש/הודעה/הנתון (callback_data) כדי לחסום את אותה פעולה בחלון זמן קצר, בלי לחסום פעולות שונות.
- static should_block(update, context, window_seconds=None)[מקור]
בודק האם יש לחסום את העדכון כלחיצה כפולה.
אם זו אותה טביעת אצבע בתוך חלון הזמן – נחסום; אחרת נסמן ונאפשר.
- Return type:
- פרמטרים:
update (telegram.Update)
context (telegram.ext.ContextTypes.DEFAULT_TYPE)
window_seconds (float | None)
- async static should_block_async(update, context, window_seconds=None)[מקור]
בודק בצורה אטומית (עם נעילה) אם לחסום לחיצה כפולה של אותו משתמש.
חסימה מבוססת חלון זמן פר-משתמש ועל אותה טביעת אצבע (משתמש/צ׳אט/הודעה/הנתון), כדי למנוע כפילויות אמיתיות בלי לחסום לחיצות שונות ברצף מהיר (UX).
- Return type:
- פרמטרים:
update (telegram.Update)
context (telegram.ext.ContextTypes.DEFAULT_TYPE)
window_seconds (float | None)
- class utils.AsyncUtils[מקור]
Bases:
objectכלים לעבודה אסינכרונית
- class utils.ValidationUtils[מקור]
Bases:
objectכלים לוולידציה
- class utils.FileUtils[מקור]
Bases:
objectכלים לעבודה עם קבצים
- class utils.ConfigUtils[מקור]
Bases:
objectכלים לקונפיגורציה
- class utils.CacheUtils[מקור]
Bases:
objectכלים לקאש זמני
- utils.detect_language_from_filename(filename)[מקור]
זיהוי שפת תכנות לפי סיומת הקובץ (Filename-only). נסה קודם את הדטקטור הדומייני כדי לשמור אחידות, ונפילה למיפוי מקומי לתאימות.
- class utils.SensitiveDataFilter(name='')[מקור]
Bases:
Filterמסנן שמטשטש טוקנים ונתונים רגישים בלוגים.
- utils.normalize_code(text, *, strip_bom=True, normalize_newlines=True, replace_nbsp=True, replace_all_space_separators=True, remove_zero_width=True, remove_directional_marks=True, trim_trailing_whitespace=True, remove_other_format_chars=True, remove_escaped_format_escapes=True, remove_variation_selectors=False)[מקור]
נרמול קוד לפני שמירה.
פעולות עיקריות:
הסרת BOM בתחילת הטקסט
המרת CRLF/CR ל-LF
החלפת רווחים לא-שוברים (NBSP/NNBSP) לרווח רגיל
הסרת תווי רוחב-אפס וסימוני כיוון (LRM/RLM/LRE/RLE/PDF/RLO/LRO/LRI/RLI/FSI/PDI)
הסרת תווי בקרה (Cc) פרט ל-t, n, r
הסרת רווחי סוף שורה
- Return type:
- פרמטרים:
text (str)
strip_bom (bool)
normalize_newlines (bool)
replace_nbsp (bool)
replace_all_space_separators (bool)
remove_zero_width (bool)
remove_directional_marks (bool)
trim_trailing_whitespace (bool)
remove_other_format_chars (bool)
remove_escaped_format_escapes (bool)
remove_variation_selectors (bool)