reporting.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # coding=utf-8
  2. #
  3. # This file is part of Hypothesis, which may be found at
  4. # https://github.com/HypothesisWorks/hypothesis-python
  5. #
  6. # Most of this work is copyright (C) 2013-2018 David R. MacIver
  7. # (david@drmaciver.com), but it contains contributions by others. See
  8. # CONTRIBUTING.rst for a full list of people who may hold copyright, and
  9. # consult the git log if you need to determine who owns an individual
  10. # contribution.
  11. #
  12. # This Source Code Form is subject to the terms of the Mozilla Public License,
  13. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  14. # obtain one at http://mozilla.org/MPL/2.0/.
  15. #
  16. # END HEADER
  17. from __future__ import division, print_function, absolute_import
  18. import inspect
  19. from hypothesis._settings import Verbosity, settings
  20. from hypothesis.internal.compat import print_unicode, \
  21. escape_unicode_characters
  22. from hypothesis.utils.dynamicvariables import DynamicVariable
  23. def silent(value):
  24. pass
  25. def default(value):
  26. try:
  27. print_unicode(value)
  28. except UnicodeEncodeError:
  29. print_unicode(escape_unicode_characters(value))
  30. reporter = DynamicVariable(default)
  31. def current_reporter():
  32. return reporter.value
  33. def with_reporter(new_reporter):
  34. return reporter.with_value(new_reporter)
  35. def current_verbosity():
  36. return settings.default.verbosity
  37. def to_text(textish):
  38. if inspect.isfunction(textish):
  39. textish = textish()
  40. if isinstance(textish, bytes):
  41. textish = textish.decode('utf-8')
  42. return textish
  43. def verbose_report(text):
  44. if current_verbosity() >= Verbosity.verbose:
  45. current_reporter()(to_text(text))
  46. def debug_report(text):
  47. if current_verbosity() >= Verbosity.debug:
  48. current_reporter()(to_text(text))
  49. def report(text):
  50. if current_verbosity() >= Verbosity.normal:
  51. current_reporter()(to_text(text))