From f9bff910bb1889f8d713830afec9bc578c1628e0 Mon Sep 17 00:00:00 2001 From: facat Date: Sat, 22 Jun 2019 15:12:08 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9A=84=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- download_speed/config.py | 43 ++++++++++++++++++++++-------------- download_speed/downloader.py | 3 +++ download_speed/model.py | 6 ++++- main.py | 8 +------ 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/download_speed/config.py b/download_speed/config.py index 0c235d7..1dd1e1f 100644 --- a/download_speed/config.py +++ b/download_speed/config.py @@ -1,14 +1,14 @@ import argparse import json import sys - -g_args = None -json_obj = None +print('Config is loaded.') +_g_args = None +_json_obj = None def get_servers(): - global json_obj - js = json_obj + global _json_obj + js = _json_obj _servers = js["servers"] servers_dic = {} for s in _servers: @@ -19,8 +19,8 @@ def get_servers(): def read_json(): - global g_args - json_path = g_args.config + global _g_args + json_path = _g_args.config if json_path is None: print("specify json path") sys.exit() @@ -30,23 +30,32 @@ def read_json(): def get_config(): - global json_obj - return json_obj + global _json_obj + return _json_obj def init_config(): - global g_args - if g_args is not None: - return g_args + global _g_args + if _g_args is not None: + return _g_args parser = argparse.ArgumentParser(description="Help") parser.add_argument("-config", type=str) _args = parser.parse_args() - g_args = _args - global json_obj - json_obj = read_json() + _g_args = _args + global _json_obj + _json_obj = read_json() return _args +def get(_setting): + if _setting in setting: + return setting[_setting] + else: + return None + + +init_config() +setting = _json_obj + if __name__ == "__main__": - args = init_config() - print(args.config) + pass diff --git a/download_speed/downloader.py b/download_speed/downloader.py index c0cce84..70d2a9f 100644 --- a/download_speed/downloader.py +++ b/download_speed/downloader.py @@ -6,6 +6,9 @@ from download_speed import config, model # file_size if byte # longest_time seconds def download_file(chunk_size=4 * 1024, file_size=100 * 1024 * 1024, longest_time=60): + config_file_size = config.get("download_file_size") + if config_file_size is not None: + file_size = config_file_size execution_datetime = datetime.datetime.now() print("Download at {time}".format(time=execution_datetime)) print( diff --git a/download_speed/model.py b/download_speed/model.py index 4357d0b..8160886 100644 --- a/download_speed/model.py +++ b/download_speed/model.py @@ -3,13 +3,15 @@ import datetime from download_speed import config config.init_config() -mysql_addr = config.read_json() +mysql_addr = config.get('mysql_addr') mysql_db = MySQLDatabase( "vps", user="dmy", password="abc123+_", host=mysql_addr, port=3306 ) def add_record(execution_datetime, server, speed): + if not config.get('mysql_enable'): + return execution_datetime_utc = execution_datetime - datetime.timedelta( hours=8 ) # 转换为UTC时间。 @@ -22,6 +24,8 @@ def add_record(execution_datetime, server, speed): def add_ping_record(execution_datetime, server, ping): + if not config.get('mysql_enable'): + return execution_datetime_utc = execution_datetime - datetime.timedelta( hours=8 ) # 转换为UTC时间。 diff --git a/main.py b/main.py index 147e922..324303f 100644 --- a/main.py +++ b/main.py @@ -3,11 +3,5 @@ from download_speed import downloader, config if __name__ == "__main__": - config.init_config() - setting = config.get_config() - if "download_file_size" in setting: - download_file_size = setting["download_file_size"] - downloader.download_file(file_size=download_file_size) - else: - downloader.download_file() + downloader.download_file() print("Finished.")