From 64e0b7f5ce0474d5f3f30e9b725ec5e7386588b4 Mon Sep 17 00:00:00 2001 From: zailon Date: Tue, 23 Jun 2026 15:33:57 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20docs/services/lxc208-manage/alert/utils.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/services/lxc208-manage/alert/utils.py | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 docs/services/lxc208-manage/alert/utils.py diff --git a/docs/services/lxc208-manage/alert/utils.py b/docs/services/lxc208-manage/alert/utils.py new file mode 100644 index 0000000..4be30ce --- /dev/null +++ b/docs/services/lxc208-manage/alert/utils.py @@ -0,0 +1,54 @@ +import re +import html +from typing import Optional + + +def escape_html(text: str) -> str: + if text is None: + return "" + return html.escape(str(text)) + + +def strip_html(text: str) -> str: + return re.sub("<[^>]+>", "", text) + + +def safe_int(value) -> Optional[int]: + try: + return int(float(value)) + except (TypeError, ValueError): + return None + + +def format_size(size_bytes) -> str: + if size_bytes is None: + return "" + + try: + size_bytes = float(size_bytes) + except (TypeError, ValueError): + return str(size_bytes) + + units = ["B", "KB", "MB", "GB", "TB", "PB"] + unit_index = 0 + + while size_bytes >= 1024 and unit_index < len(units) - 1: + size_bytes /= 1024 + unit_index += 1 + + if unit_index == 0: + return f"{int(size_bytes)} {units[unit_index]}" + else: + return f"{size_bytes:.1f} {units[unit_index]}" + + +def plural_sectors(n: int) -> str: + n = abs(int(n)) + if 11 <= n % 100 <= 19: + return "\u043f\u043e\u0432\u0440\u0435\u0436\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u0435\u043a\u0442\u043e\u0440\u043e\u0432" + last = n % 10 + if last == 1: + return "\u043f\u043e\u0432\u0440\u0435\u0436\u0434\u0435\u043d\u043d\u044b\u0439 \u0441\u0435\u043a\u0442\u043e\u0440" + if 2 <= last <= 4: + return "\u043f\u043e\u0432\u0440\u0435\u0436\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u0435\u043a\u0442\u043e\u0440\u0430" + return "\u043f\u043e\u0432\u0440\u0435\u0436\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u0435\u043a\u0442\u043e\u0440\u043e\u0432" \ No newline at end of file