1.从默认位置读取config.json文件

This commit is contained in:
facat 2019-06-22 16:34:45 +08:00
parent d0e0ca03cf
commit 83b6166daf
1 changed files with 19 additions and 2 deletions

View File

@ -1,6 +1,8 @@
import argparse import argparse
import json import json
import sys import sys
import os
import inspect
print("Config is loaded.") print("Config is loaded.")
_g_args = None _g_args = None
@ -23,8 +25,13 @@ def read_json():
global _g_args global _g_args
json_path = _g_args.config json_path = _g_args.config
if json_path is None: if json_path is None:
default_config_file_path = find_config_file_path()
if not os.path.exists(default_config_file_path):
print("specify json path") print("specify json path")
sys.exit() sys.exit()
else:
json_path = default_config_file_path
print("load config from {file_path}".format(file_path=json_path))
with open(json_path, "r", encoding="utf-8") as json_f: with open(json_path, "r", encoding="utf-8") as json_f:
js = json.load(json_f) js = json.load(json_f)
return js return js
@ -57,6 +64,16 @@ def get(_setting):
return None return None
def find_config_file_path():
current_frame_file_path = os.path.dirname(
os.path.realpath(inspect.getfile(inspect.currentframe()))
)
config_file_dir = os.path.dirname(current_frame_file_path)
config_file_path = config_file_dir + "/config.json"
config_file_path = config_file_path.replace("\\", r"/")
return config_file_path
init_config() init_config()
setting = _json_obj setting = _json_obj