コンテンツのエクスポートとインポート
Glasswallは、サポートされるファイルタイプについてコンテンツ項目をエクスポートおよびインポートする機能を提供します。これにより、処理済みファイルの内部コンポーネントを、Glasswall Embedded Engineドメインの外部にある外部プロセスやアプリケーションで追加処理できるようになります。エクスポート後、これらのコンポーネントは外部で検証でき、その後Glasswall Engineがコンポーネントをインポートしてファイルを再構成します。
ファイルは Glasswall Embedded Engine によって2回処理する必要があります。1回目は、ファイルを構成するコンポーネントを含むパッケージを抽出するため(export)、2回目は、外部で分析および/または変更されたコンポーネントをファイルに再統合するためです(import)。詳細はContent Export & Importを参照してください。
ファイルは、export_file メソッドを使用してファイルパスまたはメモリ内から個別にエクスポートすることも、export_directory メソッドを使用してディレクトリ内のすべてのファイルをエクスポートすることもできます。
例
エクスポート
ファイルパスからファイルパスへエクスポート
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use the default policy to export a file, writing the export archive to a new path
editor.export_file(
input_file=r"C:\gwpw\input\TestFile_11.doc",
output_file=r"C:\gwpw\output\editor\export_f2f\TestFile_11.doc.zip",
)
ファイルパスからメモリへエクスポート
export_file は、エクスポートされたアーカイブファイルのバイト列を返します。以下の例では、変数 export_archive への代入と、Editor エクスポートアーカイブの先頭部分の内容を確認する方法を示しています。
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use the default policy to export a file
export_archive = editor.export_file(
input_file=r"C:\gwpw\input\TestFile_11.doc",
)
assert export_archive[:8] == b'PK\x03\x04\x14\x00\x0e\x00'
メモリからエクスポート
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Read file from disk to memory
with open(r"C:\gwpw\input\TestFile_11.doc", "rb") as f:
input_bytes = f.read()
# Use the default policy to export a file
export_archive = editor.export_file(
input_file=input_bytes,
)
assert export_archive[:8] == b'PK\x03\x04\x14\x00\x0e\x00'
ディレクトリ内のファイルをエクスポート
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use the default policy to export a directory of files, writing the export archives to a new directory.
editor.export_directory(
input_directory=r"C:\gwpw\input",
output_directory=r"C:\gwpw\output\editor\export_directory"
)
サポートされていないファイルタイプが含まれる可能性のあるディレクトリ内のファイルをエクスポート
Glasswall Python ラッパーのデフォルトの動作では、処理に失敗した場合に該当する例外を発生させます(参照: glasswall.libraries.editor.errors)。raise_unsupported=False を渡すと例外の発生を防ぐことができ、サポート対象と非サポート対象のファイル形式が混在するディレクトリを扱う際に、最初の失敗で終了するのではなく、できるだけ多くのファイルを処理したい場合に便利です。
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use the default policy to export a directory of files, writing the export archives to a new directory.
editor.export_directory(
input_directory=r"C:\gwpw\input_with_unsupported_file_types",
output_directory=r"C:\gwpw\output\editor\export_directory_unsupported",
raise_unsupported=False
)
カスタムコンテンツ管理policyを使用してディレクトリ内のファイルをエクスポート
glasswall.content_management.policies.Editor を使用:
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use a custom Editor policy to export all files in the input directory
# and write them to export_directory_custom directory. Write streams as
# ".xml" instead of the default interchange_type, ".sisl". Export embedded
# images as ".xml" instead of their default image file type.
editor.export_directory(
input_directory=r"C:\gwpw\input",
output_directory=r"C:\gwpw\output\editor\export_directory_custom",
content_management_policy=glasswall.content_management.policies.Editor(
default="sanitise",
config={
"sysConfig": {
"interchange_type": "xml",
"export_embedded_images": "true",
},
}
),
raise_unsupported=False
)
ファイル形式に基づいて条件付きでディレクトリ内のファイルをエクスポートする
以下の例では、複数のファイル形式を含むネストされたディレクトリから、docファイルとdocxファイルのみを処理する方法を示します。
import os
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
input_directory = r"C:\gwpw\input"
output_directory = r"C:\gwpw\output\editor\export_directory_file_format"
# Iterate relative file paths from input_directory
for relative_file in glasswall.utils.list_file_paths(input_directory, absolute=False):
# Construct absolute paths
input_file = os.path.join(input_directory, relative_file)
output_file = os.path.join(output_directory, relative_file + ".zip")
# Get the file type of the file
file_type = editor.determine_file_type(
input_file=input_file,
as_string=True,
raise_unsupported=False
)
# Export only doc and docx files
if file_type in ["doc", "docx"]:
editor.export_file(input_file, output_file)
インポート
エクスポートされたアーカイブは、import_file メソッドを使用してファイルパスまたはメモリ内から個別にインポートできます。また、ディレクトリ内のすべてのエクスポートアーカイブは、import_directory メソッドを使用してインポートできます。
ファイルパスからファイルパスへインポート
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use the default policy to import an export archive, writing the imported file to a new path
editor.import_file(
input_file=r"C:\gwpw\output\editor\export_f2f\TestFile_11.doc.zip",
output_file=r"C:\gwpw\output\editor\import_f2f\TestFile_11.doc",
)
ファイルパスからメモリへインポート
import_file は、インポートされたファイルのバイト列を返します。以下の例では、変数 file_bytes を代入し、Editorエクスポートアーカイブの先頭部分の内容を確認する方法を示します。
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use the default policy to import an export archive
file_bytes = editor.import_file(
input_file=r"C:\gwpw\output\editor\export_f2f\TestFile_11.doc.zip",
)
assert file_bytes[:8] == b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1'
メモリからインポート
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Read file from disk to memory
with open(r"C:\gwpw\output\editor\export_f2f\TestFile_11.doc.zip", "rb") as f:
export_archive_bytes = f.read()
# Use the default policy to import an export archive
file_bytes = editor.import_file(
input_file=export_archive_bytes,
)
assert file_bytes[:8] == b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1'
ディレクトリ内のファイルをインポート
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use the default policy to import a directory of export archives, writing the import archives to a new directory.
editor.import_directory(
input_directory=r"C:\gwpw\output\editor\export_directory",
output_directory=r"C:\gwpw\output\editor\import_directory"
)
サポートされていないファイルタイプが含まれている可能性があるディレクトリ内のファイルをインポート
Glasswall Python ラッパーのデフォルトの動作では、処理に失敗した場合に該当する例外を発生させます(参照: glasswall.libraries.editor.errors)。raise_unsupported=False を渡すと例外の発生を防ぐことができ、サポート対象と非サポート対象のファイル形式が混在するディレクトリを扱う際に、最初の失敗で終了するのではなく、できるだけ多くのファイルを処理したい場合に便利です。
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use the default policy to export a directory of export archives, writing the export archives to a new directory.
editor.import_directory(
input_directory=r"C:\gwpw\output\editor\export_directory_unsupported",
output_directory=r"C:\gwpw\output\editor\import_directory_unsupported",
raise_unsupported=False
)
カスタムコンテンツ管理policyを使用してディレクトリ内のファイルをインポートする
glasswall.content_management.policies.Editor を使用:
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
# Use a custom Editor policy to import all files in the export directory
# and write them to import_directory_custom directory. Read streams as
# ".xml" instead of the default interchange_type, ".sisl".
editor.import_directory(
input_directory=r"C:\gwpw\output\editor\export_directory_custom",
output_directory=r"C:\gwpw\output\editor\import_directory_custom",
content_management_policy=glasswall.content_management.policies.Editor(
default="sanitise",
config={
"sysConfig": {
"interchange_type": "xml",
},
}
),
raise_unsupported=False
)
ファイル形式に基づいて条件付きでディレクトリ内のファイルをインポートする
以下の例では、複数のファイル形式を含むネストされたディレクトリから、docファイルとdocxファイルのみを処理する方法を示します。
import os
import glasswall
# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")
input_directory = r"C:\gwpw\output\editor\export_directory_file_format"
output_directory = r"C:\gwpw\output\editor\import_directory_file_format"
# Iterate relative file paths from input_directory
for relative_file in glasswall.utils.list_file_paths(input_directory, absolute=False):
# Construct absolute paths
input_file = os.path.join(input_directory, relative_file)
output_file = os.path.join(output_directory, os.path.splitext(relative_file)[0])
# Get the file type of the file
file_type = editor.determine_file_type(
input_file=input_file,
as_string=True,
raise_unsupported=False
)
# Import only doc.zip and docx.zip files
if file_type == "zip" and input_file.endswith(("doc.zip", "docx.zip",)):
editor.import_file(input_file, output_file)
エクスポート/インポートと分析
これらの高レベル関数を使用すると、単一のセッション内で Export または Import を実行し、分析レポートを生成できます。詳細については、以下のドキュメント リンクを参照してください。