_utils.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. from pip._vendor 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. )
  42. def capture(fut, tb):
  43. # TODO(harlowja): delete this in future, since its
  44. # has to repeatedly calculate this crap.
  45. fut.set_exception_info(tb[1], tb[2])
  46. def getargspec(func):
  47. # This was deprecated in Python 3.
  48. return inspect.getargspec(func)
  49. else:
  50. from functools import wraps # noqa
  51. def capture(fut, tb):
  52. fut.set_exception(tb[1])
  53. def getargspec(func):
  54. return inspect.getfullargspec(func)
  55. def visible_attrs(obj, attrs=None):
  56. if attrs is None:
  57. attrs = {}
  58. for attr_name, attr in inspect.getmembers(obj):
  59. if attr_name.startswith("_"):
  60. continue
  61. attrs[attr_name] = attr
  62. return attrs
  63. def find_ordinal(pos_num):
  64. # See: https://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers
  65. if pos_num == 0:
  66. return "th"
  67. elif pos_num == 1:
  68. return "st"
  69. elif pos_num == 2:
  70. return "nd"
  71. elif pos_num == 3:
  72. return "rd"
  73. elif pos_num >= 4 and pos_num <= 20:
  74. return "th"
  75. else:
  76. return find_ordinal(pos_num % 10)
  77. def to_ordinal(pos_num):
  78. return "%i%s" % (pos_num, find_ordinal(pos_num))
  79. def get_callback_name(cb):
  80. """Get a callback fully-qualified name.
  81. If no name can be produced ``repr(cb)`` is called and returned.
  82. """
  83. segments = []
  84. try:
  85. segments.append(cb.__qualname__)
  86. except AttributeError:
  87. try:
  88. segments.append(cb.__name__)
  89. if inspect.ismethod(cb):
  90. try:
  91. # This attribute doesn't exist on py3.x or newer, so
  92. # we optionally ignore it... (on those versions of
  93. # python `__qualname__` should have been found anyway).
  94. segments.insert(0, cb.im_class.__name__)
  95. except AttributeError:
  96. pass
  97. except AttributeError:
  98. pass
  99. if not segments:
  100. return repr(cb)
  101. else:
  102. try:
  103. # When running under sphinx it appears this can be none?
  104. if cb.__module__:
  105. segments.insert(0, cb.__module__)
  106. except AttributeError:
  107. pass
  108. return ".".join(segments)
  109. try:
  110. now = time.monotonic # noqa
  111. except AttributeError:
  112. from monotonic import monotonic as now # noqa
  113. class cached_property(object):
  114. """A property that is computed once per instance.
  115. Upon being computed it replaces itself with an ordinary attribute. Deleting
  116. the attribute resets the property.
  117. Source: https://github.com/bottlepy/bottle/blob/1de24157e74a6971d136550afe1b63eec5b0df2b/bottle.py#L234-L246
  118. """ # noqa: E501
  119. def __init__(self, func):
  120. update_wrapper(self, func)
  121. self.func = func
  122. def __get__(self, obj, cls):
  123. if obj is None:
  124. return self
  125. value = obj.__dict__[self.func.__name__] = self.func(obj)
  126. return value