DbClient.py 1.9 KB

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