Skip to content

Commit

Permalink
自动识别配置文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Nov 27, 2023
1 parent 900ff4b commit 9cf1670
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
22 changes: 19 additions & 3 deletions WordReview/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
__all__ = ['config']

import configparser
from pathlib import Path
import os

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

_project_dir = Path(__file__).parent
_config_file_path = _project_dir / 'config.conf'


def check_config_file_exists():
if not os.path.exists(_config_file_path):
from shutil import copyfile
print("【步骤遗漏!】还没有复制配置文件哦...")
copyfile(_project_dir / "config_sample.conf", _config_file_path)
print(
f'但我已经帮你复制好了:\n{_project_dir / "config_sample.conf"} -> {_config_file_path}'
)
print("继续往下走吧~")


check_config_file_exists()

config = configparser.ConfigParser(default_dict)
# print(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.conf'))
config.read(os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'config.conf'), encoding='utf-8')
config.read(_config_file_path, encoding='utf-8')
16 changes: 8 additions & 8 deletions WordReview/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def lock_system_port(_port):
serv = listener.accept() # just bind the port.

t = Thread(target=lock_system_port, args=(62771, ))
t.setDaemon(True)
t.daemon = True
t.start()
del t
return q.get()
Expand All @@ -48,8 +48,8 @@ def enable_browser_with_delay(argv, _t=None):
if subcommand == 'runserver' and '--noreload' not in argv:
try:
parser_port = argv[2]
port_with_colon = parser_port[
parser_port.index(":"):] if ':' in parser_port else ":" + parser_port
port_with_colon = parser_port[parser_port.index(
":"):] if ':' in parser_port else ":" + parser_port
except (IndexError, ValueError):
port_with_colon = ":8000"
finally:
Expand All @@ -71,13 +71,13 @@ def main():
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
"forget to activate a virtual environment?") from exc

if Is_child_processing() and config.getboolean('custom', '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))
t = threading.Thread(target=enable_browser_with_delay,
args=(sys.argv, 1))
t.start()
del t

Expand Down
2 changes: 1 addition & 1 deletion WordReview/requirements.txt → requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ django-sass-processor==0.8
libsass==0.19.4
mysqlclient==1.4.6
pymysql==0.9.3
pypugjs==5.8.1
pypugjs==5.9.12
django-compressor
xlrd
pandas
Expand Down

0 comments on commit 9cf1670

Please sign in to comment.