From c870d72287fe30bb854be7bee485ec6716d35e16 Mon Sep 17 00:00:00 2001 From: LittleYe233 <30514318+LittleYe233@users.noreply.github.com> Date: Tue, 22 Feb 2022 22:29:33 +0800 Subject: [PATCH] Reconstruct configuration files Now there is a user configuration file to avoid overwriting these files when updating the repository. --- .gitignore | 3 +++ README.md | 33 ++++++++++++++++++++++++++++----- auto_comment_plus.py | 11 ++++++++++- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index b6e4761..fa32c9f 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# user config +config.user.yml \ No newline at end of file diff --git a/README.md b/README.md index e263041..5557050 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,20 @@ cd jd_AutoComment pip install -r requirements.txt ``` -获取电脑版ck后填入 `config.yml` 文件: +获取电脑版ck后填入配置文件。可以选择填入默认配置文件 `config.yml` ;也可以填入用户配置文件 `config.user.yml` (需要新建后将 `config.yml` 中的内容复制到该文件中),避免后续的更新覆盖 `config.yml` 中的内容。 + +需要填入如下内容: + +```yml +user: + cookie: '' +``` + +例如,若获取得到的ck为 `a=1; b=2; c=3` ,则配置文件中填入: ```yml user: - cookie: '' + cookie: 'a=1; b=2; c=3' ``` 最后运行 `auto_comment_plus.py` : @@ -56,11 +65,15 @@ python3 auto_comment_plus.py 本程序支持命令行参数: ```text -usage: auto_comment_plus.py [-h] [--dry-run] +usage: auto_comment_plus.py [-h] [--dry-run] [--log-level LOG_LEVEL] [-o LOG_FILE] optional arguments: - -h, --help show this help message and exit - --dry-run have a full run without comment submission + -h, --help show this help message and exit + --dry-run have a full run without comment submission + --log-level LOG_LEVEL + specify logging level (default: info) + -o LOG_FILE, --log-file LOG_FILE + specify logging file ``` **`-h`, `--help`:** @@ -71,6 +84,16 @@ optional arguments: 完整地运行程序,但不实际提交评论。 +**`--log-level LOG_LEVEL`:** + +设置输出日志的等级。默认为 `INFO` 。可选等级为 `DEBUG`、`INFO`、`WARNING`、`ERROR` ,输出内容量依次递减。 + +**注意:** 若你需要提交 issue 来报告一个 bug ,请将该选项设置为 `DEBUG` 。 + +**`-o LOG_FILE`:** + +设置输出日志文件的路径。若无此选项,则不输出到文件。 + ## 声明 本项目为Python学习交流的开源非营利项目,仅作为程序员之间相互学习交流之用。 diff --git a/auto_comment_plus.py b/auto_comment_plus.py index 2462465..1a65a98 100644 --- a/auto_comment_plus.py +++ b/auto_comment_plus.py @@ -6,6 +6,7 @@ import argparse import copy import logging +import os import random import sys import time @@ -20,6 +21,7 @@ # constants CONFIG_PATH = './config.yml' +USER_CONFIG_PATH = './config.user.yml' ORDINARY_SLEEP_SEC = 10 SUNBW_SLEEP_SEC = 5 REVIEW_SLEEP_SEC = 10 @@ -641,6 +643,7 @@ def main(opts=None): logger.debug('Options passed to functions: %s', opts) logger.debug('Builtin constants:') logger.debug(' CONFIG_PATH: %s', CONFIG_PATH) + logger.debug(' USER_CONFIG_PATH: %s', USER_CONFIG_PATH) logger.debug(' ORDINARY_SLEEP_SEC: %s', ORDINARY_SLEEP_SEC) logger.debug(' SUNBW_SLEEP_SEC: %s', SUNBW_SLEEP_SEC) logger.debug(' REVIEW_SLEEP_SEC: %s', REVIEW_SLEEP_SEC) @@ -648,7 +651,13 @@ def main(opts=None): # parse configurations logger.debug('Reading the configuration file') - with open(CONFIG_PATH, 'r', encoding='utf-8') as f: + if os.path.exists(USER_CONFIG_PATH): + logger.debug('User configuration file exists') + _cfg_path = USER_CONFIG_PATH + else: + logger.debug('User configuration file doesn\'t exist, fallback to the default one') + _cfg_path = CONFIG_PATH + with open(_cfg_path, 'r', encoding='utf-8') as f: cfg = yaml.safe_load(f) logger.debug('Closed the configuration file') logger.debug('Configurations in Python-dict format: %s', cfg)