compat.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from __future__ import absolute_import
  2. import sys
  3. PY3 = sys.version>'3'
  4. if PY3:
  5. long = int
  6. basestring = str
  7. unichr = chr
  8. raw_input = input
  9. class unicode(str):
  10. def __init__(self, string, encoding, errors):
  11. str.__init__(self, string)
  12. def as_bytes(s): return s if isinstance(s,bytes) else s.encode('utf8')
  13. def as_string(s): return s if isinstance(s,str) else s.decode('utf8')
  14. from functools import reduce
  15. else:
  16. long = long
  17. raw_input = raw_input
  18. unicode = unicode
  19. basestring = basestring
  20. def as_bytes(s): return s if isinstance(s, str) else s.encode('utf-8')
  21. def as_string(s): return s if isinstance(s, unicode) else s.decode('utf-8')
  22. reduce = reduce
  23. def print_function(*args,**kwargs): sys.stdout.write(' '.join(str(i) for i in args)+kwargs.get('end','\n'))
  24. def total_ordering(cls): # pragma: no cover
  25. """Class decorator that fills in missing ordering methods"""
  26. convert = {
  27. '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
  28. ('__le__', lambda self, other: self < other or self == other),
  29. ('__ge__', lambda self, other: not self < other)],
  30. '__le__': [('__ge__', lambda self, other: not self <= other or self == other),
  31. ('__lt__', lambda self, other: self <= other and not self == other),
  32. ('__gt__', lambda self, other: not self <= other)],
  33. '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
  34. ('__ge__', lambda self, other: self > other or self == other),
  35. ('__le__', lambda self, other: not self > other)],
  36. '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
  37. ('__gt__', lambda self, other: self >= other and not self == other),
  38. ('__lt__', lambda self, other: not self >= other)]
  39. }
  40. roots = set(dir(cls)) & set(convert)
  41. if not roots:
  42. raise ValueError('must define at least one ordering operation: < > <= >=')
  43. root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
  44. for opname, opfunc in convert[root]:
  45. if opname not in roots:
  46. opfunc.__name__ = opname
  47. try:
  48. op = getattr(int, opname)
  49. except AttributeError: # py25 int has no __gt__
  50. pass
  51. else:
  52. opfunc.__doc__ = op.__doc__
  53. setattr(cls, opname, opfunc)
  54. return cls
  55. try:
  56. import xmlrpc.client as xmlrpclib
  57. except ImportError:
  58. import xmlrpclib
  59. try:
  60. import urllib.parse as urlparse
  61. import urllib.parse as urllib
  62. except ImportError:
  63. import urlparse
  64. import urllib
  65. if PY3:
  66. from base64 import encodebytes as encodestring
  67. else:
  68. from base64 import encodestring
  69. try:
  70. from hashlib import sha1
  71. except ImportError:
  72. from sha import new as sha1
  73. try:
  74. import syslog
  75. except ImportError:
  76. syslog = None
  77. try:
  78. import configparser as ConfigParser
  79. except ImportError:
  80. import ConfigParser
  81. try:
  82. from StringIO import StringIO
  83. except ImportError:
  84. from io import StringIO
  85. try:
  86. from sys import maxint
  87. except ImportError:
  88. from sys import maxsize as maxint
  89. try:
  90. from urllib.parse import parse_qs, parse_qsl
  91. except ImportError:
  92. from cgi import parse_qs, parse_qsl
  93. try:
  94. import http.client as httplib
  95. except ImportError:
  96. import httplib
  97. try:
  98. from base64 import decodebytes as decodestring, encodebytes as encodestring
  99. except ImportError:
  100. from base64 import decodestring, encodestring
  101. if PY3:
  102. func_attribute = '__func__'
  103. else:
  104. func_attribute = 'im_func'
  105. try:
  106. # Python 2.6 contains a version of cElementTree inside it.
  107. from xml.etree.ElementTree import iterparse
  108. except ImportError:
  109. try:
  110. # Failing that, try cElementTree instead.
  111. from cElementTree import iterparse
  112. except ImportError:
  113. iterparse = None
  114. try:
  115. from unittest.mock import Mock, patch, sentinel
  116. except ImportError:
  117. from mock import Mock, patch, sentinel
  118. try:
  119. import unittest.mock as mock
  120. except ImportError:
  121. import mock
  122. try:
  123. from xmlrpc.client import Fault
  124. except ImportError:
  125. from xmlrpclib import Fault
  126. try:
  127. from string import ascii_letters as letters
  128. except ImportError:
  129. from string import letters
  130. try:
  131. from hashlib import md5
  132. except ImportError:
  133. from md5 import md5
  134. try:
  135. import thread
  136. except ImportError:
  137. import _thread as thread