filer.utils.recursive_dictionary: 25 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/filer/utils/recursive_dictionary.py

Stats: 0 executed, 25 missed, 0 excluded, 54 ignored

  1. #-*- coding: utf-8 -*-
  2. # https://gist.github.com/114831
  3. # recursive_dictionary.py
  4. # Created 2009-05-20 by Jannis Andrija Schnitzer.
  5. #
  6. # Copyright (c) 2009 Jannis Andrija Schnitzer
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining a copy
  9. # of this software and associated documentation files (the "Software"), to deal
  10. # in the Software without restriction, including without limitation the rights
  11. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the Software is
  13. # furnished to do so, subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included in
  16. # all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. # THE SOFTWARE.
  25. __author__ = 'jannis@itisme.org (Jannis Andrija Schnitzer)'
  26. class RecursiveDictionary(dict):
  27. """RecursiveDictionary provides the methods rec_update and iter_rec_update
  28. that can be used to update member dictionaries rather than overwriting
  29. them."""
  30. def rec_update(self, other, **third):
  31. """Recursively update the dictionary with the contents of other and
  32. third like dict.update() does - but don't overwrite sub-dictionaries.
  33. Example:
  34. >>> d = RecursiveDictionary({'foo': {'bar': 42}})
  35. >>> d.rec_update({'foo': {'baz': 36}})
  36. >>> d
  37. {'foo': {'baz': 36, 'bar': 42}}
  38. """
  39. try:
  40. iterator = other.iteritems()
  41. except AttributeError:
  42. iterator = other
  43. self.iter_rec_update(iterator)
  44. self.iter_rec_update(third.iteritems())
  45. def iter_rec_update(self, iterator):
  46. for (key, value) in iterator:
  47. if key in self and\
  48. isinstance(self[key], dict) and isinstance(value, dict):
  49. self[key] = RecursiveDictionary(self[key])
  50. self[key].rec_update(value)
  51. else:
  52. self[key] = value
  53. # changed version
  54. class RecursiveDictionaryWithExcludes(RecursiveDictionary):
  55. """
  56. Same as RecursiveDictionary, but respects a list of keys that should be excluded from recursion
  57. and handled like a normal dict.update()
  58. """
  59. def __init__(self, *args, **kwargs):
  60. self.rec_excluded_keys = kwargs.pop('rec_excluded_keys', ())
  61. super(RecursiveDictionaryWithExcludes, self).__init__(*args, **kwargs)
  62. def iter_rec_update(self, iterator):
  63. for (key, value) in iterator:
  64. if key in self and\
  65. isinstance(self[key], dict) and isinstance(value, dict) and\
  66. key not in self.rec_excluded_keys:
  67. self[key] = RecursiveDictionaryWithExcludes(self[key],
  68. rec_excluded_keys=self.rec_excluded_keys)
  69. self[key].rec_update(value)
  70. else:
  71. self[key] = value