compat.py 4.3 KB

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