namedict.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-2017 Nominum, Inc.
  3. # Copyright (C) 2016 Coresec Systems AB
  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. # THE SOFTWARE IS PROVIDED "AS IS" AND CORESEC SYSTEMS AB DISCLAIMS ALL
  19. # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  20. # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL CORESEC
  21. # SYSTEMS AB BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  22. # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  23. # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  24. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  25. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. """DNS name dictionary"""
  27. from collections.abc import MutableMapping
  28. import dns.name
  29. class NameDict(MutableMapping):
  30. """A dictionary whose keys are dns.name.Name objects.
  31. In addition to being like a regular Python dictionary, this
  32. dictionary can also get the deepest match for a given key.
  33. """
  34. __slots__ = ["max_depth", "max_depth_items", "__store"]
  35. def __init__(self, *args, **kwargs):
  36. super().__init__()
  37. self.__store = dict()
  38. #: the maximum depth of the keys that have ever been added
  39. self.max_depth = 0
  40. #: the number of items of maximum depth
  41. self.max_depth_items = 0
  42. self.update(dict(*args, **kwargs))
  43. def __update_max_depth(self, key):
  44. if len(key) == self.max_depth:
  45. self.max_depth_items = self.max_depth_items + 1
  46. elif len(key) > self.max_depth:
  47. self.max_depth = len(key)
  48. self.max_depth_items = 1
  49. def __getitem__(self, key):
  50. return self.__store[key]
  51. def __setitem__(self, key, value):
  52. if not isinstance(key, dns.name.Name):
  53. raise ValueError('NameDict key must be a name')
  54. self.__store[key] = value
  55. self.__update_max_depth(key)
  56. def __delitem__(self, key):
  57. self.__store.pop(key)
  58. if len(key) == self.max_depth:
  59. self.max_depth_items = self.max_depth_items - 1
  60. if self.max_depth_items == 0:
  61. self.max_depth = 0
  62. for k in self.__store:
  63. self.__update_max_depth(k)
  64. def __iter__(self):
  65. return iter(self.__store)
  66. def __len__(self):
  67. return len(self.__store)
  68. def has_key(self, key):
  69. return key in self.__store
  70. def get_deepest_match(self, name):
  71. """Find the deepest match to *name* in the dictionary.
  72. The deepest match is the longest name in the dictionary which is
  73. a superdomain of *name*. Note that *superdomain* includes matching
  74. *name* itself.
  75. *name*, a ``dns.name.Name``, the name to find.
  76. Returns a ``(key, value)`` where *key* is the deepest
  77. ``dns.name.Name``, and *value* is the value associated with *key*.
  78. """
  79. depth = len(name)
  80. if depth > self.max_depth:
  81. depth = self.max_depth
  82. for i in range(-depth, 0):
  83. n = dns.name.Name(name[i:])
  84. if n in self:
  85. return (n, self[n])
  86. v = self[dns.name.empty]
  87. return (dns.name.empty, v)