Skip to content

Commit f1f237a

Browse files
committed
feat(exp): add zone txt record helpers
1 parent c6aadce commit f1f237a

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

hcloud/exp/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
The `exp` module is a namespace that holds experimental features for the `hcloud-python`
3+
library, breaking changes may occur within minor releases.
4+
"""

hcloud/exp/zone.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
The `exp.zone` module is a namespace that holds experimental features for the `hcloud-python`
3+
library, breaking changes may occur within minor releases.
4+
"""
5+
6+
from __future__ import annotations
7+
8+
9+
def format_txt_record(value: str) -> str:
10+
"""
11+
Format a TXT record by splitting it in quoted strings of 255 characters.
12+
Existing quotes will be escaped.
13+
14+
- hello world => "hello world"
15+
- hello "world" => "hello \"world\""
16+
"""
17+
value = value.replace('"', '\\"')
18+
19+
parts = []
20+
for start in range(0, len(value), 255):
21+
end = min(start + 255, len(value))
22+
parts.append('"' + value[start:end] + '"')
23+
value = " ".join(parts)
24+
25+
return value

tests/unit/exp/__init__.py

Whitespace-only changes.

tests/unit/exp/test_zone.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import annotations
2+
3+
from datetime import timedelta
4+
5+
import pytest
6+
7+
from hcloud.exp.zone import format_txt_record
8+
9+
manyA = "a" * 255
10+
someB = "b" * 10
11+
12+
13+
@pytest.mark.parametrize(
14+
("value", "expected"),
15+
[
16+
("", ""),
17+
('""', '"\\"\\""'),
18+
("hello world", '"hello world"'),
19+
("hello\nworld", '"hello\nworld"'),
20+
('hello "world"', '"hello \\"world\\""'),
21+
('hello "world', '"hello \\"world"'),
22+
(manyA + someB, f'"{manyA}" "{someB}"'),
23+
],
24+
)
25+
def test_parse_duration(value: str, expected: timedelta):
26+
assert format_txt_record(value) == expected

0 commit comments

Comments
 (0)