Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/dns/rdtypes/mxbase.py: 61%

44 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:58 +0000

1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license 

2 

3# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. 

4# 

5# Permission to use, copy, modify, and distribute this software and its 

6# documentation for any purpose with or without fee is hereby granted, 

7# provided that the above copyright notice and this permission notice 

8# appear in all copies. 

9# 

10# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 

11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 

12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 

13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 

14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 

15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 

16# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 

17 

18"""MX-like base classes.""" 

19 

20import struct 

21 

22import dns.exception 

23import dns.immutable 

24import dns.name 

25import dns.rdata 

26import dns.rdtypes.util 

27 

28 

29@dns.immutable.immutable 

30class MXBase(dns.rdata.Rdata): 

31 

32 """Base class for rdata that is like an MX record.""" 

33 

34 __slots__ = ["preference", "exchange"] 

35 

36 def __init__(self, rdclass, rdtype, preference, exchange): 

37 super().__init__(rdclass, rdtype) 

38 self.preference = self._as_uint16(preference) 

39 self.exchange = self._as_name(exchange) 

40 

41 def to_text(self, origin=None, relativize=True, **kw): 

42 exchange = self.exchange.choose_relativity(origin, relativize) 

43 return "%d %s" % (self.preference, exchange) 

44 

45 @classmethod 

46 def from_text( 

47 cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None 

48 ): 

49 preference = tok.get_uint16() 

50 exchange = tok.get_name(origin, relativize, relativize_to) 

51 return cls(rdclass, rdtype, preference, exchange) 

52 

53 def _to_wire(self, file, compress=None, origin=None, canonicalize=False): 

54 pref = struct.pack("!H", self.preference) 

55 file.write(pref) 

56 self.exchange.to_wire(file, compress, origin, canonicalize) 

57 

58 @classmethod 

59 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): 

60 preference = parser.get_uint16() 

61 exchange = parser.get_name(origin) 

62 return cls(rdclass, rdtype, preference, exchange) 

63 

64 def _processing_priority(self): 

65 return self.preference 

66 

67 @classmethod 

68 def _processing_order(cls, iterable): 

69 return dns.rdtypes.util.priority_processing_order(iterable) 

70 

71 

72@dns.immutable.immutable 

73class UncompressedMX(MXBase): 

74 

75 """Base class for rdata that is like an MX record, but whose name 

76 is not compressed when converted to DNS wire format, and whose 

77 digestable form is not downcased.""" 

78 

79 def _to_wire(self, file, compress=None, origin=None, canonicalize=False): 

80 super()._to_wire(file, None, origin, False) 

81 

82 

83@dns.immutable.immutable 

84class UncompressedDowncasingMX(MXBase): 

85 

86 """Base class for rdata that is like an MX record, but whose name 

87 is not compressed when convert to DNS wire format.""" 

88 

89 def _to_wire(self, file, compress=None, origin=None, canonicalize=False): 

90 super()._to_wire(file, None, origin, canonicalize)