From 83b6166daf7f335bb250559127bbde870bd6e2e1 Mon Sep 17 00:00:00 2001 From: facat Date: Sat, 22 Jun 2019 16:34:45 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BB=8E=E9=BB=98=E8=AE=A4=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E8=AF=BB=E5=8F=96config.json=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- download_speed/config.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/download_speed/config.py b/download_speed/config.py index 5433f3b..313bd4a 100644 --- a/download_speed/config.py +++ b/download_speed/config.py @@ -1,6 +1,8 @@ import argparse import json import sys +import os +import inspect print("Config is loaded.") _g_args = None @@ -23,8 +25,13 @@ def read_json(): global _g_args json_path = _g_args.config if json_path is None: - print("specify json path") - sys.exit() + default_config_file_path = find_config_file_path() + if not os.path.exists(default_config_file_path): + print("specify json path") + 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: js = json.load(json_f) return js @@ -57,6 +64,16 @@ def get(_setting): 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() setting = _json_obj