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"