initial commit
This commit is contained in:
99
utils/wifi_qr.py
Normal file
99
utils/wifi_qr.py
Normal file
@@ -0,0 +1,99 @@
|
||||
"""WiFi QR code generation utilities."""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class WiFiQRCode:
|
||||
"""Generate WiFi QR code strings in the standard format."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
ssid: str,
|
||||
password: str,
|
||||
security: str = "WPA",
|
||||
hidden: bool = False
|
||||
):
|
||||
"""
|
||||
Initialize WiFi QR code generator.
|
||||
|
||||
Args:
|
||||
ssid: WiFi network name (SSID)
|
||||
password: WiFi password
|
||||
security: Security type (WPA, WEP, or nopass)
|
||||
hidden: Whether the network is hidden
|
||||
"""
|
||||
self.ssid = ssid
|
||||
self.password = password
|
||||
self.security = security.upper()
|
||||
self.hidden = hidden
|
||||
|
||||
@staticmethod
|
||||
def _escape_special_chars(text: str) -> str:
|
||||
"""
|
||||
Escape special characters for WiFi QR code format.
|
||||
|
||||
Special characters that need escaping: \ ; , " :
|
||||
|
||||
Args:
|
||||
text: Text to escape
|
||||
|
||||
Returns:
|
||||
Escaped text
|
||||
"""
|
||||
# Characters that need to be escaped with backslash
|
||||
special_chars = ['\\', ';', ',', '"', ':']
|
||||
|
||||
result = text
|
||||
for char in special_chars:
|
||||
result = result.replace(char, f'\\{char}')
|
||||
|
||||
return result
|
||||
|
||||
def generate_wifi_string(self) -> str:
|
||||
"""
|
||||
Generate WiFi QR code string in standard format.
|
||||
|
||||
Format: WIFI:T:<WPA/WEP/nopass>;S:<SSID>;P:<password>;H:<true/false>;;
|
||||
|
||||
Returns:
|
||||
Formatted WiFi QR code string
|
||||
"""
|
||||
# Escape special characters in SSID and password
|
||||
escaped_ssid = self._escape_special_chars(self.ssid)
|
||||
escaped_password = self._escape_special_chars(self.password)
|
||||
|
||||
# Build WiFi string
|
||||
wifi_string = f"WIFI:T:{self.security};"
|
||||
wifi_string += f"S:{escaped_ssid};"
|
||||
|
||||
# Only add password if security is not 'nopass'
|
||||
if self.security.upper() != "NOPASS":
|
||||
wifi_string += f"P:{escaped_password};"
|
||||
|
||||
# Add hidden flag
|
||||
wifi_string += f"H:{'true' if self.hidden else 'false'};;"
|
||||
|
||||
return wifi_string
|
||||
|
||||
@staticmethod
|
||||
def validate_security_type(security: str) -> bool:
|
||||
"""
|
||||
Validate security type.
|
||||
|
||||
Args:
|
||||
security: Security type to validate
|
||||
|
||||
Returns:
|
||||
True if valid, False otherwise
|
||||
"""
|
||||
valid_types = ["WPA", "WEP", "NOPASS"]
|
||||
return security.upper() in valid_types
|
||||
|
||||
def to_string(self) -> str:
|
||||
"""
|
||||
Convert WiFi credentials to QR code string.
|
||||
|
||||
Returns:
|
||||
WiFi QR code string
|
||||
"""
|
||||
return self.generate_wifi_string()
|
||||
Reference in New Issue
Block a user