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
6 changes: 3 additions & 3 deletions examples/async_apple_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
def async_on_service_state_change(
zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
) -> None:
print("Service %s of type %s state changed: %s" % (name, service_type, state_change))
print(f"Service {name} of type {service_type} state changed: {state_change}")
if state_change is not ServiceStateChange.Added:
return
base_name = name[: -len(service_type) - 1]
Expand All @@ -55,11 +55,11 @@ async def _async_show_service_info(zeroconf: Zeroconf, service_type: str, name:
print(" Name: %s" % name)
print(" Addresses: %s" % ", ".join(addresses))
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
print(" Server: %s" % (info.server,))
print(f" Server: {info.server}")
if info.properties:
print(" Properties are:")
for key, value in info.properties.items():
print(" %s: %s" % (key, value))
print(f" {key}: {value}")
else:
print(" No properties")
else:
Expand Down
6 changes: 3 additions & 3 deletions examples/async_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
def async_on_service_state_change(
zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
) -> None:
print("Service %s of type %s state changed: %s" % (name, service_type, state_change))
print(f"Service {name} of type {service_type} state changed: {state_change}")
if state_change is not ServiceStateChange.Added:
return
asyncio.ensure_future(async_display_service_info(zeroconf, service_type, name))
Expand All @@ -32,11 +32,11 @@ async def async_display_service_info(zeroconf: Zeroconf, service_type: str, name
print(" Name: %s" % name)
print(" Addresses: %s" % ", ".join(addresses))
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
print(" Server: %s" % (info.server,))
print(f" Server: {info.server}")
if info.properties:
print(" Properties are:")
for key, value in info.properties.items():
print(" %s: %s" % (key, value))
print(f" {key}: {value}")
else:
print(" No properties")
else:
Expand Down
4 changes: 2 additions & 2 deletions examples/async_service_info_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None:
addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_addresses()]
print(" Addresses: %s" % ", ".join(addresses))
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
print(" Server: %s" % (info.server,))
print(f" Server: {info.server}")
if info.properties:
print(" Properties are:")
for key, value in info.properties.items():
print(" %s: %s" % (key, value))
print(f" {key}: {value}")
else:
print(" No properties")
else:
Expand Down
6 changes: 3 additions & 3 deletions examples/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def on_service_state_change(
zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
) -> None:
print("Service %s of type %s state changed: %s" % (name, service_type, state_change))
print(f"Service {name} of type {service_type} state changed: {state_change}")

if state_change is ServiceStateChange.Added:
info = zeroconf.get_service_info(service_type, name)
Expand All @@ -26,11 +26,11 @@ def on_service_state_change(
addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_scoped_addresses()]
print(" Addresses: %s" % ", ".join(addresses))
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
print(" Server: %s" % (info.server,))
print(f" Server: {info.server}")
if info.properties:
print(" Properties are:")
for key, value in info.properties.items():
print(" %s: %s" % (key, value))
print(f" {key}: {value}")
else:
print(" No properties")
else:
Expand Down
4 changes: 2 additions & 2 deletions examples/self_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# Test a few module features, including service registration, service
# query (for Zoe), and service unregistration.
print("Multicast DNS Service Discovery for Python, version %s" % (__version__,))
print(f"Multicast DNS Service Discovery for Python, version {__version__}")
r = Zeroconf()
print("1. Testing registration of a service...")
desc = {'version': '0.10', 'a': 'test value', 'b': 'another value'}
Expand All @@ -40,7 +40,7 @@
queried_info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.")
assert queried_info
assert set(queried_info.parsed_addresses()) == expected
print(" Getting self: %s" % (queried_info,))
print(f" Getting self: {queried_info}")
print(" Query done.")
print("4. Testing unregister of service information...")
r.unregister_service(info)
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


""" conftest for zeroconf tests. """
Expand Down
35 changes: 16 additions & 19 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


"""Unit tests for aio.py."""
Expand Down Expand Up @@ -57,11 +56,9 @@ def verify_threads_ended():
yield
threads_after = frozenset(threading.enumerate())
non_executor_threads = frozenset(
[
thread
for thread in threads_after
if "asyncio" not in thread.name and "ThreadPoolExecutor" not in thread.name
]
thread
for thread in threads_after
if "asyncio" not in thread.name and "ThreadPoolExecutor" not in thread.name
)
threads = non_executor_threads - threads_before
assert not threads
Expand Down Expand Up @@ -119,7 +116,7 @@ async def test_async_service_registration() -> None:
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
type_ = "_test1-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
registration_name = f"{name}.{type_}"

calls = []

Expand Down Expand Up @@ -179,7 +176,7 @@ async def test_async_service_registration_name_conflict() -> None:
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
type_ = "_test-srvc2-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
registration_name = f"{name}.{type_}"

desc = {'path': '/~paulsm/'}
info = ServiceInfo(
Expand Down Expand Up @@ -227,7 +224,7 @@ async def test_async_service_registration_name_does_not_match_type() -> None:
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
type_ = "_test-srvc3-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
registration_name = f"{name}.{type_}"

desc = {'path': '/~paulsm/'}
info = ServiceInfo(
Expand All @@ -254,7 +251,7 @@ async def test_async_tasks() -> None:
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
type_ = "_test-srvc4-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
registration_name = f"{name}.{type_}"

calls = []

Expand Down Expand Up @@ -320,7 +317,7 @@ async def test_async_wait_unblocks_on_update() -> None:
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
type_ = "_test-srvc4-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
registration_name = f"{name}.{type_}"

desc = {'path': '/~paulsm/'}
info = ServiceInfo(
Expand Down Expand Up @@ -356,8 +353,8 @@ async def test_service_info_async_request() -> None:
type_ = "_test1-srvc-type._tcp.local."
name = "xxxyyy"
name2 = "abc"
registration_name = "%s.%s" % (name, type_)
registration_name2 = "%s.%s" % (name2, type_)
registration_name = f"{name}.{type_}"
registration_name2 = f"{name2}.{type_}"

# Start a tasks BEFORE the registration that will keep trying
# and see the registration a bit later
Expand Down Expand Up @@ -454,7 +451,7 @@ async def test_async_service_browser() -> None:
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
type_ = "_test9-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
registration_name = f"{name}.{type_}"

calls = []

Expand Down Expand Up @@ -513,7 +510,7 @@ async def test_async_context_manager() -> None:
"""Test using an async context manager."""
type_ = "_test10-sr-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
registration_name = f"{name}.{type_}"

async with AsyncZeroconf(interfaces=['127.0.0.1']) as aiozc:
info = ServiceInfo(
Expand All @@ -539,8 +536,8 @@ async def test_async_unregister_all_services() -> None:
type_ = "_test1-srvc-type._tcp.local."
name = "xxxyyy"
name2 = "abc"
registration_name = "%s.%s" % (name, type_)
registration_name2 = "%s.%s" % (name2, type_)
registration_name = f"{name}.{type_}"
registration_name2 = f"{name2}.{type_}"

desc = {'path': '/~paulsm/'}
info = ServiceInfo(
Expand Down Expand Up @@ -594,7 +591,7 @@ async def test_async_unregister_all_services() -> None:
async def test_async_zeroconf_service_types():
type_ = "_test-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
registration_name = f"{name}.{type_}"

zeroconf_registrar = AsyncZeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
Expand Down Expand Up @@ -808,7 +805,7 @@ async def test_info_asking_default_is_asking_qm_questions_after_the_first_qu():
zeroconf_info = aiozc.zeroconf

name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
registration_name = f"{name}.{type_}"

desc = {'path': '/~paulsm/'}
info = ServiceInfo(
Expand Down
21 changes: 10 additions & 11 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


""" Unit tests for zeroconf._cache. """
Expand Down Expand Up @@ -98,7 +97,7 @@ def test_async_all_by_details(self):
record2 = r.DNSAddress('a', const._TYPE_A, const._CLASS_IN, 1, b'b')
cache = r.DNSCache()
cache.async_add_records([record1, record2])
assert set(cache.async_all_by_details('a', const._TYPE_A, const._CLASS_IN)) == set([record1, record2])
assert set(cache.async_all_by_details('a', const._TYPE_A, const._CLASS_IN)) == {record1, record2}

def test_async_entries_with_server(self):
record1 = r.DNSService(
Expand All @@ -109,8 +108,8 @@ def test_async_entries_with_server(self):
)
cache = r.DNSCache()
cache.async_add_records([record1, record2])
assert set(cache.async_entries_with_server('ab')) == set([record1, record2])
assert set(cache.async_entries_with_server('AB')) == set([record1, record2])
assert set(cache.async_entries_with_server('ab')) == {record1, record2}
assert set(cache.async_entries_with_server('AB')) == {record1, record2}

def test_async_entries_with_name(self):
record1 = r.DNSService(
Expand All @@ -121,8 +120,8 @@ def test_async_entries_with_name(self):
)
cache = r.DNSCache()
cache.async_add_records([record1, record2])
assert set(cache.async_entries_with_name('irrelevant')) == set([record1, record2])
assert set(cache.async_entries_with_name('Irrelevant')) == set([record1, record2])
assert set(cache.async_entries_with_name('irrelevant')) == {record1, record2}
assert set(cache.async_entries_with_name('Irrelevant')) == {record1, record2}


# These functions have been seen in other projects so
Expand Down Expand Up @@ -152,7 +151,7 @@ def test_get_all_by_details(self):
record2 = r.DNSAddress('a', const._TYPE_A, const._CLASS_IN, 1, b'b')
cache = r.DNSCache()
cache.async_add_records([record1, record2])
assert set(cache.get_all_by_details('a', const._TYPE_A, const._CLASS_IN)) == set([record1, record2])
assert set(cache.get_all_by_details('a', const._TYPE_A, const._CLASS_IN)) == {record1, record2}

def test_entries_with_server(self):
record1 = r.DNSService(
Expand All @@ -163,8 +162,8 @@ def test_entries_with_server(self):
)
cache = r.DNSCache()
cache.async_add_records([record1, record2])
assert set(cache.entries_with_server('ab')) == set([record1, record2])
assert set(cache.entries_with_server('AB')) == set([record1, record2])
assert set(cache.entries_with_server('ab')) == {record1, record2}
assert set(cache.entries_with_server('AB')) == {record1, record2}

def test_entries_with_name(self):
record1 = r.DNSService(
Expand All @@ -175,8 +174,8 @@ def test_entries_with_name(self):
)
cache = r.DNSCache()
cache.async_add_records([record1, record2])
assert set(cache.entries_with_name('irrelevant')) == set([record1, record2])
assert set(cache.entries_with_name('Irrelevant')) == set([record1, record2])
assert set(cache.entries_with_name('irrelevant')) == {record1, record2}
assert set(cache.entries_with_name('Irrelevant')) == {record1, record2}

def test_current_entry_with_name_and_alias(self):
record1 = r.DNSPointer(
Expand Down
43 changes: 20 additions & 23 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


""" Unit tests for zeroconf._core """
Expand Down Expand Up @@ -55,28 +54,26 @@ async def test_reaper():
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
zeroconf = aiozc.zeroconf
cache = zeroconf.cache
original_entries = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
original_entries = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names())))
record_with_10s_ttl = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 10, b'a')
record_with_1s_ttl = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
zeroconf.cache.async_add_records([record_with_10s_ttl, record_with_1s_ttl])
question = r.DNSQuestion("_hap._tcp._local.", const._TYPE_PTR, const._CLASS_IN)
now = r.current_time_millis()
other_known_answers = set(
[
r.DNSPointer(
"_hap._tcp.local.",
const._TYPE_PTR,
const._CLASS_IN,
10000,
'known-to-other._hap._tcp.local.',
)
]
)
other_known_answers = {
r.DNSPointer(
"_hap._tcp.local.",
const._TYPE_PTR,
const._CLASS_IN,
10000,
'known-to-other._hap._tcp.local.',
)
}
zeroconf.question_history.add_question_at_time(question, now, other_known_answers)
assert zeroconf.question_history.suppresses(question, now, other_known_answers)
entries_with_cache = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
entries_with_cache = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names())))
await asyncio.sleep(1.2)
entries = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
entries = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names())))
await aiozc.async_close()
assert not zeroconf.question_history.suppresses(question, now, other_known_answers)
assert entries != original_entries
Expand Down Expand Up @@ -367,7 +364,7 @@ def test_register_service_with_custom_ttl():
name = "MyTestHome"
info_service = r.ServiceInfo(
type_,
'%s.%s' % (name, type_),
f'{name}.{type_}',
80,
0,
0,
Expand Down Expand Up @@ -423,9 +420,9 @@ def test_tc_bit_defers():
name2 = "knownname2"
name3 = "knownname3"

registration_name = "%s.%s" % (name, type_)
registration2_name = "%s.%s" % (name2, type_)
registration3_name = "%s.%s" % (name3, type_)
registration_name = f"{name}.{type_}"
registration2_name = f"{name2}.{type_}"
registration3_name = f"{name3}.{type_}"

desc = {'path': '/~paulsm/'}
server_name = "ash-2.local."
Expand Down Expand Up @@ -502,9 +499,9 @@ def test_tc_bit_defers_last_response_missing():
name2 = "knownname2"
name3 = "knownname3"

registration_name = "%s.%s" % (name, type_)
registration2_name = "%s.%s" % (name2, type_)
registration3_name = "%s.%s" % (name3, type_)
registration_name = f"{name}.{type_}"
registration2_name = f"{name2}.{type_}"
registration3_name = f"{name3}.{type_}"

desc = {'path': '/~paulsm/'}
server_name = "ash-2.local."
Expand Down Expand Up @@ -729,7 +726,7 @@ def test_shutdown_while_register_in_process():
name = "MyTestHome"
info_service = r.ServiceInfo(
type_,
'%s.%s' % (name, type_),
f'{name}.{type_}',
80,
0,
0,
Expand Down
Loading