Skip to content

Commit

Permalink
配置文件更换为.conf文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Apr 18, 2020
1 parent ca131c6 commit d8cae10
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 74 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ logs/
/media/

# config
/config.py
/config.conf

# PyInstaller
/build/
/dist/
/*.spec
*.spec

# local
data/
*.sqlite3
/backup/
/pypi/
WordReview.wiki/
WordReview.wiki/
*_ig.*
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<a href="https://benature.github.io/"><img src="./static/media/muyi.png" height="200" align="right"></a>
<a href="https://benature.github.io/WordReview/"><img src="./static/media/muyi.png" height="200" align="right"></a>

# Word Review 单词复习

![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/Benature/WordReview)
![GitHub stars](https://img.shields.io/github/stars/Benature/WordReview?style=flat)
![GitHub stars](https://img.shields.io/github/forks/Benature/WordReview?style=flat)
![GitHub issues](https://img.shields.io/github/issues/Benature/WordReview)
![GitHub closed issues](https://img.shields.io/github/issues-closed/Benature/WordReview)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/Benature/WordReview)
![GitHub](https://img.shields.io/github/license/Benature/WordReview)

Django + MySQL + Pug + JS

- Python 3.7+
- Django 3
- Mysql 8
- Mysql 8 / sqlite 3

简单录了一个 DEMO 视频,上传到了[B站](https://www.bilibili.com/video/av90579311/),欢迎康康。
二月的时候简单录了一个 DEMO 视频,上传到了[微信公众号](https://mp.weixin.qq.com/s/3VjiflLdzKv0sjxm1noxDw)[B站](https://www.bilibili.com/video/av90579311/)(BV 号:**BV1q7411w7r9**,欢迎康康。

还有一个[在线试玩](https://benature.github.io/WordReview/),可以先感受一下。
>在线体验的版本对应`master`分支,现在默认显示的是`ben`分支(开发分支)
如果觉得还不错的话,不如给个 stars🌟呗( ̄▽ ̄)~*

Expand Down Expand Up @@ -155,7 +156,7 @@ tain
### List 的记忆率

- 蓝条:历史记忆率,对 List 内单词的总记忆率取平均
- 绿条:上轮记忆率,按 List 内单词的 **最新两次** 记忆情况就散平均记忆率
- 绿条:上轮记忆率,按 List 内单词的 **最新两次** 记忆情况计算平均记忆率

### 单词的`flag`

Expand All @@ -170,6 +171,7 @@ tain
### 更新日志

- 2020.04
- update: 配置文件更换为`.conf`文件(以支持默认参数) @04-18
- feature: 命令行启动后自动打开浏览器 ([Issue#4](https://github.com/Benature/WordReview/issues/4)) @04-16
- feature: 新增[在线预览](https://benature.github.io/WordReview/) @04-16
- feature: 对<http://dict.cn/mini.php>的后端爬虫 API ([Issue#2](https://github.com/Benature/WordReview/issues/2)) @04-15
Expand Down
4 changes: 2 additions & 2 deletions WordReview/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config
if config.database_type == 'mysql':
from config import config
if config.get('custom', 'db_type') == 'mysql':
import pymysql
pymysql.install_as_MySQLdb()
# import pymysql
Expand Down
40 changes: 23 additions & 17 deletions WordReview/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import os
import sys
import config
from config import config

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -89,22 +89,28 @@
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = config.DATABASES
# {
# # 'default': {
# # 'ENGINE': 'django.db.backends.sqlite3',
# # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# # }
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': 'tg_word_db', # database name
# 'USER': 'tg_word_user', # user name
# 'PASSWORD': 'tg_word2020', # user pwd
# 'HOST': 'localhost', # '116.62.12.178',
# 'PORT': '',
# }
# }

db_type = config.get('custom', 'db_type')
if db_type == 'mysql':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': config.get('mysql', 'mysql_name', fallback='word_db'),
'USER': config.get('mysql', 'mysql_user', fallback='word_user'),
'PASSWORD': config.get('mysql', 'mysql_password', fallback='word2020'),
'HOST': config.get('mysql', 'mysql_host', fallback='localhost'),
'PORT': config.get('mysql', 'mysql_port', fallback=''),
'OPTIONS': {'charset': 'utf8mb4'},
}
}
elif db_type == 'sqlite':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
else:
raise ValueError(f'请选择正确的数据库:`mysql`、`sqlite`,而非{db_type}')

# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
Expand Down
11 changes: 11 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
__all__ = ['config']

import configparser

default_dict = {
'db_type': 'sqlite',
'auto_open_browser': 'yes',
}

config = configparser.ConfigParser(default_dict)
config.read('config.conf')
20 changes: 20 additions & 0 deletions config_sample.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ======================================================
# 用户自定义配置区
# ======================================================
[custom]
# 是否自动打开浏览器
auto_open_browser = yes

# 使用数据库类型:`sqlite`(默认)、`mysql`
db_type = sqlite

# ======================================================
# 数据库使用配置
# (除非你知道你在干嘛,否则请勿修改下面代码)
# ======================================================
[mysql]
mysql_name = 'word_db'
mysql_user = 'word_user'
mysql_password = 'word2020'
mysql_host = 'localhost'
mysql_port = ''
38 changes: 0 additions & 38 deletions config_sample.py

This file was deleted.

14 changes: 7 additions & 7 deletions doc/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ pip: command not found

`config.py`文件下,找到下面这个变量,定义为`sqlite`。(默认就是这个,一般不用动了)

```python
```conf
# 使用数据库类型:`mysql`、`sqlite`
database_type = 'sqlite'
db_type = sqlite
```

>不过我是用 MySQL 的,如果想直接操作数据库的话,主要靠你自己百度的,你来问我我也是去百度的。
>当然,只要你操作正常,一般没必要去直接操作数据库的。
>再其实,就算要直接操作数据库,也可以借助 GUI 工具,工具有哪些可以[自己找找看](https://www.bing.com/search?q=sqlite+GUI)
<!-- >不过我是用 MySQL 的,如果想直接操作数据库的话,主要靠你自己百度的,你来问我我也是去百度的。
>当然,只要你操作正常,一般没必要去直接操作数据库的。 -->
>如果需要直接操作数据库,可以借助 GUI 工具,工具有哪些可以[在这里找找看](https://www.bing.com/search?q=sqlite+GUI)
## 3.2. 选择二:MySQL

Expand Down Expand Up @@ -182,9 +182,9 @@ flush privileges; -- 刷新系统权限表
`config.py`文件下,找到下面这个变量,定义为`mysql`。(默认就是这个,一般不用动了)

```python
```conf
# 使用数据库类型:`mysql`、`sqlite`
database_type = 'mysql'
db_type = mysql
```

</details>
Expand Down
4 changes: 2 additions & 2 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""Django's command-line utility for administrative tasks."""
import os
import sys
from config import auto_open_browser
from config import config

BASE_DIR = os.path.abspath(__file__)
sys.path.append(os.path.join(BASE_DIR, "pypi"))
Expand Down Expand Up @@ -74,7 +74,7 @@ def main():
"forget to activate a virtual environment?"
) from exc

if Is_child_processing() and auto_open_browser:
if Is_child_processing() and config.getboolean('custom', 'auto_open_browser'):
import threading
t = threading.Thread(
target=enable_browser_with_delay, args=(sys.argv, 1))
Expand Down

0 comments on commit d8cae10

Please sign in to comment.