Utils - RFC1034 Compliance
smarter.common.utils.rfc1034_compliance
Helpers for generating and converting RFC 1034-compliant strings.
This module provides utility functions for working with DNS-safe names and resource identifiers that comply with RFC 1034. It includes:
rfc1034_compliant_str: Converts arbitrary strings to RFC 1034-compliant DNS labels.rfc1034_compliant_to_snake: Converts RFC 1034-compliant names to Pythonicsnake_case.
Example usage:
from smarter.common.utils import rfc1034_compliant_str, rfc1034_compliant_to_snake
label = rfc1034_compliant_str("My_ChatBot_2025")
print(label) # Output: my-chatbot-2025
snake = rfc1034_compliant_to_snake(label)
print(snake) # Output: my_chatbot_2025
- smarter.common.utils.rfc1034_compliance.rfc1034_compliant_str(val)[source]
Generates a RFC 1034-compliant name string suitable for use as a DNS label or resource identifier.
- Parameters:
val (str) – The input string to convert to RFC 1034-compliant format.
- Returns:
A string that is: - lower case - contains only alphanumeric characters and hyphens - starts and ends with an alphanumeric character - has a maximum length of 63 characters
- Return type:
- Raises:
SmarterValueError – If the input is not a string or is empty after conversion.
Note
Underscores in the input are replaced with hyphens.
Invalid characters (anything other than a-z, 0-9, or ‘-’) are removed.
Leading and trailing hyphens are stripped.
The result is truncated to 63 characters if necessary.
Warning
This function is intended for generating DNS-safe names. It does not guarantee uniqueness or suitability for all RFC 1034 use cases.
Example usage:
from smarter.common.utils import rfc1034_compliant_str # Basic usage print(rfc1034_compliant_str("My_ChatBot_2025")) # Output: my-chatbot-2025 # With special characters print(rfc1034_compliant_str("My@Bot!_Name")) # Output: my-bot-name # With long input long_name = "ThisIsAReallyLongChatBotNameThatShouldBeTruncatedToSixtyThreeCharacters_Extra" print(rfc1034_compliant_str(long_name)) # Output: thisisareallylongchatbotnamethatshouldbetruncatedtosixtythreecharacters
- smarter.common.utils.rfc1034_compliance.rfc1034_compliant_to_snake(val)[source]
Converts a RFC 1034-compliant name (typically used for DNS labels or resource identifiers) to a more human-readable
snake_casename.This function is useful for translating machine-friendly names (which use hyphens as word separators) into Pythonic identifiers (which use underscores).
- Parameters:
val (str) – The RFC 1034-compliant name to convert. This should be a string containing only lowercase letters, numbers, and hyphens.
- Returns:
The converted name in
snake_caseformat, with hyphens replaced by underscores.- Return type:
- Raises:
SmarterValueError – If the input is not a string.
Note
Only hyphens are replaced; other characters are preserved.
The function does not validate that the input is strictly RFC 1034-compliant. It assumes the input is already sanitized.
Warning
This function does not handle conversion of other non-alphanumeric characters. If the input contains characters other than hyphens, underscores, letters, or numbers, they will remain unchanged.
Example usage:
from smarter.common.utils import rfc1034_compliant_to_snake # Basic conversion print(rfc1034_compliant_to_snake("my-chatbot-2025")) # Output: my_chatbot_2025 # Input with no hyphens print(rfc1034_compliant_to_snake("simplelabel")) # Output: simplelabel # Input with multiple hyphens print(rfc1034_compliant_to_snake("this-is-a-test-label")) # Output: this_is_a_test_label # Input with invalid type try: rfc1034_compliant_to_snake(12345) except SmarterValueError as e: print(e) # Output: Could not convert RFC 1034 compliant name from <class 'int'>