Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hcloud/exp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
The `exp` module is a namespace that holds experimental features for the `hcloud-python`
library, breaking changes may occur within minor releases.
"""
35 changes: 35 additions & 0 deletions hcloud/exp/zone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
The `exp.zone` module is a namespace that holds experimental features for the `hcloud-python`
library, breaking changes may occur within minor releases.
"""

from __future__ import annotations


def is_txt_record_quoted(value: str) -> bool:
"""
Check whether a TXT record is already quoted.

- hello world => false
- "hello world" => true
"""
return value.startswith('"') and value.endswith('"')


def format_txt_record(value: str) -> str:
"""
Format a TXT record by splitting it in quoted strings of 255 characters.
Existing quotes will be escaped.

- hello world => "hello world"
- hello "world" => "hello \"world\""
"""
value = value.replace('"', '\\"')

parts = []
for start in range(0, len(value), 255):
end = min(start + 255, len(value))
parts.append('"' + value[start:end] + '"')
value = " ".join(parts)

return value
Empty file added tests/unit/exp/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions tests/unit/exp/test_zone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import annotations

import pytest

from hcloud.exp.zone import format_txt_record, is_txt_record_quoted


@pytest.mark.parametrize(
("value", "expected"),
[
("hello world", False),
('"hello world', False),
('"hello world"', True),
],
)
def test_is_txt_record_quoted(value: str, expected: bool):
assert is_txt_record_quoted(value) == expected


manyA = "a" * 255
someB = "b" * 10


@pytest.mark.parametrize(
("value", "expected"),
[
("", ""),
('""', '"\\"\\""'),
("hello world", '"hello world"'),
("hello\nworld", '"hello\nworld"'),
('hello "world"', '"hello \\"world\\""'),
('hello "world', '"hello \\"world"'),
(manyA + someB, f'"{manyA}" "{someB}"'),
],
)
def test_format_txt_record(value: str, expected: str):
assert format_txt_record(value) == expected