File tree Expand file tree Collapse file tree 4 files changed +65
-0
lines changed
Expand file tree Collapse file tree 4 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 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+ """
Original file line number Diff line number Diff line change 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 is_txt_record_quoted (value : str ) -> bool :
10+ """
11+ Check whether a TXT record is already quoted.
12+
13+ - hello world => false
14+ - "hello world" => true
15+ """
16+ return value .startswith ('"' ) and value .endswith ('"' )
17+
18+
19+ def format_txt_record (value : str ) -> str :
20+ """
21+ Format a TXT record by splitting it in quoted strings of 255 characters.
22+ Existing quotes will be escaped.
23+
24+ - hello world => "hello world"
25+ - hello "world" => "hello \" world\" "
26+ """
27+ value = value .replace ('"' , '\\ "' )
28+
29+ parts = []
30+ for start in range (0 , len (value ), 255 ):
31+ end = min (start + 255 , len (value ))
32+ parts .append ('"' + value [start :end ] + '"' )
33+ value = " " .join (parts )
34+
35+ return value
Original file line number Diff line number Diff line change 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\n world" , '"hello\n world"' ),
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
You can’t perform that action at this time.
0 commit comments