feat: JSONのコメントを取り除くパーサーを実装

This commit is contained in:
shimoda.m@nds-tyo.co.jp 2022-07-12 15:55:59 +09:00
parent a8d377f7e1
commit 2f0bbf53b7
4 changed files with 38 additions and 5 deletions

View File

@ -41,3 +41,7 @@ LAUNCH_ON_LOCAL = 'local'
CHECK_TARGET_SCHEMAS = 'check_target_schemas'
# メール本文に出力する不足ファイル名一覧のインデント
MAIL_INDENT = '\n  '
# JSONファイル上のコメント業を表すシンボル
JSON_COMMENT_SYMBOL = '#'
# JSON内のコメントを置き換える正規表現
REPLACE_COMMENT_REGEX = rf'\s(?!\"){JSON_COMMENT_SYMBOL}[\s\S]*?.*'

View File

@ -32,3 +32,8 @@ class QueryExecutionException(MeDaCaException):
class SNSPublishException(MeDaCaException):
"""AmazonSNSへの通知に失敗した場合の例外"""
pass
class JSONParseException(MeDaCaException):
"""JSONのパースに失敗した場合の例外"""
pass

View File

@ -0,0 +1,17 @@
import json
import re
from constants import REPLACE_COMMENT_REGEX
class JSONParser:
__json_str: str = None
def __init__(self, json_str: str) -> None:
self.__json_str = json_str
def parse(self):
# コメントを除去して辞書に変換する
without_comment = re.sub(REPLACE_COMMENT_REGEX, '', self.__json_str)
return json.loads(without_comment)

View File

@ -2,8 +2,6 @@
Viewセキュリティオプション付与チェック用Lambda関数のエントリーポイント
"""
import json
import botocore
from aws.s3 import ConfigBucket
@ -20,8 +18,10 @@ from environments import (CONFIG_BUCKET_NAME, MBJ_NOTICE_TOPIC,
NDS_NOTICE_TOPIC, NOTICE_MAIL_BODY_TEMPLATE_PATH,
NOTICE_MAIL_TITLE_TEMPLATE_PATH)
from exceptions import (DatabaseConnectionException, FileNotFoundException,
MeDaCaException, ParameterNotFoundException,
QueryExecutionException, SNSPublishException)
JSONParseException, MeDaCaException,
ParameterNotFoundException, QueryExecutionException,
SNSPublishException)
from json_perser import JSONParser
from medaca_logger import MeDaCaLogger
logger = MeDaCaLogger.get_logger()
@ -87,6 +87,7 @@ def read_check_target_schemas() -> list:
Raises:
FileNotFoundException: ファイルが読み込めなかったエラー
JSONParseException: JSONを辞書オブジェクトに変換できなかったエラー
Exception: 想定外のエラー
Returns:
@ -95,12 +96,18 @@ def read_check_target_schemas() -> list:
try:
config_bucket = ConfigBucket()
check_target_schema_names = config_bucket.check_target_schema_names
return json.loads(check_target_schema_names)[CHECK_TARGET_SCHEMAS]
except botocore.exceptions.ClientError as e:
if e.response[RESPONSE_ERROR][RESPONSE_ERROR_CODE] == RESPONSE_CODE_NO_SUCH_KEY:
raise FileNotFoundException('E-02-01', f'チェック対象スキーマ名ファイルの読み込みに失敗しました エラー内容:{e}')
else:
raise Exception(e)
try:
json_parser = JSONParser(check_target_schema_names)
check_target_schemas = json_parser.parse()[CHECK_TARGET_SCHEMAS]
except Exception as e:
raise JSONParseException('E-02-01', f'チェック対象スキーマ名ファイルの読み込みに失敗しました エラー内容:{e}')
return check_target_schemas
def read_db_param_from_parameter_store() -> tuple: