read_config.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import configparser
  2. import os
  3. class ReadConfig:
  4. """定义一个读取配置文件的类"""
  5. def __init__(self, filepath=None):
  6. # 从环境变量中获取项目目录
  7. projectDir = os.getenv('AGV_DIR')
  8. if filepath:
  9. self.configpath = filepath
  10. else:
  11. if projectDir:
  12. root_dir = projectDir
  13. else:
  14. root_dir = os.path.dirname(os.path.abspath(__file__))
  15. root_dir = root_dir.replace("common", "")
  16. self.configpath = os.path.join(root_dir, "config.ini")
  17. self.cf = configparser.ConfigParser()
  18. self.cf.read(self.configpath, encoding='UTF-8')
  19. def get_common_port(self, param):
  20. value = self.cf.get("common_port", param)
  21. return value
  22. def get_constant(self, param):
  23. value = self.cf.get("constant", param)
  24. return value
  25. def get_redis(self, param):
  26. value = self.cf.get("redis", param)
  27. return value
  28. def get_redis_slave(self, param):
  29. value = self.cf.get("redis_slave", param)
  30. return value
  31. def get_postgresql_db(self, param):
  32. value = self.cf.get("PostgreSQL-Database", param)
  33. return value
  34. def get_mysql_db(self, param):
  35. value = self.cf.get("MySQL-Database", param)
  36. return value
  37. if __name__ == '__main__':
  38. test = ReadConfig()
  39. t = test.get_common_port("single_board_com")
  40. print(t, "===============", t)
  41. # default_speed = test.get_plc_config("default_speed")
  42. # print(default_speed, "===============", type(default_speed))
  43. # print(int("0x80", 16))
  44. # ReadConfig().get_constant("channel_distance")