Skip to content

Commit e50b62b

Browse files
authored
Move ZeroconfServiceTypes to zeroconf.services.types (#553)
1 parent e7fb4e5 commit e50b62b

2 files changed

Lines changed: 83 additions & 59 deletions

File tree

zeroconf/__init__.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
"""
2222

2323
import sys
24-
import time
25-
from typing import Optional, Union
26-
from typing import Set, Tuple # noqa # used in type hints
2724

2825
from .const import ( # noqa # import needed for backwards compat
2926
_BROWSER_BACKOFF_LIMIT,
@@ -112,6 +109,7 @@
112109
ServiceStateChange,
113110
)
114111
from .services.registry import ServiceRegistry # noqa # import needed for backwards compat
112+
from .services.types import ZeroconfServiceTypes # noqa # import needed for backwards compat
115113
from .utils.name import service_type_name # noqa # import needed for backwards compat
116114
from .utils.net import ( # noqa # import needed for backwards compat
117115
add_multicast_member,
@@ -155,59 +153,3 @@
155153
If you need support for Python 3.5 please use version 0.28.0
156154
'''
157155
)
158-
159-
160-
# implementation classes
161-
162-
163-
class ZeroconfServiceTypes(ServiceListener):
164-
"""
165-
Return all of the advertised services on any local networks
166-
"""
167-
168-
def __init__(self) -> None:
169-
"""Keep track of found services in a set."""
170-
self.found_services = set() # type: Set[str]
171-
172-
def add_service(self, zc: 'Zeroconf', type_: str, name: str) -> None:
173-
"""Service added."""
174-
self.found_services.add(name)
175-
176-
def update_service(self, zc: 'Zeroconf', type_: str, name: str) -> None:
177-
"""Service updated."""
178-
179-
def remove_service(self, zc: 'Zeroconf', type_: str, name: str) -> None:
180-
"""Service removed."""
181-
182-
@classmethod
183-
def find(
184-
cls,
185-
zc: Optional['Zeroconf'] = None,
186-
timeout: Union[int, float] = 5,
187-
interfaces: InterfacesType = InterfaceChoice.All,
188-
ip_version: Optional[IPVersion] = None,
189-
) -> Tuple[str, ...]:
190-
"""
191-
Return all of the advertised services on any local networks.
192-
193-
:param zc: Zeroconf() instance. Pass in if already have an
194-
instance running or if non-default interfaces are needed
195-
:param timeout: seconds to wait for any responses
196-
:param interfaces: interfaces to listen on.
197-
:param ip_version: IP protocol version to use.
198-
:return: tuple of service type strings
199-
"""
200-
local_zc = zc or Zeroconf(interfaces=interfaces, ip_version=ip_version)
201-
listener = cls()
202-
browser = ServiceBrowser(local_zc, _SERVICE_TYPE_ENUMERATION_NAME, listener=listener)
203-
204-
# wait for responses
205-
time.sleep(timeout)
206-
207-
browser.cancel()
208-
209-
# close down anything we opened
210-
if zc is None:
211-
local_zc.close()
212-
213-
return tuple(sorted(listener.found_services))

zeroconf/services/types.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine
2+
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine
3+
4+
This module provides a framework for the use of DNS Service Discovery
5+
using IP multicast.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20+
USA
21+
"""
22+
23+
import time
24+
from typing import Optional, Set, Tuple, Union
25+
26+
from ..const import _SERVICE_TYPE_ENUMERATION_NAME
27+
from ..core import Zeroconf
28+
from ..services import ServiceBrowser, ServiceListener
29+
from ..utils.net import IPVersion, InterfaceChoice, InterfacesType
30+
31+
32+
class ZeroconfServiceTypes(ServiceListener):
33+
"""
34+
Return all of the advertised services on any local networks
35+
"""
36+
37+
def __init__(self) -> None:
38+
"""Keep track of found services in a set."""
39+
self.found_services: Set[str] = set()
40+
41+
def add_service(self, zc: Zeroconf, type_: str, name: str) -> None:
42+
"""Service added."""
43+
self.found_services.add(name)
44+
45+
def update_service(self, zc: Zeroconf, type_: str, name: str) -> None:
46+
"""Service updated."""
47+
48+
def remove_service(self, zc: Zeroconf, type_: str, name: str) -> None:
49+
"""Service removed."""
50+
51+
@classmethod
52+
def find(
53+
cls,
54+
zc: Optional[Zeroconf] = None,
55+
timeout: Union[int, float] = 5,
56+
interfaces: InterfacesType = InterfaceChoice.All,
57+
ip_version: Optional[IPVersion] = None,
58+
) -> Tuple[str, ...]:
59+
"""
60+
Return all of the advertised services on any local networks.
61+
62+
:param zc: Zeroconf() instance. Pass in if already have an
63+
instance running or if non-default interfaces are needed
64+
:param timeout: seconds to wait for any responses
65+
:param interfaces: interfaces to listen on.
66+
:param ip_version: IP protocol version to use.
67+
:return: tuple of service type strings
68+
"""
69+
local_zc = zc or Zeroconf(interfaces=interfaces, ip_version=ip_version)
70+
listener = cls()
71+
browser = ServiceBrowser(local_zc, _SERVICE_TYPE_ENUMERATION_NAME, listener=listener)
72+
73+
# wait for responses
74+
time.sleep(timeout)
75+
76+
browser.cancel()
77+
78+
# close down anything we opened
79+
if zc is None:
80+
local_zc.close()
81+
82+
return tuple(sorted(listener.found_services))

0 commit comments

Comments
 (0)