data.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # Copyright (C) 2010 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. #
  16. # Helper functions for data elements as used in cc-channel and
  17. # configuration. There is no python equivalent for the cpp Element
  18. # class, since data elements are represented by native python types
  19. # (int, real, bool, string, list and dict respectively)
  20. #
  21. import json
  22. class DataNotFoundError(Exception): pass
  23. class DataTypeError(Exception): pass
  24. def remove_identical(a, b):
  25. """Removes the values from dict a that are the same as in dict b.
  26. Raises a DataTypeError is a or b is not a dict"""
  27. to_remove = []
  28. if type(a) != dict or type(b) != dict:
  29. raise DataTypeError("Not a dict in remove_identical()")
  30. duplicate_keys = [key for key in a.keys() if key in b and a[key] == b[key]]
  31. for id in duplicate_keys:
  32. del(a[id])
  33. def merge(orig, new):
  34. """Merges the contents of new into orig, think recursive update()
  35. orig and new must both be dicts. If an element value is None in
  36. new it will be removed in orig."""
  37. if type(orig) != dict or type(new) != dict:
  38. raise DataTypeError("Not a dict in merge()")
  39. orig.update(new)
  40. remove_null_items(orig)
  41. def remove_null_items(d):
  42. """Recursively removes all (key,value) pairs from d where the
  43. value is None"""
  44. null_keys = []
  45. for key in d.keys():
  46. if type(d[key]) == dict:
  47. remove_null_items(d[key])
  48. elif d[key] is None:
  49. null_keys.append(key)
  50. for k in null_keys:
  51. del d[k]
  52. def _concat_identifier(id_parts):
  53. """Concatenates the given identifier parts into a string,
  54. delimited with the '/' character.
  55. """
  56. return '/'.join(id_parts)
  57. def split_identifier(identifier):
  58. """Splits the given identifier into a list of identifier parts,
  59. as delimited by the '/' character.
  60. Raises a DataTypeError if identifier is not a string."""
  61. if type(identifier) != str:
  62. raise DataTypeError("identifier is not a string")
  63. id_parts = identifier.split('/')
  64. id_parts[:] = (value for value in id_parts if value != "")
  65. return id_parts
  66. def split_identifier_list_indices(identifier):
  67. """Finds list indexes in the given identifier, which are of the
  68. format [integer].
  69. Identifier must be a string.
  70. This will only give the list index for the last 'part' of the
  71. given identifier (as delimited by the '/' sign).
  72. Raises a DataTypeError if the identifier is not a string,
  73. or if the format is bad.
  74. Returns a tuple, where the first element is the string part of
  75. the identifier, and the second element is a list of (nested) list
  76. indices.
  77. Examples:
  78. 'a/b/c' will return ('a/b/c', None)
  79. 'a/b/c[1]' will return ('a/b/c', [1])
  80. 'a/b/c[1][2][3]' will return ('a/b/c', [1, 2, 3])
  81. 'a[0]/b[1]/c[2]' will return ('a[0]/b[1]/c', [2])
  82. """
  83. if type(identifier) != str:
  84. raise DataTypeError("identifier in "
  85. "split_identifier_list_indices() "
  86. "not a string: " + str(identifier))
  87. # We only work on the final 'part' of the identifier
  88. id_parts = split_identifier(identifier)
  89. id_str = id_parts[-1]
  90. i = id_str.find('[')
  91. if i < 0:
  92. if id_str.find(']') >= 0:
  93. raise DataTypeError("Bad format in identifier (] but no [): " + str(identifier))
  94. return identifier, None
  95. # keep the non-index part of that to replace later
  96. id = id_str[:i]
  97. indices = []
  98. while i >= 0:
  99. e = id_str.find(']')
  100. if e < i + 1:
  101. raise DataTypeError("Bad format in identifier (] before [): " + str(identifier))
  102. try:
  103. indices.append(int(id_str[i+1:e]))
  104. except ValueError:
  105. raise DataTypeError("List index in " + identifier + " not an integer")
  106. id_str = id_str[e + 1:]
  107. i = id_str.find('[')
  108. if i > 0:
  109. raise DataTypeError("Bad format in identifier ([ within []): " + str(identifier))
  110. if id.find(']') >= 0 or len(id_str) > 0:
  111. raise DataTypeError("Bad format in identifier (extra ]): " + str(identifier))
  112. # we replace the final part of the original identifier with
  113. # the stripped string
  114. id_parts[-1] = id
  115. id = _concat_identifier(id_parts)
  116. return id, indices
  117. def _find_child_el(element, id):
  118. """Finds the child of element with the given id. If the id contains
  119. [i], where i is a number, and the child element is a list, the
  120. i-th element of that list is returned instead of the list itself.
  121. Raises a DataTypeError if the element is of wrong type, if id
  122. is not a string, or if the id string contains a bad value.
  123. Raises a DataNotFoundError if the element at id could not be
  124. found.
  125. """
  126. id, list_indices = split_identifier_list_indices(id)
  127. if type(element) == dict and id in element.keys():
  128. result = element[id]
  129. else:
  130. raise DataNotFoundError(id + " in " + str(element))
  131. if type(result) == list and list_indices is not None:
  132. for list_index in list_indices:
  133. if list_index >= len(result):
  134. raise DataNotFoundError("Element " + str(list_index) + " in " + str(result))
  135. result = result[list_index]
  136. return result
  137. def find(element, identifier):
  138. """Returns the subelement in the given data element, raises
  139. DataNotFoundError if not found.
  140. Returns the given element if the identifier is an empty string.
  141. Raises a DataTypeError if identifier is not a string, or if
  142. identifier is not empty, and element is not a dict.
  143. """
  144. if type(identifier) != str:
  145. raise DataTypeError("identifier in find() is not a str")
  146. if identifier == "":
  147. return element
  148. if type(element) != dict:
  149. raise DataTypeError("element in find() is not a dict")
  150. id_parts = split_identifier(identifier)
  151. cur_el = element
  152. for id in id_parts:
  153. cur_el = _find_child_el(cur_el, id)
  154. return cur_el
  155. def set(element, identifier, value):
  156. """Sets the value at the element specified by identifier to value.
  157. If the value is None, it is removed from the dict. If element
  158. is not a dict, or if the identifier points to something that is
  159. not, a DataTypeError is raised. The element is updated inline,
  160. so if the original needs to be kept, you must make a copy before
  161. calling set(). The updated base element is returned (so that
  162. el.set().set().set() is possible)"""
  163. if type(element) != dict:
  164. raise DataTypeError("element in set() is not a dict")
  165. if type(identifier) != str:
  166. raise DataTypeError("identifier in set() is not a str")
  167. id_parts = split_identifier(identifier)
  168. cur_el = element
  169. for id in id_parts[:-1]:
  170. try:
  171. cur_el = _find_child_el(cur_el, id)
  172. except DataNotFoundError:
  173. if value is None:
  174. # ok we are unsetting a value that wasn't set in
  175. # the first place. Simply stop.
  176. return
  177. cur_el[id] = {}
  178. cur_el = cur_el[id]
  179. id, list_indices = split_identifier_list_indices(id_parts[-1])
  180. if list_indices is None:
  181. # value can be an empty list or dict, so check for None eplicitely
  182. if value is not None:
  183. cur_el[id] = value
  184. else:
  185. del cur_el[id]
  186. else:
  187. cur_el = cur_el[id]
  188. # in case of nested lists, we need to get to the next to last
  189. for list_index in list_indices[:-1]:
  190. if type(cur_el) != list:
  191. raise DataTypeError("Element at " + identifier + " is not a list")
  192. if len(cur_el) <= list_index:
  193. raise DataNotFoundError("List index at " + identifier + " out of range")
  194. cur_el = cur_el[list_index]
  195. # value can be an empty list or dict, so check for None eplicitely
  196. list_index = list_indices[-1]
  197. if type(cur_el) != list:
  198. raise DataTypeError("Element at " + identifier + " is not a list")
  199. if len(cur_el) <= list_index:
  200. raise DataNotFoundError("List index at " + identifier + " out of range")
  201. if value is not None:
  202. cur_el[list_index] = value
  203. else:
  204. del cur_el[list_index]
  205. return element
  206. def unset(element, identifier):
  207. """Removes the element at the given identifier if it exists. Raises
  208. a DataTypeError if element is not a dict or if identifier is not
  209. a string. Returns the base element."""
  210. # perhaps we can simply do with set none, and remove this whole
  211. # function
  212. return set(element, identifier, None)
  213. def find_no_exc(element, identifier):
  214. """Returns the subelement in the given data element, returns None
  215. if not found, or if an error occurred (i.e. this function should
  216. never raise an exception)"""
  217. try:
  218. return find(element, identifier)
  219. except DataNotFoundError:
  220. return None
  221. except DataTypeError:
  222. return None
  223. def parse_value_str(value_str):
  224. """Parses the given string to a native python object. If the
  225. string cannot be parsed, it is returned. If it is not a string,
  226. None is returned"""
  227. if type(value_str) != str:
  228. return None
  229. try:
  230. return json.loads(value_str)
  231. except ValueError as ve:
  232. # simply return the string itself
  233. return value_str