api_impl.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #
  2. # Metrix++, Copyright 2009-2019, Metrix++ Project
  3. # Link: https://github.com/metrixplusplus/metrixplusplus
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. class PackagerError(Exception):
  8. def __init__(self, message=None):
  9. if message == None:
  10. Exception.__init__(self, "Failed to pack or unpack.")
  11. else:
  12. Exception.__init__(self, message)
  13. class PackagerFactory(object):
  14. def create(self, python_type, non_zero):
  15. if python_type == None:
  16. return PackagerFactory.SkipPackager()
  17. if python_type == int:
  18. if non_zero == False:
  19. return PackagerFactory.IntPackager()
  20. else:
  21. return PackagerFactory.IntNonZeroPackager()
  22. if python_type == float and non_zero == False:
  23. return PackagerFactory.FloatPackager()
  24. if python_type == str:
  25. return PackagerFactory.StringPackager()
  26. class PackagerFactoryError(Exception):
  27. def __init__(self, python_type):
  28. Exception.__init__(self, "Python type '" + str(python_type) + "' is not supported by the factory.")
  29. raise PackagerFactoryError(python_type)
  30. def get_python_type(self, sql_type):
  31. if sql_type == "integer":
  32. return int
  33. if sql_type == "real":
  34. return float
  35. if sql_type == "text":
  36. return str
  37. class PackagerFactoryError(Exception):
  38. def __init__(self, sql_type):
  39. Exception.__init__(self, "SQL type '" + str(sql_type) + "' is not supported by the factory.")
  40. raise PackagerFactoryError(sql_type)
  41. class IPackager(object):
  42. def pack(self, unpacked_data):
  43. assert False, "Internal interface not implemented"
  44. def unpack(self, packed_data):
  45. assert False, "Internal interface not implemented"
  46. def get_sql_type(self):
  47. assert False, "Internal interface not implemented"
  48. def get_python_type(self):
  49. assert False, "Internal interface not implemented"
  50. def is_non_zero(self):
  51. return False
  52. class IntPackager(IPackager):
  53. def pack(self, unpacked_data):
  54. if not isinstance(unpacked_data, int):
  55. raise PackagerError()
  56. return str(unpacked_data)
  57. def unpack(self, packed_data):
  58. try:
  59. return int(packed_data)
  60. except ValueError:
  61. raise PackagerError()
  62. def get_sql_type(self):
  63. return "integer"
  64. def get_python_type(self):
  65. return int
  66. class IntNonZeroPackager(IntPackager):
  67. def pack(self, unpacked_data):
  68. if unpacked_data == 0:
  69. raise PackagerError()
  70. return PackagerFactory.IntPackager.pack(self, unpacked_data)
  71. def is_non_zero(self):
  72. return True
  73. class FloatPackager(IPackager):
  74. def pack(self, unpacked_data):
  75. if not isinstance(unpacked_data, float):
  76. raise PackagerError()
  77. return str(unpacked_data)
  78. def unpack(self, packed_data):
  79. try:
  80. return float(packed_data)
  81. except ValueError:
  82. raise PackagerError()
  83. def get_sql_type(self):
  84. return "real"
  85. def get_python_type(self):
  86. return float
  87. class FloatNonZeroPackager(FloatPackager):
  88. def pack(self, unpacked_data):
  89. if unpacked_data == 0:
  90. raise PackagerError()
  91. return PackagerFactory.FloatPackager.pack(self, unpacked_data)
  92. def is_non_zero(self):
  93. return True
  94. class StringPackager(IPackager):
  95. def pack(self, unpacked_data):
  96. if not isinstance(unpacked_data, str):
  97. raise PackagerError()
  98. return str(unpacked_data)
  99. def unpack(self, packed_data):
  100. try:
  101. return str(packed_data)
  102. except ValueError:
  103. raise PackagerError()
  104. def get_sql_type(self):
  105. return "text"
  106. def get_python_type(self):
  107. return str
  108. class SkipPackager(IPackager):
  109. def pack(self, unpacked_data):
  110. return None
  111. def unpack(self, packed_data):
  112. return None
  113. def get_sql_type(self):
  114. return None
  115. def get_python_type(self):
  116. return None