_utils.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import itertools
  2. import json
  3. import pkgutil
  4. import re
  5. from jsonschema.compat import MutableMapping, str_types, urlsplit
  6. class URIDict(MutableMapping):
  7. """
  8. Dictionary which uses normalized URIs as keys.
  9. """
  10. def normalize(self, uri):
  11. return urlsplit(uri).geturl()
  12. def __init__(self, *args, **kwargs):
  13. self.store = dict()
  14. self.store.update(*args, **kwargs)
  15. def __getitem__(self, uri):
  16. return self.store[self.normalize(uri)]
  17. def __setitem__(self, uri, value):
  18. self.store[self.normalize(uri)] = value
  19. def __delitem__(self, uri):
  20. del self.store[self.normalize(uri)]
  21. def __iter__(self):
  22. return iter(self.store)
  23. def __len__(self):
  24. return len(self.store)
  25. def __repr__(self):
  26. return repr(self.store)
  27. class Unset(object):
  28. """
  29. An as-of-yet unset attribute or unprovided default parameter.
  30. """
  31. def __repr__(self):
  32. return "<unset>"
  33. def load_schema(name):
  34. """
  35. Load a schema from ./schemas/``name``.json and return it.
  36. """
  37. data = pkgutil.get_data("jsonschema", "schemas/{0}.json".format(name))
  38. return json.loads(data.decode("utf-8"))
  39. def indent(string, times=1):
  40. """
  41. A dumb version of `textwrap.indent` from Python 3.3.
  42. """
  43. return "\n".join(" " * (4 * times) + line for line in string.splitlines())
  44. def format_as_index(indices):
  45. """
  46. Construct a single string containing indexing operations for the indices.
  47. For example, [1, 2, "foo"] -> [1][2]["foo"]
  48. Arguments:
  49. indices (sequence):
  50. The indices to format.
  51. """
  52. if not indices:
  53. return ""
  54. return "[%s]" % "][".join(repr(index) for index in indices)
  55. def find_additional_properties(instance, schema):
  56. """
  57. Return the set of additional properties for the given ``instance``.
  58. Weeds out properties that should have been validated by ``properties`` and
  59. / or ``patternProperties``.
  60. Assumes ``instance`` is dict-like already.
  61. """
  62. properties = schema.get("properties", {})
  63. patterns = "|".join(schema.get("patternProperties", {}))
  64. for property in instance:
  65. if property not in properties:
  66. if patterns and re.search(patterns, property):
  67. continue
  68. yield property
  69. def extras_msg(extras):
  70. """
  71. Create an error message for extra items or properties.
  72. """
  73. if len(extras) == 1:
  74. verb = "was"
  75. else:
  76. verb = "were"
  77. return ", ".join(repr(extra) for extra in extras), verb
  78. def types_msg(instance, types):
  79. """
  80. Create an error message for a failure to match the given types.
  81. If the ``instance`` is an object and contains a ``name`` property, it will
  82. be considered to be a description of that object and used as its type.
  83. Otherwise the message is simply the reprs of the given ``types``.
  84. """
  85. reprs = []
  86. for type in types:
  87. try:
  88. reprs.append(repr(type["name"]))
  89. except Exception:
  90. reprs.append(repr(type))
  91. return "%r is not of type %s" % (instance, ", ".join(reprs))
  92. def flatten(suitable_for_isinstance):
  93. """
  94. isinstance() can accept a bunch of really annoying different types:
  95. * a single type
  96. * a tuple of types
  97. * an arbitrary nested tree of tuples
  98. Return a flattened tuple of the given argument.
  99. """
  100. types = set()
  101. if not isinstance(suitable_for_isinstance, tuple):
  102. suitable_for_isinstance = (suitable_for_isinstance,)
  103. for thing in suitable_for_isinstance:
  104. if isinstance(thing, tuple):
  105. types.update(flatten(thing))
  106. else:
  107. types.add(thing)
  108. return tuple(types)
  109. def ensure_list(thing):
  110. """
  111. Wrap ``thing`` in a list if it's a single str.
  112. Otherwise, return it unchanged.
  113. """
  114. if isinstance(thing, str_types):
  115. return [thing]
  116. return thing
  117. def equal(one, two):
  118. """
  119. Check if two things are equal, but evade booleans and ints being equal.
  120. """
  121. return unbool(one) == unbool(two)
  122. def unbool(element, true=object(), false=object()):
  123. """
  124. A hack to make True and 1 and False and 0 unique for ``uniq``.
  125. """
  126. if element is True:
  127. return true
  128. elif element is False:
  129. return false
  130. return element
  131. def uniq(container):
  132. """
  133. Check if all of a container's elements are unique.
  134. Successively tries first to rely that the elements are hashable, then
  135. falls back on them being sortable, and finally falls back on brute
  136. force.
  137. """
  138. try:
  139. return len(set(unbool(i) for i in container)) == len(container)
  140. except TypeError:
  141. try:
  142. sort = sorted(unbool(i) for i in container)
  143. sliced = itertools.islice(sort, 1, None)
  144. for i, j in zip(sort, sliced):
  145. if i == j:
  146. return False
  147. except (NotImplementedError, TypeError):
  148. seen = []
  149. for e in container:
  150. e = unbool(e)
  151. if e in seen:
  152. return False
  153. seen.append(e)
  154. return True