1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- # -*- coding: utf-8 -*-
- # @Time : 2021/5/27 11:23
- # @Author : XuJiakai
- # @File : environment_switch
- # @Software: PyCharm
- import platform
- import subprocess
- import os
- import yaml
- _module_path = os.path.dirname(__file__)
- def __ping_judge(ip):
- CREATE_NO_WINDOW = 0x08000000
- win_ping_cmd = "ping -n 1 {}".format(ip)
- linux_ping_cmd = "ping -c 1 {}".format(ip)
- if platform.system() == "Windows":
- ret = subprocess.call(win_ping_cmd, shell=True, creationflags=CREATE_NO_WINDOW)
- if platform.system() == "Linux":
- ret = subprocess.call(linux_ping_cmd, shell=True)
- if ret == 0:
- return True
- if ret == 1:
- return False
- def _is_intranet_env():
- if platform.system() == "Windows":
- return False
- # return __ping_judge("oss-cn-shanghai-internal.aliyuncs.com")
- return True
- class environment_switch:
- def __init__(self):
- self.is_intranet = _is_intranet_env()
- self.config = self.__get_conf()
- pass
- def __get_conf(self):
- if self.is_intranet:
- f = open(os.path.join(_module_path, 'env-prod.yaml')) # 打开yaml文件
- pass
- else:
- f = open(os.path.join(_module_path, 'env-dev.yaml')) # 打开yaml文件
- pass
- d = yaml.load(f, Loader=yaml.FullLoader) # 使用load方法加载
- return d
- def get_val(self, path):
- return self.__get_val(self.config, path)
- def __get_val(self, conf, path):
- index = path.find('.')
- if index == -1 and path in conf:
- return conf[path]
- k = path[0: index]
- other_path = path[index + 1:]
- if k in conf:
- v = conf[k]
- return self.__get_val(v, other_path)
- return None
- project_env = environment_switch()
- if __name__ == '__main__':
- env = environment_switch()
- print(env.is_intranet)
- # vv = env.get_val('mongo.itslaw.url')
- # print(vv)
- pass
|