You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.3 KiB

4 years ago
  1. #!/usr/bin/python
  2. # -- Content-Encoding: UTF-8 --
  3. """
  4. Utility methods, for compatibility between Python version
  5. :author: Thomas Calmant
  6. :copyright: Copyright 2018, Thomas Calmant
  7. :license: Apache License 2.0
  8. :version: 0.3.2
  9. ..
  10. Copyright 2018 Thomas Calmant
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. """
  21. import sys
  22. # ------------------------------------------------------------------------------
  23. # Module version
  24. __version_info__ = (0, 3, 2)
  25. __version__ = ".".join(str(x) for x in __version_info__)
  26. # Documentation strings format
  27. __docformat__ = "restructuredtext en"
  28. # ------------------------------------------------------------------------------
  29. if sys.version_info[0] < 3:
  30. # Python 2
  31. # pylint: disable=E1101
  32. import types
  33. try:
  34. STRING_TYPES = (
  35. types.StringType,
  36. types.UnicodeType
  37. )
  38. except NameError:
  39. # Python built without unicode support
  40. STRING_TYPES = (types.StringType,)
  41. NUMERIC_TYPES = (
  42. types.IntType,
  43. types.LongType,
  44. types.FloatType
  45. )
  46. def to_bytes(string):
  47. """
  48. Converts the given string into bytes
  49. """
  50. # pylint: disable=E0602
  51. if type(string) is unicode:
  52. return str(string)
  53. return string
  54. def from_bytes(data):
  55. """
  56. Converts the given bytes into a string
  57. """
  58. if type(data) is str:
  59. return data
  60. return str(data)
  61. else:
  62. # Python 3
  63. # pylint: disable=E1101
  64. STRING_TYPES = (
  65. bytes,
  66. str
  67. )
  68. NUMERIC_TYPES = (
  69. int,
  70. float
  71. )
  72. def to_bytes(string):
  73. """
  74. Converts the given string into bytes
  75. """
  76. if type(string) is bytes:
  77. return string
  78. return bytes(string, "UTF-8")
  79. def from_bytes(data):
  80. """
  81. Converts the given bytes into a string
  82. """
  83. if type(data) is str:
  84. return data
  85. return str(data, "UTF-8")
  86. # ------------------------------------------------------------------------------
  87. # Enumerations
  88. try:
  89. import enum
  90. def is_enum(obj):
  91. """
  92. Checks if an object is from an enumeration class
  93. :param obj: Object to test
  94. :return: True if the object is an enumeration item
  95. """
  96. return isinstance(obj, enum.Enum)
  97. except ImportError:
  98. # Pre-Python 3.4
  99. def is_enum(_):
  100. """
  101. Before Python 3.4, enumerations didn't exist.
  102. :param _: Object to test
  103. :return: Always False
  104. """
  105. return False
  106. # ------------------------------------------------------------------------------
  107. # Common
  108. DictType = dict
  109. ListType = list
  110. TupleType = tuple
  111. ITERABLE_TYPES = (
  112. list,
  113. set, frozenset,
  114. tuple
  115. )
  116. VALUE_TYPES = (
  117. bool,
  118. type(None)
  119. )
  120. PRIMITIVE_TYPES = STRING_TYPES + NUMERIC_TYPES + VALUE_TYPES