compat.py 4.9 KB

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