Skip to content

Commit

Permalink
Merge strict and allow_underscores
Browse files Browse the repository at this point in the history
Those really serve the same purpose -- are we receiving data (and want
to be flexible) or registering services (and want to be strict).
  • Loading branch information
jstasiak committed Oct 13, 2020
1 parent 6ab0cd0 commit 73b5980
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions zeroconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def _encode_address(address: str) -> bytes:
return socket.inet_pton(address_family, address)


def service_type_name(type_: str, *, allow_underscores: bool = False, strict: bool = True) -> str:
def service_type_name(type_: str, *, strict: bool = True) -> str:
"""
Validate a fully qualified service name, instance or subtype. [rfc6763]
Expand Down Expand Up @@ -322,14 +322,13 @@ def service_type_name(type_: str, *, allow_underscores: bool = False, strict: bo
)

allowed_characters_re = (
_HAS_ONLY_A_TO_Z_NUM_HYPHEN_UNDERSCORE if allow_underscores else _HAS_ONLY_A_TO_Z_NUM_HYPHEN
_HAS_ONLY_A_TO_Z_NUM_HYPHEN if strict else _HAS_ONLY_A_TO_Z_NUM_HYPHEN_UNDERSCORE
)

if not allowed_characters_re.search(test_service_name):
raise BadTypeInNameException(
"Service name (%s) must contain only these characters: "
"A-Z, a-z, 0-9, hyphen ('-')%s"
% (test_service_name, ", underscore ('_')" if allow_underscores else "")
"A-Z, a-z, 0-9, hyphen ('-')%s" % (test_service_name, "" if strict else ", underscore ('_')")
)
else:
service_name = ''
Expand Down Expand Up @@ -1564,7 +1563,7 @@ def __init__(
assert handlers or listener, 'You need to specify at least one handler'
self.types = set(type_ if isinstance(type_, list) else [type_])
for check_type_ in self.types:
if not check_type_.endswith(service_type_name(check_type_, allow_underscores=True)):
if not check_type_.endswith(service_type_name(check_type_, strict=False)):
raise BadTypeInNameException
threading.Thread.__init__(self)
self.daemon = True
Expand Down Expand Up @@ -1796,7 +1795,7 @@ def __init__(
# Accept both none, or one, but not both.
if addresses is not None and parsed_addresses is not None:
raise TypeError("addresses and parsed_addresses cannot be provided together")
if not type_.endswith(service_type_name(name, strict=False, allow_underscores=True)):
if not type_.endswith(service_type_name(name, strict=False)):
raise BadTypeInNameException
self.type = type_
self.name = name
Expand Down
2 changes: 1 addition & 1 deletion zeroconf/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ def test_good_service_names(self):
for name, result in good_names_to_try:
assert r.service_type_name(name) == result

assert r.service_type_name('_one_two._tcp.local.', allow_underscores=True) == '_one_two._tcp.local.'
assert r.service_type_name('_one_two._tcp.local.', strict=False) == '_one_two._tcp.local.'

def test_invalid_addresses(self):
type_ = "_test-srvc-type._tcp.local."
Expand Down

0 comments on commit 73b5980

Please sign in to comment.