namedict.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. # pylint seems to be confused about this one!
  28. from collections.abc import MutableMapping # pylint: disable=no-name-in-module
  29. import dns.name
  30. class NameDict(MutableMapping):
  31. """A dictionary whose keys are dns.name.Name objects.
  32. In addition to being like a regular Python dictionary, this
  33. dictionary can also get the deepest match for a given key.
  34. """
  35. __slots__ = ["max_depth", "max_depth_items", "__store"]
  36. def __init__(self, *args, **kwargs):
  37. super().__init__()
  38. self.__store = dict()
  39. #: the maximum depth of the keys that have ever been added
  40. self.max_depth = 0
  41. #: the number of items of maximum depth
  42. self.max_depth_items = 0
  43. self.update(dict(*args, **kwargs))
  44. def __update_max_depth(self, key):
  45. if len(key) == self.max_depth:
  46. self.max_depth_items = self.max_depth_items + 1
  47. elif len(key) > self.max_depth:
  48. self.max_depth = len(key)
  49. self.max_depth_items = 1
  50. def __getitem__(self, key):
  51. return self.__store[key]
  52. def __setitem__(self, key, value):
  53. if not isinstance(key, dns.name.Name):
  54. raise ValueError("NameDict key must be a name")
  55. self.__store[key] = value
  56. self.__update_max_depth(key)
  57. def __delitem__(self, key):
  58. self.__store.pop(key)
  59. if len(key) == self.max_depth:
  60. self.max_depth_items = self.max_depth_items - 1
  61. if self.max_depth_items == 0:
  62. self.max_depth = 0
  63. for k in self.__store:
  64. self.__update_max_depth(k)
  65. def __iter__(self):
  66. return iter(self.__store)
  67. def __len__(self):
  68. return len(self.__store)
  69. def has_key(self, key):
  70. return key in self.__store
  71. def get_deepest_match(self, name):
  72. """Find the deepest match to *name* in the dictionary.
  73. The deepest match is the longest name in the dictionary which is
  74. a superdomain of *name*. Note that *superdomain* includes matching
  75. *name* itself.
  76. *name*, a ``dns.name.Name``, the name to find.
  77. Returns a ``(key, value)`` where *key* is the deepest
  78. ``dns.name.Name``, and *value* is the value associated with *key*.
  79. """
  80. depth = len(name)
  81. if depth > self.max_depth:
  82. depth = self.max_depth
  83. for i in range(-depth, 0):
  84. n = dns.name.Name(name[i:])
  85. if n in self:
  86. return (n, self[n])
  87. v = self[dns.name.empty]
  88. return (dns.name.empty, v)