| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import configparser
- import os
- class ReadConfig:
- """定义一个读取配置文件的类"""
- def __init__(self, filepath=None):
- # 从环境变量中获取项目目录
- projectDir = os.getenv('AGV_DIR')
- if filepath:
- self.configpath = filepath
- else:
- if projectDir:
- root_dir = projectDir
- else:
- root_dir = os.path.dirname(os.path.abspath(__file__))
- root_dir = root_dir.replace("common", "")
- self.configpath = os.path.join(root_dir, "config.ini")
- self.cf = configparser.ConfigParser()
- self.cf.read(self.configpath, encoding='UTF-8')
- def get_common_port(self, param):
- value = self.cf.get("common_port", param)
- return value
- def get_constant(self, param):
- value = self.cf.get("constant", param)
- return value
- def get_redis(self, param):
- value = self.cf.get("redis", param)
- return value
- def get_redis_slave(self, param):
- value = self.cf.get("redis_slave", param)
- return value
- def get_postgresql_db(self, param):
- value = self.cf.get("PostgreSQL-Database", param)
- return value
- def get_mysql_db(self, param):
- value = self.cf.get("MySQL-Database", param)
- return value
- if __name__ == '__main__':
- test = ReadConfig()
- t = test.get_common_port("single_board_com")
- print(t, "===============", t)
- # default_speed = test.get_plc_config("default_speed")
- # print(default_speed, "===============", type(default_speed))
- # print(int("0x80", 16))
- # ReadConfig().get_constant("channel_distance")
|