-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to parse multiple
Address
and Via
from a string
- Loading branch information
Showing
5 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import typing | ||
|
||
from . import grammar | ||
|
||
T = typing.TypeVar("T") | ||
|
||
|
||
def parse_many(parser: typing.Callable[[str], tuple[T, str]], value: str) -> list[T]: | ||
value = grammar.simplify_whitespace(value) | ||
|
||
items: list[T] = [] | ||
while value: | ||
item, value = parser(value) | ||
items.append(item) | ||
|
||
# Consume list separator(s). | ||
while value.startswith(","): | ||
value = value[1:].lstrip() | ||
|
||
return items | ||
|
||
|
||
def parse_single(parser: typing.Callable[[str], tuple[T, str]], value: str) -> T: | ||
value = grammar.simplify_whitespace(value) | ||
|
||
item, value = parser(value) | ||
if value: | ||
raise ValueError("Header has trailing data") | ||
|
||
return item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,3 +200,23 @@ def test_rfc4475_intmeth_to(self) -> None: | |
'"BEL:\x07 NUL:\x00 DEL:\x7f" ' | ||
"<sip:1_unusual.URI~(to-be!sure)&isn't+it$/crazy?,/;;*@example.com>", | ||
) | ||
|
||
def test_trailing_comma(self) -> None: | ||
with self.assertRaises(ValueError) as cm: | ||
Address.parse("<sip:1.2.3.4;lr>,") | ||
self.assertEqual(str(cm.exception), "Header has trailing data") | ||
|
||
|
||
class AddressParseManyTest(unittest.TestCase): | ||
def test_simple(self) -> None: | ||
contacts = Address.parse_many( | ||
"<sip:[email protected]>, <sip:[email protected]>, <sip:[email protected]>" | ||
) | ||
self.assertEqual( | ||
contacts, | ||
[ | ||
Address(uri=URI(scheme="sip", host="atlanta.com", user="alice")), | ||
Address(uri=URI(scheme="sip", host="biloxi.com", user="bob")), | ||
Address(uri=URI(scheme="sip", host="chicago.com", user="carol")), | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters