brain_http.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
  4. """Astroid brain hints for some of the `http` module."""
  5. import textwrap
  6. from astroid.brain.helpers import register_module_extender
  7. from astroid.builder import AstroidBuilder
  8. from astroid.manager import AstroidManager
  9. def _http_transform():
  10. code = textwrap.dedent(
  11. """
  12. from enum import IntEnum
  13. from collections import namedtuple
  14. _HTTPStatus = namedtuple('_HTTPStatus', 'value phrase description')
  15. class HTTPStatus(IntEnum):
  16. @property
  17. def phrase(self):
  18. return ""
  19. @property
  20. def value(self):
  21. return 0
  22. @property
  23. def description(self):
  24. return ""
  25. # informational
  26. CONTINUE = _HTTPStatus(100, 'Continue', 'Request received, please continue')
  27. SWITCHING_PROTOCOLS = _HTTPStatus(101, 'Switching Protocols',
  28. 'Switching to new protocol; obey Upgrade header')
  29. PROCESSING = _HTTPStatus(102, 'Processing', '')
  30. OK = _HTTPStatus(200, 'OK', 'Request fulfilled, document follows')
  31. CREATED = _HTTPStatus(201, 'Created', 'Document created, URL follows')
  32. ACCEPTED = _HTTPStatus(202, 'Accepted',
  33. 'Request accepted, processing continues off-line')
  34. NON_AUTHORITATIVE_INFORMATION = _HTTPStatus(203,
  35. 'Non-Authoritative Information', 'Request fulfilled from cache')
  36. NO_CONTENT = _HTTPStatus(204, 'No Content', 'Request fulfilled, nothing follows')
  37. RESET_CONTENT =_HTTPStatus(205, 'Reset Content', 'Clear input form for further input')
  38. PARTIAL_CONTENT = _HTTPStatus(206, 'Partial Content', 'Partial content follows')
  39. MULTI_STATUS = _HTTPStatus(207, 'Multi-Status', '')
  40. ALREADY_REPORTED = _HTTPStatus(208, 'Already Reported', '')
  41. IM_USED = _HTTPStatus(226, 'IM Used', '')
  42. MULTIPLE_CHOICES = _HTTPStatus(300, 'Multiple Choices',
  43. 'Object has several resources -- see URI list')
  44. MOVED_PERMANENTLY = _HTTPStatus(301, 'Moved Permanently',
  45. 'Object moved permanently -- see URI list')
  46. FOUND = _HTTPStatus(302, 'Found', 'Object moved temporarily -- see URI list')
  47. SEE_OTHER = _HTTPStatus(303, 'See Other', 'Object moved -- see Method and URL list')
  48. NOT_MODIFIED = _HTTPStatus(304, 'Not Modified',
  49. 'Document has not changed since given time')
  50. USE_PROXY = _HTTPStatus(305, 'Use Proxy',
  51. 'You must use proxy specified in Location to access this resource')
  52. TEMPORARY_REDIRECT = _HTTPStatus(307, 'Temporary Redirect',
  53. 'Object moved temporarily -- see URI list')
  54. PERMANENT_REDIRECT = _HTTPStatus(308, 'Permanent Redirect',
  55. 'Object moved permanently -- see URI list')
  56. BAD_REQUEST = _HTTPStatus(400, 'Bad Request',
  57. 'Bad request syntax or unsupported method')
  58. UNAUTHORIZED = _HTTPStatus(401, 'Unauthorized',
  59. 'No permission -- see authorization schemes')
  60. PAYMENT_REQUIRED = _HTTPStatus(402, 'Payment Required',
  61. 'No payment -- see charging schemes')
  62. FORBIDDEN = _HTTPStatus(403, 'Forbidden',
  63. 'Request forbidden -- authorization will not help')
  64. NOT_FOUND = _HTTPStatus(404, 'Not Found',
  65. 'Nothing matches the given URI')
  66. METHOD_NOT_ALLOWED = _HTTPStatus(405, 'Method Not Allowed',
  67. 'Specified method is invalid for this resource')
  68. NOT_ACCEPTABLE = _HTTPStatus(406, 'Not Acceptable',
  69. 'URI not available in preferred format')
  70. PROXY_AUTHENTICATION_REQUIRED = _HTTPStatus(407,
  71. 'Proxy Authentication Required',
  72. 'You must authenticate with this proxy before proceeding')
  73. REQUEST_TIMEOUT = _HTTPStatus(408, 'Request Timeout',
  74. 'Request timed out; try again later')
  75. CONFLICT = _HTTPStatus(409, 'Conflict', 'Request conflict')
  76. GONE = _HTTPStatus(410, 'Gone',
  77. 'URI no longer exists and has been permanently removed')
  78. LENGTH_REQUIRED = _HTTPStatus(411, 'Length Required',
  79. 'Client must specify Content-Length')
  80. PRECONDITION_FAILED = _HTTPStatus(412, 'Precondition Failed',
  81. 'Precondition in headers is false')
  82. REQUEST_ENTITY_TOO_LARGE = _HTTPStatus(413, 'Request Entity Too Large',
  83. 'Entity is too large')
  84. REQUEST_URI_TOO_LONG = _HTTPStatus(414, 'Request-URI Too Long',
  85. 'URI is too long')
  86. UNSUPPORTED_MEDIA_TYPE = _HTTPStatus(415, 'Unsupported Media Type',
  87. 'Entity body in unsupported format')
  88. REQUESTED_RANGE_NOT_SATISFIABLE = _HTTPStatus(416,
  89. 'Requested Range Not Satisfiable',
  90. 'Cannot satisfy request range')
  91. EXPECTATION_FAILED = _HTTPStatus(417, 'Expectation Failed',
  92. 'Expect condition could not be satisfied')
  93. MISDIRECTED_REQUEST = _HTTPStatus(421, 'Misdirected Request',
  94. 'Server is not able to produce a response')
  95. UNPROCESSABLE_ENTITY = _HTTPStatus(422, 'Unprocessable Entity')
  96. LOCKED = _HTTPStatus(423, 'Locked')
  97. FAILED_DEPENDENCY = _HTTPStatus(424, 'Failed Dependency')
  98. UPGRADE_REQUIRED = _HTTPStatus(426, 'Upgrade Required')
  99. PRECONDITION_REQUIRED = _HTTPStatus(428, 'Precondition Required',
  100. 'The origin server requires the request to be conditional')
  101. TOO_MANY_REQUESTS = _HTTPStatus(429, 'Too Many Requests',
  102. 'The user has sent too many requests in '
  103. 'a given amount of time ("rate limiting")')
  104. REQUEST_HEADER_FIELDS_TOO_LARGE = _HTTPStatus(431,
  105. 'Request Header Fields Too Large',
  106. 'The server is unwilling to process the request because its header '
  107. 'fields are too large')
  108. UNAVAILABLE_FOR_LEGAL_REASONS = _HTTPStatus(451,
  109. 'Unavailable For Legal Reasons',
  110. 'The server is denying access to the '
  111. 'resource as a consequence of a legal demand')
  112. INTERNAL_SERVER_ERROR = _HTTPStatus(500, 'Internal Server Error',
  113. 'Server got itself in trouble')
  114. NOT_IMPLEMENTED = _HTTPStatus(501, 'Not Implemented',
  115. 'Server does not support this operation')
  116. BAD_GATEWAY = _HTTPStatus(502, 'Bad Gateway',
  117. 'Invalid responses from another server/proxy')
  118. SERVICE_UNAVAILABLE = _HTTPStatus(503, 'Service Unavailable',
  119. 'The server cannot process the request due to a high load')
  120. GATEWAY_TIMEOUT = _HTTPStatus(504, 'Gateway Timeout',
  121. 'The gateway server did not receive a timely response')
  122. HTTP_VERSION_NOT_SUPPORTED = _HTTPStatus(505, 'HTTP Version Not Supported',
  123. 'Cannot fulfill request')
  124. VARIANT_ALSO_NEGOTIATES = _HTTPStatus(506, 'Variant Also Negotiates')
  125. INSUFFICIENT_STORAGE = _HTTPStatus(507, 'Insufficient Storage')
  126. LOOP_DETECTED = _HTTPStatus(508, 'Loop Detected')
  127. NOT_EXTENDED = _HTTPStatus(510, 'Not Extended')
  128. NETWORK_AUTHENTICATION_REQUIRED = _HTTPStatus(511,
  129. 'Network Authentication Required',
  130. 'The client needs to authenticate to gain network access')
  131. """
  132. )
  133. return AstroidBuilder(AstroidManager()).string_build(code)
  134. def _http_client_transform():
  135. return AstroidBuilder(AstroidManager()).string_build(
  136. textwrap.dedent(
  137. """
  138. from http import HTTPStatus
  139. CONTINUE = HTTPStatus.CONTINUE
  140. SWITCHING_PROTOCOLS = HTTPStatus.SWITCHING_PROTOCOLS
  141. PROCESSING = HTTPStatus.PROCESSING
  142. OK = HTTPStatus.OK
  143. CREATED = HTTPStatus.CREATED
  144. ACCEPTED = HTTPStatus.ACCEPTED
  145. NON_AUTHORITATIVE_INFORMATION = HTTPStatus.NON_AUTHORITATIVE_INFORMATION
  146. NO_CONTENT = HTTPStatus.NO_CONTENT
  147. RESET_CONTENT = HTTPStatus.RESET_CONTENT
  148. PARTIAL_CONTENT = HTTPStatus.PARTIAL_CONTENT
  149. MULTI_STATUS = HTTPStatus.MULTI_STATUS
  150. ALREADY_REPORTED = HTTPStatus.ALREADY_REPORTED
  151. IM_USED = HTTPStatus.IM_USED
  152. MULTIPLE_CHOICES = HTTPStatus.MULTIPLE_CHOICES
  153. MOVED_PERMANENTLY = HTTPStatus.MOVED_PERMANENTLY
  154. FOUND = HTTPStatus.FOUND
  155. SEE_OTHER = HTTPStatus.SEE_OTHER
  156. NOT_MODIFIED = HTTPStatus.NOT_MODIFIED
  157. USE_PROXY = HTTPStatus.USE_PROXY
  158. TEMPORARY_REDIRECT = HTTPStatus.TEMPORARY_REDIRECT
  159. PERMANENT_REDIRECT = HTTPStatus.PERMANENT_REDIRECT
  160. BAD_REQUEST = HTTPStatus.BAD_REQUEST
  161. UNAUTHORIZED = HTTPStatus.UNAUTHORIZED
  162. PAYMENT_REQUIRED = HTTPStatus.PAYMENT_REQUIRED
  163. FORBIDDEN = HTTPStatus.FORBIDDEN
  164. NOT_FOUND = HTTPStatus.NOT_FOUND
  165. METHOD_NOT_ALLOWED = HTTPStatus.METHOD_NOT_ALLOWED
  166. NOT_ACCEPTABLE = HTTPStatus.NOT_ACCEPTABLE
  167. PROXY_AUTHENTICATION_REQUIRED = HTTPStatus.PROXY_AUTHENTICATION_REQUIRED
  168. REQUEST_TIMEOUT = HTTPStatus.REQUEST_TIMEOUT
  169. CONFLICT = HTTPStatus.CONFLICT
  170. GONE = HTTPStatus.GONE
  171. LENGTH_REQUIRED = HTTPStatus.LENGTH_REQUIRED
  172. PRECONDITION_FAILED = HTTPStatus.PRECONDITION_FAILED
  173. REQUEST_ENTITY_TOO_LARGE = HTTPStatus.REQUEST_ENTITY_TOO_LARGE
  174. REQUEST_URI_TOO_LONG = HTTPStatus.REQUEST_URI_TOO_LONG
  175. UNSUPPORTED_MEDIA_TYPE = HTTPStatus.UNSUPPORTED_MEDIA_TYPE
  176. REQUESTED_RANGE_NOT_SATISFIABLE = HTTPStatus.REQUESTED_RANGE_NOT_SATISFIABLE
  177. EXPECTATION_FAILED = HTTPStatus.EXPECTATION_FAILED
  178. UNPROCESSABLE_ENTITY = HTTPStatus.UNPROCESSABLE_ENTITY
  179. LOCKED = HTTPStatus.LOCKED
  180. FAILED_DEPENDENCY = HTTPStatus.FAILED_DEPENDENCY
  181. UPGRADE_REQUIRED = HTTPStatus.UPGRADE_REQUIRED
  182. PRECONDITION_REQUIRED = HTTPStatus.PRECONDITION_REQUIRED
  183. TOO_MANY_REQUESTS = HTTPStatus.TOO_MANY_REQUESTS
  184. REQUEST_HEADER_FIELDS_TOO_LARGE = HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE
  185. INTERNAL_SERVER_ERROR = HTTPStatus.INTERNAL_SERVER_ERROR
  186. NOT_IMPLEMENTED = HTTPStatus.NOT_IMPLEMENTED
  187. BAD_GATEWAY = HTTPStatus.BAD_GATEWAY
  188. SERVICE_UNAVAILABLE = HTTPStatus.SERVICE_UNAVAILABLE
  189. GATEWAY_TIMEOUT = HTTPStatus.GATEWAY_TIMEOUT
  190. HTTP_VERSION_NOT_SUPPORTED = HTTPStatus.HTTP_VERSION_NOT_SUPPORTED
  191. VARIANT_ALSO_NEGOTIATES = HTTPStatus.VARIANT_ALSO_NEGOTIATES
  192. INSUFFICIENT_STORAGE = HTTPStatus.INSUFFICIENT_STORAGE
  193. LOOP_DETECTED = HTTPStatus.LOOP_DETECTED
  194. NOT_EXTENDED = HTTPStatus.NOT_EXTENDED
  195. NETWORK_AUTHENTICATION_REQUIRED = HTTPStatus.NETWORK_AUTHENTICATION_REQUIRED
  196. """
  197. )
  198. )
  199. register_module_extender(AstroidManager(), "http", _http_transform)
  200. register_module_extender(AstroidManager(), "http.client", _http_client_transform)