DbClient.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: DbClient.py
  5. Description : DB工厂类
  6. Author : JHao
  7. date: 2016/12/2
  8. -------------------------------------------------
  9. Change Activity:
  10. 2016/12/2:
  11. -------------------------------------------------
  12. """
  13. __author__ = 'JHao'
  14. import os
  15. import sys
  16. from Util.GetConfig import GetConfig
  17. from Util.utilClass import Singleton
  18. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  19. class DbClient(object):
  20. """
  21. DbClient
  22. """
  23. __metaclass__ = Singleton
  24. def __init__(self):
  25. """
  26. init
  27. :return:
  28. """
  29. self.config = GetConfig()
  30. self.__initDbClient()
  31. def __initDbClient(self):
  32. """
  33. init DB Client
  34. :return:
  35. """
  36. __type = None
  37. if "SSDB" == self.config.db_type:
  38. __type = "SsdbClient"
  39. else:
  40. pass
  41. assert __type, 'type error, Not support DB type: {}'.format(self.config.db_type)
  42. self.client = getattr(__import__(__type), __type)(name=self.config.db_name,
  43. host=self.config.db_host,
  44. port=self.config.db_port)
  45. def get(self, **kwargs):
  46. return self.client.get(**kwargs)
  47. def put(self, value, **kwargs):
  48. return self.client.put(value, **kwargs)
  49. def pop(self, **kwargs):
  50. return self.client.pop(**kwargs)
  51. def delete(self, value, **kwargs):
  52. return self.client.delete(value, **kwargs)
  53. def getAll(self):
  54. return self.client.getAll()
  55. def changeTable(self, name):
  56. self.client.changeTable(name)
  57. if __name__ == "__main__":
  58. account = DbClient()
  59. print account.get()
  60. account.changeTable('use')
  61. account.put('ac')
  62. print(account)