replace_recursive プログラム仕様
- 概要:
指定されたディレクトリツリー内のファイルを再帰的に処理し、特定の文字列置換とファイルコピーを行うスクリプト。
- 詳細説明:
このスクリプトは、source_dir で指定されたディレクトリから target_dir へファイルをコピーします。 convert_file_extensions に含まれる拡張子を持つファイルに対しては、 replace_list に基づく正規表現置換を実行します。 exclude_list に含まれるファイルは置換処理をスキップし、そのままコピーされます。
- 関連リンク:
replace_recursive_usage
- web.replace_recursive.copy_file(source_file, target_file)
- 概要:
ファイルをソースパスからターゲットパスへコピーします。
- 詳細説明:
ターゲットディレクトリが存在しない場合は作成し、 ファイルのメタデータも保持してコピーします。
- 引数:
- param source_file:
コピー元のファイルパス。
- type source_file:
str
- param target_file:
コピー先のファイルパス。
- type target_file:
str
- 戻り値:
- returns:
なし
- rtype:
None
- web.replace_recursive.detect_encoding(file_path)
- 概要:
指定されたファイルのエンコーディングを検出します。
- 詳細説明:
chardetライブラリを使用して、ファイルのバイト列からエンコーディングを推測します。
- 引数:
- param file_path:
エンコーディングを検出するファイルのパス。
- type file_path:
str
- 戻り値:
- returns:
検出されたエンコーディング名(例: 'utf-8', 'EUC-JP')。
- rtype:
str
- web.replace_recursive.extract_filename(file_path)
- 概要:
ファイルパスからファイル名を抽出します。
- 引数:
- param file_path:
ファイルのパス。
- type file_path:
str
- 戻り値:
- returns:
抽出されたファイル名。
- rtype:
str
- web.replace_recursive.main()
- 概要:
スクリプトの主要な実行ロジックをカプセル化します。
- 詳細説明:
定義済みの source_dir, target_dir, replace_list, convert_file_extensions を使用して process_directory 関数を呼び出します。
- 戻り値:
- returns:
なし
- rtype:
None
- web.replace_recursive.process_directory(source_dir, target_dir, replace_list, file_extensions)
- 概要:
ソースディレクトリツリーを走査し、ファイルをターゲットディレクトリにコピーしながら、 指定された拡張子のファイルに対して文字列置換を実行します。
- 詳細説明:
os.walk を使用してディレクトリを再帰的に走査します。 file_extensions に含まれるファイルは replace_strings_in_file で処理され、 exclude_list に含まれるファイルは置換をスキップしてコピーされます。 その他のファイルは単純にコピーされます。
- 引数:
- param source_dir:
処理対象のソースディレクトリパス。
- type source_dir:
str
- param target_dir:
処理結果を保存するターゲットディレクトリパス。
- type target_dir:
str
- param replace_list:
(正規表現パターン, 置換文字列)のリスト。
- type replace_list:
list[list[str]]
- param file_extensions:
文字列置換を行うファイル拡張子のリスト(例: ['.html', '.css'])。
- type file_extensions:
list[str]
- 戻り値:
- returns:
なし
- rtype:
None
- web.replace_recursive.read_file_with_detected_encoding(file_path)
- 概要:
ファイルのエンコーディングを自動検出してファイルを読み込みます。
- 詳細説明:
ファイルが存在しない場合や読み込みに失敗した場合はFalseを返します。
- 引数:
- param file_path:
読み込むファイルのパス。
- type file_path:
str
- 戻り値:
- returns:
ファイルの内容、または読み込み失敗時にはFalse。
- rtype:
str or bool
- web.replace_recursive.regex_replace(text, replace_list)
- 概要:
テキスト内の指定されたパターンを正規表現に基づいて置換します。
- 詳細説明:
replace_listに含まれる各(パターン, 置換文字列)のペアを使用して、 入力テキストに対して正規表現置換を順番に適用します。
- 引数:
- param text:
置換対象の文字列。
- type text:
str
- param replace_list:
(正規表現パターン, 置換文字列)のリスト。
- type replace_list:
list[list[str]]
- 戻り値:
- returns:
置換後の文字列。
- rtype:
str
- web.replace_recursive.replace_strings_in_file(source_file, target_file, replace_list)
- 概要:
ソースファイルを読み込み、指定された置換リストに基づいて文字列を置換し、結果をターゲットファイルに保存します。
- 詳細説明:
detect_encoding を使用してエンコーディングを検出し、ファイルを読み込みます。 置換は正規表現で行われます。ターゲットファイルのディレクトリが存在しない場合は作成されます。
- 引数:
- param source_file:
読み込み元のファイルパス。
- type source_file:
str
- param target_file:
置換結果を書き込むファイルパス。
- type target_file:
str
- param replace_list:
(正規表現パターン, 置換文字列)のリスト。
- type replace_list:
list[list[str]]
- 戻り値:
- returns:
ファイルの処理が成功した場合はTrue、読み込みに失敗した場合はFalse。
- rtype:
bool