ポスト処理実行スクリプト¶
パラメータファイルからのコマンド実行¶
計算後に、 毎回、一定の決まったポスト処理 (グラフ生成など)をする場合、 パラメータファイルにポスト処理を記載し自動実行できると便利 .
情報整理 の観点で◎ ( readme 不要 )
スクリプト化すれば入力ファイル選択により 自動実行可能 ( e.g. go__phits.py )
使い方¶
コマンドの記載¶
コメントラインとして、<postProcess>句を記載して、コマンドを記載する
# <postProcess> cp out.dat result.dat
記号 "#" はコメントマーク.変更可能.
スクリプトの実行¶
command__postProcess.py スクリプトを呼ぶ.:
$ command__postProcess.py parameter.inp
or
$ command__postProcess.py parameter.inp -c --comment_mark $
etc.
引数¶
Argument |
Attribute |
Description |
---|---|---|
inpFile |
第一引数 |
入力ファイル名 |
--comment_mark |
Optional |
コメント記号 ( Default: '#' ) |
--command/-c |
Optional |
設定すればコマンド表示のみ、実行なし |
コマンドの登録¶
他のコマンドと同様、以下の3つを設定
スクリプト先頭におまじない(shebang)を記載 ( e.g. "#!/usr/bin/env python3.10" )
パスの通ったところにおく ( e.g. /usr/local/bin/ )
実行権限付与 ( e.g. chmod +x /usr/local/bin/ )
コマンドライン実行可能に
$ command__postProcess.py replace_sample.conf --comment_mark $
コード¶
ポスト処理実行スクリプト ( command__postProcess.py )¶
#!/usr/bin/env python3.10
# -*- coding: utf-8 -*-
import os, sys, re, subprocess
# ========================================================= #
# === command__postProcess.py === #
# ========================================================= #
def command__postProcess( inpFile=None, lines=None, comment_mark="#", execute=True, \
verbose__command=True, silent=True, \
postProcess_mark="<postProcess>", escapeType ="UseEscapeSequence" ):
replace__mark = "#"
# ------------------------------------------------- #
# --- [1] Arguments --- #
# ------------------------------------------------- #
if ( lines is None ):
if ( inpFile is None ):
sys.exit( "[command__postProcess.py] lines, inpFile == ???? [ERROR] " )
else:
with open( inpFile, "r" ) as f:
lines = f.readlines()
if ( type( lines ) is str ):
lines = [ lines ]
if ( comment_mark in [ "$", "*"] ): # exception for "$" and "*"
comment_mark_ = comment_mark*2
else:
comment_mark_ = comment_mark
# ------------------------------------------------- #
# --- [2] expression of definition --- #
# ------------------------------------------------- #
vdict = {}
if ( comment_mark in [ "$", "*" ] ): # --:: Need - Escape-Sequence ... ::-- #
if ( escapeType == "UseEscapeSequence" ):
cmt = "\\" + comment_mark
expr_def = "{0}\s*{1}\s*(.*)".format( cmt, postProcess_mark )
elif ( escapeType == "ReplaceCommentMark" ):
original = comment_mark
expr_def = "{0}\s*{1}\s*(.*)".format( replace_mark, postProcess_mark )
for ik,line in enumerate( lines ):
lines[ik] = ( lines[ik] ).replace( original, replace_mark )
else:
expr_def = "{0}\s*{1}\s*(.*)".format( comment_mark, postProcess_mark )
# ------------------------------------------------- #
# --- [3] parse variables --- #
# ------------------------------------------------- #
stack = []
for ik,line in enumerate(lines): # 1-line, 1-argument.
# ------------------------------------------------- #
# --- search variable notation --- #
# ------------------------------------------------- #
ret = re.match( expr_def, line )
if ( ret ): # Found.
# ------------------------------------------------- #
# --- [3-1] get command --- #
# ------------------------------------------------- #
if ( comment_mark_ in ret.group(1) ):
command = ( ( ( ret.group(1) ).split(comment_mark_) )[0] ).strip()
else:
command = ( ret.group(1) ).strip()
# ------------------------------------------------- #
# --- [3-2] stack commands --- #
# ------------------------------------------------- #
stack += [command]
# ------------------------------------------------- #
# --- [4] execute & return --- #
# ------------------------------------------------- #
# -- [4-1] print & execute command -- #
for command in stack:
if ( verbose__command ):
print( command )
if ( execute ):
subprocess.run( command, shell=True )
# -- [4-2] return -- #
if ( not( silent ) ):
print( "[command__postProcess.py] command list is returned." + "\n" )
return( stack )
# ========================================================= #
# === Execution of Pragram === #
# ========================================================= #
if ( __name__=="__main__" ):
# ------------------------------------------------- #
# --- [1] arguments --- #
# ------------------------------------------------- #
import argparse
parser = argparse.ArgumentParser()
parser.add_argument( "inpFile" , help="input file name." , default=None )
parser.add_argument( "--comment_mark" , help="comment mark like # or $", default="#" )
parser.add_argument( "-c", "--command", help="display command only", \
default=False, action="store_true" )
args = parser.parse_args()
if ( args.inpFile is None ):
print( "[command__postProcess.py] inpFile == ???" )
print( " ( e.g. ) $ command__postProcess.py inpFile -c --comment_mark $" )
sys.exit()
# ------------------------------------------------- #
# --- [3] call replace variableDefinition --- #
# ------------------------------------------------- #
execute = not( args.command )
print( "\n" + "[commands]" )
ret = command__postProcess( inpFile=args.inpFile, comment_mark=args.comment_mark, \
execute=execute )
print( "\n" + "[return]" )
print( "\n".join( ret ) + "\n" )
サンプル入力ファイル ( replace_sample.conf )¶
# this is a sample of variable definition
# -- below is definitions. -- #
# <define> @coef1 = 1.0 # this is coef1
# <define> @coef2 = 1000
# <define> @coef3 = @coef1 + @coef2
$ <define> @coef1 = 3.0 $ this is coef1
$ <define> @coef2 = 22
$ <define> @coef3 = @coef1 + @coef2
$ <define> @type = type-A
# -- to disable the definition, use ## or ### etc. -- #
# -- program catch following expressions "#\s*@(variable's name)\s# -- #
set01 0.1 0.2 @coef3
set02 0.2 @coef1 @coef2
Set03 0.1 0.2 0.4
Set04 0.1 0.2 @coef2
type @type
val_frm_tbl @val_frm_tbl
$ <include> filepath = test/include.conf
$ <postProcess> echo "This is an echo test."
$ <postProcess> echo "This is an echo test."
$ <postProcess> cp replace_sample.conf replace_copy.conf
出力例¶
$ cd cnf
$ command__postProcess.py replace_sample.conf --comment_mark $