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
23from typing import List, NamedTuple, Optional, TYPE_CHECKING
24
25
26from ._dns import DNSRecord
27
28
29if TYPE_CHECKING:
30    from ._core import Zeroconf
31
32
33class RecordUpdate(NamedTuple):
34    new: DNSRecord
35    old: Optional[DNSRecord]
36
37
38class RecordUpdateListener:
39    """Base call for all record listeners.
40
41    All listeners passed to async_add_listener should use RecordUpdateListener
42    as a base class. In the future it will be required.
43    """
44
45    def update_record(  # pylint: disable=no-self-use
46        self, zc: 'Zeroconf', now: float, record: DNSRecord
47    ) -> None:
48        """Update a single record.
49
50        This method is deprecated and will be removed in a future version.
51        update_records should be implemented instead.
52        """
53        raise RuntimeError("update_record is deprecated and will be removed in a future version.")
54
55    def async_update_records(self, zc: 'Zeroconf', now: float, records: List[RecordUpdate]) -> None:
56        """Update multiple records in one shot.
57
58        All records that are received in a single packet are passed
59        to update_records.
60
61        This implementation is a compatiblity shim to ensure older code
62        that uses RecordUpdateListener as a base class will continue to
63        get calls to update_record. This method will raise
64        NotImplementedError in a future version.
65
66        At this point the cache will not have the new records
67
68        Records are passed as a list of RecordUpdate.  This
69        allows consumers of async_update_records to avoid cache lookups.
70
71        This method will be run in the event loop.
72        """
73        for record in records:
74            self.update_record(zc, now, record[0])
75
76    def async_update_records_complete(self) -> None:
77        """Called when a record update has completed for all handlers.
78
79        At this point the cache will have the new records.
80
81        This method will be run in the event loop.
82        """
83