GetConfig.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: GetConfig.py
  6. Description : fetch config from config.ini
  7. Author : JHao
  8. date: 2016/12/3
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/12/3: get db property func
  12. -------------------------------------------------
  13. """
  14. __author__ = 'JHao'
  15. import os
  16. from Util.utilClass import ConfigParse
  17. from Util.utilClass import LazyProperty
  18. class GetConfig(object):
  19. """
  20. to get config from config.ini
  21. """
  22. def __init__(self):
  23. self.pwd = os.path.split(os.path.realpath(__file__))[0]
  24. self.config_path = os.path.join(os.path.split(self.pwd)[0], 'Config.ini')
  25. self.config_file = ConfigParse()
  26. self.config_file.read(self.config_path)
  27. @LazyProperty
  28. def db_type(self):
  29. return self.config_file.get('DB', 'type')
  30. @LazyProperty
  31. def db_name(self):
  32. return self.config_file.get('DB', 'name')
  33. @LazyProperty
  34. def db_host(self):
  35. return self.config_file.get('DB', 'host')
  36. @LazyProperty
  37. def db_port(self):
  38. return int(self.config_file.get('DB', 'port'))
  39. @LazyProperty
  40. def db_password(self):
  41. try:
  42. password = self.config_file.get('DB', 'password')
  43. except Exception:
  44. password = None
  45. return password
  46. @LazyProperty
  47. def proxy_getter_functions(self):
  48. return self.config_file.options('ProxyGetter')
  49. @LazyProperty
  50. def host_ip(self):
  51. return self.config_file.get('API', 'ip')
  52. @LazyProperty
  53. def host_port(self):
  54. return int(self.config_file.get('API', 'port'))
  55. @LazyProperty
  56. def processes(self):
  57. return int(self.config_file.get('API', 'processes'))
  58. config = GetConfig()
  59. if __name__ == '__main__':
  60. gg = GetConfig()
  61. print(gg.db_type)
  62. print(gg.db_name)
  63. print(gg.db_host)
  64. print(gg.db_port)
  65. print(gg.proxy_getter_functions)
  66. print(gg.host_ip)
  67. print(gg.host_port)
  68. print(gg.processes)