api_impl.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. class PackagerError(Exception):
  20. def __init__(self, message=None):
  21. if message == None:
  22. Exception.__init__(self, "Failed to pack or unpack.")
  23. else:
  24. Exception.__init__(self, message)
  25. class PackagerFactory(object):
  26. def create(self, python_type, non_zero):
  27. if python_type == None:
  28. return PackagerFactory.SkipPackager()
  29. if python_type == int:
  30. if non_zero == False:
  31. return PackagerFactory.IntPackager()
  32. else:
  33. return PackagerFactory.IntNonZeroPackager()
  34. if python_type == float and non_zero == False:
  35. return PackagerFactory.FloatPackager()
  36. if python_type == str:
  37. return PackagerFactory.StringPackager()
  38. class PackagerFactoryError(Exception):
  39. def __init__(self, python_type):
  40. Exception.__init__(self, "Python type '" + str(python_type) + "' is not supported by the factory.")
  41. raise PackagerFactoryError(python_type)
  42. def get_python_type(self, sql_type):
  43. if sql_type == "integer":
  44. return int
  45. if sql_type == "real":
  46. return float
  47. if sql_type == "text":
  48. return str
  49. class PackagerFactoryError(Exception):
  50. def __init__(self, sql_type):
  51. Exception.__init__(self, "SQL type '" + str(sql_type) + "' is not supported by the factory.")
  52. raise PackagerFactoryError(sql_type)
  53. class IPackager(object):
  54. def pack(self, unpacked_data):
  55. assert False, "Internal interface not implemented"
  56. def unpack(self, packed_data):
  57. assert False, "Internal interface not implemented"
  58. def get_sql_type(self):
  59. assert False, "Internal interface not implemented"
  60. def get_python_type(self):
  61. assert False, "Internal interface not implemented"
  62. def is_non_zero(self):
  63. return False
  64. class IntPackager(IPackager):
  65. def pack(self, unpacked_data):
  66. if not isinstance(unpacked_data, int):
  67. raise PackagerError()
  68. return str(unpacked_data)
  69. def unpack(self, packed_data):
  70. try:
  71. return int(packed_data)
  72. except ValueError:
  73. raise PackagerError()
  74. def get_sql_type(self):
  75. return "integer"
  76. def get_python_type(self):
  77. return int
  78. class IntNonZeroPackager(IntPackager):
  79. def pack(self, unpacked_data):
  80. if unpacked_data == 0:
  81. raise PackagerError()
  82. return PackagerFactory.IntPackager.pack(self, unpacked_data)
  83. def is_non_zero(self):
  84. return True
  85. class FloatPackager(IPackager):
  86. def pack(self, unpacked_data):
  87. if not isinstance(unpacked_data, float):
  88. raise PackagerError()
  89. return str(unpacked_data)
  90. def unpack(self, packed_data):
  91. try:
  92. return float(packed_data)
  93. except ValueError:
  94. raise PackagerError()
  95. def get_sql_type(self):
  96. return "real"
  97. def get_python_type(self):
  98. return float
  99. class FloatNonZeroPackager(FloatPackager):
  100. def pack(self, unpacked_data):
  101. if unpacked_data == 0:
  102. raise PackagerError()
  103. return PackagerFactory.FloatPackager.pack(self, unpacked_data)
  104. def is_non_zero(self):
  105. return True
  106. class StringPackager(IPackager):
  107. def pack(self, unpacked_data):
  108. if not isinstance(unpacked_data, str):
  109. raise PackagerError()
  110. return str(unpacked_data)
  111. def unpack(self, packed_data):
  112. try:
  113. return str(packed_data)
  114. except ValueError:
  115. raise PackagerError()
  116. def get_sql_type(self):
  117. return "text"
  118. def get_python_type(self):
  119. return str
  120. class SkipPackager(IPackager):
  121. def pack(self, unpacked_data):
  122. return None
  123. def unpack(self, packed_data):
  124. return None
  125. def get_sql_type(self):
  126. return None
  127. def get_python_type(self):
  128. return None