Last active
July 23, 2018 12:53
-
-
Save pypeach/73286baa08b85affe07491824e870216 to your computer and use it in GitHub Desktop.
アプリケーション設定制御
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding:utf-8 | |
import logging | |
import logging.config | |
import os | |
import sys | |
import yaml | |
from app.util import db_access, string_helper | |
""" | |
アプリケーションの設定を制御する | |
""" | |
__author__ = "t.ebinuma" | |
__version__ = "1.0" | |
__date__ = "22 January 2018" | |
def init(): | |
""" | |
アプリケーション起動時に設定を初期化する | |
""" | |
# loggingファイルを読み込む | |
with open(_get_log_include_path(), 'r', encoding='UTF-8') as f: | |
logging_resource_file = f.read().replace("{$log_file}", get_log_file_path()) | |
# ログ出力先をloggingに設定する | |
logging.config.dictConfig(yaml.load(logging_resource_file)) | |
# dbディレクトリ存在チェック | |
db_dir = os.path.dirname(get_value('db_path')) | |
# dbディレクトリがなければ作成する | |
if os.path.exists(db_dir) is False: | |
os.makedirs(db_dir) | |
# logディレクトリ存在チェック | |
log_dir = os.path.dirname(get_value('log_path')) | |
# logディレクトリがなければ作成する | |
if os.path.exists(log_dir) is False: | |
os.makedirs(log_dir) | |
# dbスキーマーの初期化有無 | |
if get_value("db_schema_initialize") is True: | |
# dbスキーマーの初期化する場合、schema.sqlを実行する | |
with open(os.path.join(get_resource_path(), get_value('db_schema_file')), 'r', encoding='UTF-8') as f: | |
sql_list = f.read().split(';') | |
for sql_item in sql_list: | |
sql = string_helper.skip_string_stream(sql_item.strip(), "#") | |
if len(sql) > 0: | |
db_access.ddl_execute(sql) | |
def get_value(key): | |
""" | |
設定ファイルからキーに紐づく値を取得する | |
""" | |
with open(os.path.join(_get_app_include_path()), 'r', encoding='UTF-8') as f: | |
config = f.read() | |
# ${app_home}は環境変数PYTHONPATHを使用する | |
config = config.replace("{$python_path}", _get_python_path()) | |
return yaml.load(config)[key] | |
def get_resource_path(): | |
""" | |
リソースパスを取得する | |
""" | |
python_path = _get_python_path() | |
return os.path.join(python_path, "resources") | |
def _get_log_include_path(): | |
""" | |
インクルードを行うloggingパスを取得する | |
""" | |
with open(os.path.join(get_resource_path(), "application.yml"), 'r', encoding='UTF-8') as f: | |
app_base = f.read() | |
app_include = yaml.load(app_base)["app_include"] | |
app_file_name = "logging_" + app_include + ".yml" | |
return os.path.join(_get_python_path(), "resources", app_file_name) | |
def _get_app_include_path(): | |
""" | |
インクルードを行うアプリケーションファイルパスを取得する | |
""" | |
with open(os.path.join(get_resource_path(), "application.yml"), 'r', encoding='UTF-8') as f: | |
app_base = f.read() | |
app_include = yaml.load(app_base)["app_include"] | |
app_file_name = "application_" + app_include + ".yml" | |
return os.path.join(_get_python_path(), "resources", app_file_name) | |
def _get_python_path(): | |
""" | |
環境変数PYTHONPATHのパスを取得する | |
環境変数PYTHON_APP_HOMEに設定した場合はAPP_HOMEのパスを優先します | |
(PyCharmでUnitTestを行うケースはPYTHONPATHにIDEのパスが含まれるため本対応を行いました) | |
""" | |
# OS環境変数からPYTHONPATHを取得する | |
python_path = "" | |
for key, path in os.environ.items(): | |
# 環境変数にPYTHON_APP_HOMEが指定された場合は | |
# PYTHONPATHにPYTHON_APP_HOMEをセットする | |
if key == "PYTHON_APP_HOME": | |
python_path = path | |
break | |
elif key == "PYTHONPATH": | |
python_path = path | |
# PYTHONPATHがない場合、アプリケーションを終了する | |
if len(python_path) == 0: | |
print("PYTHONPATHが設定されていません") | |
sys.exit() | |
return python_path | |
def get_log_file_path(): | |
""" | |
ログファイルのフルパスを取得する | |
""" | |
return os.path.join(get_value("log_path"), get_value("log_file")) | |
def get_data_file_path(file_name): | |
""" | |
データファイルのフルパスを取得する | |
""" | |
return os.path.join(get_value("data_path"), file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
loggingのファイル切り替えを追加