Skip to content

Commit c870d72

Browse files
committed
Reconstruct configuration files
Now there is a user configuration file to avoid overwriting these files when updating the repository.
1 parent b8b32f7 commit c870d72

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,6 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# user config
132+
config.user.yml

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,20 @@ cd jd_AutoComment
3636
pip install -r requirements.txt
3737
```
3838

39-
获取电脑版ck后填入 `config.yml` 文件:
39+
获取电脑版ck后填入配置文件。可以选择填入默认配置文件 `config.yml` ;也可以填入用户配置文件 `config.user.yml` (需要新建后将 `config.yml` 中的内容复制到该文件中),避免后续的更新覆盖 `config.yml` 中的内容。
40+
41+
需要填入如下内容:
42+
43+
```yml
44+
user:
45+
cookie: '<Cookie>'
46+
```
47+
48+
例如,若获取得到的ck为 `a=1; b=2; c=3` ,则配置文件中填入:
4049

4150
```yml
4251
user:
43-
cookie: ''
52+
cookie: 'a=1; b=2; c=3'
4453
```
4554

4655
最后运行 `auto_comment_plus.py` :
@@ -56,11 +65,15 @@ python3 auto_comment_plus.py
5665
本程序支持命令行参数:
5766

5867
```text
59-
usage: auto_comment_plus.py [-h] [--dry-run]
68+
usage: auto_comment_plus.py [-h] [--dry-run] [--log-level LOG_LEVEL] [-o LOG_FILE]
6069
6170
optional arguments:
62-
-h, --help show this help message and exit
63-
--dry-run have a full run without comment submission
71+
-h, --help show this help message and exit
72+
--dry-run have a full run without comment submission
73+
--log-level LOG_LEVEL
74+
specify logging level (default: info)
75+
-o LOG_FILE, --log-file LOG_FILE
76+
specify logging file
6477
```
6578

6679
**`-h`, `--help`:**
@@ -71,6 +84,16 @@ optional arguments:
7184

7285
完整地运行程序,但不实际提交评论。
7386

87+
**`--log-level LOG_LEVEL`:**
88+
89+
设置输出日志的等级。默认为 `INFO` 。可选等级为 `DEBUG`、`INFO`、`WARNING`、`ERROR` ,输出内容量依次递减。
90+
91+
**注意:** 若你需要提交 issue 来报告一个 bug ,请将该选项设置为 `DEBUG` 。
92+
93+
**`-o LOG_FILE`:**
94+
95+
设置输出日志文件的路径。若无此选项,则不输出到文件。
96+
7497
## 声明
7598

7699
本项目为Python学习交流的开源非营利项目,仅作为程序员之间相互学习交流之用。

auto_comment_plus.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import argparse
77
import copy
88
import logging
9+
import os
910
import random
1011
import sys
1112
import time
@@ -20,6 +21,7 @@
2021

2122
# constants
2223
CONFIG_PATH = './config.yml'
24+
USER_CONFIG_PATH = './config.user.yml'
2325
ORDINARY_SLEEP_SEC = 10
2426
SUNBW_SLEEP_SEC = 5
2527
REVIEW_SLEEP_SEC = 10
@@ -641,14 +643,21 @@ def main(opts=None):
641643
logger.debug('Options passed to functions: %s', opts)
642644
logger.debug('Builtin constants:')
643645
logger.debug(' CONFIG_PATH: %s', CONFIG_PATH)
646+
logger.debug(' USER_CONFIG_PATH: %s', USER_CONFIG_PATH)
644647
logger.debug(' ORDINARY_SLEEP_SEC: %s', ORDINARY_SLEEP_SEC)
645648
logger.debug(' SUNBW_SLEEP_SEC: %s', SUNBW_SLEEP_SEC)
646649
logger.debug(' REVIEW_SLEEP_SEC: %s', REVIEW_SLEEP_SEC)
647650
logger.debug(' SERVICE_RATING_SLEEP_SEC: %s', SERVICE_RATING_SLEEP_SEC)
648651

649652
# parse configurations
650653
logger.debug('Reading the configuration file')
651-
with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
654+
if os.path.exists(USER_CONFIG_PATH):
655+
logger.debug('User configuration file exists')
656+
_cfg_path = USER_CONFIG_PATH
657+
else:
658+
logger.debug('User configuration file doesn\'t exist, fallback to the default one')
659+
_cfg_path = CONFIG_PATH
660+
with open(_cfg_path, 'r', encoding='utf-8') as f:
652661
cfg = yaml.safe_load(f)
653662
logger.debug('Closed the configuration file')
654663
logger.debug('Configurations in Python-dict format: %s', cfg)

0 commit comments

Comments
 (0)