GetConfig.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 db_username(self):
  48. try:
  49. username = self.config_file.get('DB', 'username')
  50. except Exception:
  51. username = None
  52. return username
  53. @LazyProperty
  54. def proxy_getter_functions(self):
  55. return self.config_file.options('ProxyGetter')
  56. @LazyProperty
  57. def host_ip(self):
  58. return self.config_file.get('API','ip')
  59. @LazyProperty
  60. def host_port(self):
  61. return int(self.config_file.get('API', 'port'))
  62. @LazyProperty
  63. def processes(self):
  64. return int(self.config_file.get('API', 'processes'))
  65. if __name__ == '__main__':
  66. gg = GetConfig()
  67. print(gg.db_type)
  68. print(gg.db_name)
  69. print(gg.db_host)
  70. print(gg.db_port)
  71. print(gg.proxy_getter_functions)
  72. print(gg.host_ip)
  73. print(gg.host_port)
  74. print(gg.processes)