Skip to content

Commit

Permalink
Reconstruct configuration files
Browse files Browse the repository at this point in the history
Now there is a user configuration file to avoid overwriting these files
when updating the repository.
  • Loading branch information
LittleYe233 committed Feb 22, 2022
1 parent b8b32f7 commit c870d72
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# user config
config.user.yml
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<Cookie>'
```
例如,若获取得到的ck为 `a=1; b=2; c=3` ,则配置文件中填入:

```yml
user:
cookie: ''
cookie: 'a=1; b=2; c=3'
```

最后运行 `auto_comment_plus.py` :
Expand All @@ -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`:**
Expand All @@ -71,6 +84,16 @@ optional arguments:

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

**`--log-level LOG_LEVEL`:**

设置输出日志的等级。默认为 `INFO` 。可选等级为 `DEBUG`、`INFO`、`WARNING`、`ERROR` ,输出内容量依次递减。

**注意:** 若你需要提交 issue 来报告一个 bug ,请将该选项设置为 `DEBUG` 。

**`-o LOG_FILE`:**

设置输出日志文件的路径。若无此选项,则不输出到文件。

## 声明

本项目为Python学习交流的开源非营利项目,仅作为程序员之间相互学习交流之用。
Expand Down
11 changes: 10 additions & 1 deletion auto_comment_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import argparse
import copy
import logging
import os
import random
import sys
import time
Expand All @@ -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
Expand Down Expand Up @@ -641,14 +643,21 @@ 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)
logger.debug(' SERVICE_RATING_SLEEP_SEC: %s', SERVICE_RATING_SLEEP_SEC)

# 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)
Expand Down

0 comments on commit c870d72

Please sign in to comment.