_utils.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright 2016 Julien Danjou
  2. # Copyright 2016 Joshua Harlow
  3. # Copyright 2013-2014 Ray Holder
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import inspect
  17. import sys
  18. import time
  19. from functools import update_wrapper
  20. import six
  21. # sys.maxint / 2, since Python 3.2 doesn't have a sys.maxint...
  22. try:
  23. MAX_WAIT = sys.maxint / 2
  24. except AttributeError:
  25. MAX_WAIT = 1073741823
  26. if six.PY2:
  27. from functools import WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES
  28. def wraps(fn):
  29. """Do the same as six.wraps but only copy attributes that exist.
  30. For example, object instances don't have __name__ attribute, so
  31. six.wraps fails. This is fixed in Python 3
  32. (https://bugs.python.org/issue3445), but didn't get backported to six.
  33. Also, see https://github.com/benjaminp/six/issues/250.
  34. """
  35. def filter_hasattr(obj, attrs):
  36. return tuple(a for a in attrs if hasattr(obj, a))
  37. return six.wraps(
  38. fn,
  39. assigned=filter_hasattr(fn, WRAPPER_ASSIGNMENTS),
  40. updated=filter_hasattr(fn, WRAPPER_UPDATES))
  41. def capture(fut, tb):
  42. # TODO(harlowja): delete this in future, since its
  43. # has to repeatedly calculate this crap.
  44. fut.set_exception_info(tb[1], tb[2])
  45. def getargspec(func):
  46. # This was deprecated in Python 3.
  47. return inspect.getargspec(func)
  48. else:
  49. from functools import wraps # noqa
  50. def capture(fut, tb):
  51. fut.set_exception(tb[1])
  52. def getargspec(func):
  53. return inspect.getfullargspec(func)
  54. def visible_attrs(obj, attrs=None):
  55. if attrs is None:
  56. attrs = {}
  57. for attr_name, attr in inspect.getmembers(obj):
  58. if attr_name.startswith("_"):
  59. continue
  60. attrs[attr_name] = attr
  61. return attrs
  62. def find_ordinal(pos_num):
  63. # See: https://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers
  64. if pos_num == 0:
  65. return "th"
  66. elif pos_num == 1:
  67. return 'st'
  68. elif pos_num == 2:
  69. return 'nd'
  70. elif pos_num == 3:
  71. return 'rd'
  72. elif pos_num >= 4 and pos_num <= 20:
  73. return 'th'
  74. else:
  75. return find_ordinal(pos_num % 10)
  76. def to_ordinal(pos_num):
  77. return "%i%s" % (pos_num, find_ordinal(pos_num))
  78. def get_callback_name(cb):
  79. """Get a callback fully-qualified name.
  80. If no name can be produced ``repr(cb)`` is called and returned.
  81. """
  82. segments = []
  83. try:
  84. segments.append(cb.__qualname__)
  85. except AttributeError:
  86. try:
  87. segments.append(cb.__name__)
  88. if inspect.ismethod(cb):
  89. try:
  90. # This attribute doesn't exist on py3.x or newer, so
  91. # we optionally ignore it... (on those versions of
  92. # python `__qualname__` should have been found anyway).
  93. segments.insert(0, cb.im_class.__name__)
  94. except AttributeError:
  95. pass
  96. except AttributeError:
  97. pass
  98. if not segments:
  99. return repr(cb)
  100. else:
  101. try:
  102. # When running under sphinx it appears this can be none?
  103. if cb.__module__:
  104. segments.insert(0, cb.__module__)
  105. except AttributeError:
  106. pass
  107. return ".".join(segments)
  108. try:
  109. now = time.monotonic # noqa
  110. except AttributeError:
  111. from monotonic import monotonic as now # noqa
  112. class cached_property(object):
  113. """A property that is computed once per instance.
  114. Upon being computed it replaces itself with an ordinary attribute. Deleting
  115. the attribute resets the property.
  116. Source: https://github.com/bottlepy/bottle/blob/1de24157e74a6971d136550afe1b63eec5b0df2b/bottle.py#L234-L246
  117. """ # noqa: E501
  118. def __init__(self, func):
  119. update_wrapper(self, func)
  120. self.func = func
  121. def __get__(self, obj, cls):
  122. if obj is None:
  123. return self
  124. value = obj.__dict__[self.func.__name__] = self.func(obj)
  125. return value