style: ドキュメントコメント追加
This commit is contained in:
parent
8efd302566
commit
989c894f60
@ -14,6 +14,8 @@ class DatFileLine:
|
||||
|
||||
|
||||
class DatFile:
|
||||
"""アルトマークデータファイル"""
|
||||
|
||||
lines: list[DatFileLine]
|
||||
success_count: int = 0
|
||||
error_count: int = 0
|
||||
@ -45,6 +47,14 @@ class DatFile:
|
||||
|
||||
@classmethod
|
||||
def from_path(cls, local_file_path: str):
|
||||
"""アルトマークデータファイルを読み込み、新しいインスタンスを作成する
|
||||
|
||||
Args:
|
||||
local_file_path (str): ローカルのファイルパス
|
||||
|
||||
Returns:
|
||||
DatFile: このクラスのインスタンス
|
||||
"""
|
||||
# cp932(Shift-JIS Windows拡張)でファイルを読み込む
|
||||
file = open(local_file_path, encoding='cp932')
|
||||
instance = cls(file)
|
||||
|
||||
@ -7,7 +7,6 @@ from src.batch.ultmarc.datfile import DatFile
|
||||
from src.batch.ultmarc.utmp_tables.ultmarc_table_mapper_factory import \
|
||||
UltmarcTableMapperFactory
|
||||
from src.db.database import Database
|
||||
from src.error.exceptions import DBException
|
||||
from src.logging.get_logger import get_logger
|
||||
|
||||
logger = get_logger('アルトマークデータ保管')
|
||||
@ -28,18 +27,18 @@ def import_process():
|
||||
dat_file_list = ultmarc_bucket.list_edi_file()
|
||||
# ファイルがない場合は処理せず、正常終了とする
|
||||
if len(dat_file_list) == 0:
|
||||
logger.info('ファイルがないため、処理をスキップします')
|
||||
logger.info('ファイルがないため、アルトマーク取込処理をスキップします')
|
||||
return
|
||||
|
||||
# ファイルが複数ある場合はエラーとする
|
||||
if len(dat_file_list) > 1:
|
||||
logger.error('複数の取込ファイルがあるため、異常終了')
|
||||
logger.error(f'複数の取込ファイルがあるため、異常終了 ファイル一覧:{dat_file_list}')
|
||||
return
|
||||
# ファイルの件数は必ず1件になる
|
||||
dat_file_info = dat_file_list[0]
|
||||
# 0Byteの場合、
|
||||
if dat_file_info['size'] == 0:
|
||||
logger.info('0Byteファイルのため、処理をスキップします')
|
||||
logger.info(f'0Byteファイルのため、処理をスキップします。ファイル名={dat_file_info["filename"]}')
|
||||
return
|
||||
dat_file_name = dat_file_info['filename']
|
||||
logger.info(f"Get File Name :{dat_file_name}")
|
||||
@ -53,7 +52,7 @@ def import_process():
|
||||
mapper_factory = UltmarcTableMapperFactory()
|
||||
dat_file = DatFile.from_path(local_file_path)
|
||||
# datファイルを1行ずつ処理し、各テーブルへ登録
|
||||
for log_count, line in enumerate(dat_file):
|
||||
for line in dat_file:
|
||||
try:
|
||||
# 書き込み先のテーブルを特定
|
||||
mapper_class = mapper_factory.create(
|
||||
@ -65,11 +64,7 @@ def import_process():
|
||||
mapper_class.make_query()
|
||||
mapper_class.execute_queries()
|
||||
dat_file.count_up_success()
|
||||
# 5000件ごとにログ記録
|
||||
# これいる??
|
||||
if log_count % 5000 == 0:
|
||||
logger.info(f'Count: {log_count}')
|
||||
except DBException as e:
|
||||
except Exception as e:
|
||||
logger.warning(e)
|
||||
record = line.records
|
||||
log_message = ','.join([f'"{r}"' for r in record])
|
||||
@ -77,6 +72,7 @@ def import_process():
|
||||
dat_file.count_up_error()
|
||||
# すべての行を登録終えたらコミットする
|
||||
db.commit()
|
||||
# 処理結果をログに出力する
|
||||
logger.info('Transaction COMMIT')
|
||||
logger.info(f'ultmarc import process RESULT')
|
||||
logger.info(f'SUCCESS_COUNT={dat_file.success_count}')
|
||||
|
||||
@ -10,6 +10,8 @@ batch_config = BatchConfig.get_instance()
|
||||
|
||||
|
||||
class UltmarcTableMapper(metaclass=ABCMeta):
|
||||
"""アルトマークテーブルへの登録処理の抽象クラス"""
|
||||
|
||||
record: UltmarcTable
|
||||
db: Database
|
||||
queries: list[str]
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
class UltmarcTable:
|
||||
|
||||
"""アルトマーク関連テーブルの抽象クラス"""
|
||||
record: list
|
||||
|
||||
def __init__(self, record: list):
|
||||
self.record = record
|
||||
|
||||
|
||||
def to_sql_parameter(self):
|
||||
return vars(self)
|
||||
|
||||
@ -86,12 +86,26 @@ COM_TABLE_LIST = {
|
||||
class UltmarcTableMapperFactory:
|
||||
|
||||
def create(self, layout_class: str, record_id: str, records: list[str], db: Database) -> UltmarcTableMapper:
|
||||
# レイアウト種別とレコードIDから、マッピング先のテーブルを特定
|
||||
"""レイアウト区分とレコードIDから、マッピング先のテーブルマッパーを特定する
|
||||
|
||||
Args:
|
||||
layout_class (str): レイアウト区分
|
||||
record_id (str): レコードID
|
||||
records (list[str]): アルトマークデータの1行
|
||||
db (Database): データベース操作クラス
|
||||
|
||||
Raises:
|
||||
Exception: レイアウトを特定できない場合
|
||||
|
||||
Returns:
|
||||
UltmarcTableMapper: マッパークラス
|
||||
"""
|
||||
# レイアウト区分から、マッピング先のテーブルを特定
|
||||
table_by_layout_class = COM_TABLE_LIST.get(layout_class)
|
||||
|
||||
# レイアウト種別が特定できない場合はエラーとする
|
||||
# レイアウト区分が特定できない場合はエラーとする
|
||||
if table_by_layout_class is None:
|
||||
raise Exception('特定できませんでした')
|
||||
raise Exception(f'マッピング先のテーブルを特定できませんでした。レイアウト区分={layout_class}, レコードID={record_id}')
|
||||
|
||||
mapper_class: UltmarcTableMapper = None
|
||||
if type(table_by_layout_class) is dict:
|
||||
@ -99,4 +113,7 @@ class UltmarcTableMapperFactory:
|
||||
elif issubclass(table_by_layout_class, UltmarcTableMapper):
|
||||
mapper_class = table_by_layout_class
|
||||
|
||||
if mapper_class is None:
|
||||
raise Exception(f'マッピング先のテーブルを特定できませんでした。レイアウト区分={layout_class}, レコードID={record_id}')
|
||||
|
||||
return mapper_class(records, db)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user