Lumaktaw sa pangunahing nilalaman

Pag-export at pag-import ng content

Nagbibigay ang Glasswall ng kakayahang mag-export at mag-import ng mga content item para sa mga sinusuportahang uri ng file. Nagbibigay-daan ito upang maging available ang mga panloob na bahagi ng mga naprosesong file sa mga panlabas na proseso at application para sa karagdagang pagproseso sa labas ng saklaw ng Glasswall Embedded Engine. Kapag na-export na, maaaring ma-validate ang mga bahaging ito sa labas bago i-import ng Glasswall Engine ang mga bahagi at buuing muli ang mga file.

Ang mga file ay dapat iproseso ng Glasswall Embedded Engine nang dalawang beses; una upang mag-extract ng package na naglalaman ng mga component na bumubuo sa isang file (export), at pangalawang pasada upang muling isama sa file ang mga component na sinuri sa labas at/o binago (import). Tingnan ang Content Export & Import.

Maaaring i-export ang mga file nang paisa-isa mula sa isang file path o sa memory gamit ang paraang export_file, o maaaring i-export ang lahat ng file mula sa isang directory gamit ang paraang export_directory.

Mga Halimbawa

Export

Mag-export mula sa file path papunta sa file path

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",
)

Mag-export mula sa file path papunta sa memory

Ang export_file ay nagbabalik ng bytes ng na-export na archive file. Ipinapakita ng halimbawa sa ibaba ang pag-assign sa variable na export_archive at ang pagsuri sa nilalaman ng simula ng isang Editor export archive.

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'

Mag-export mula sa memory

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'

Mag-export ng mga file sa isang directory

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"
)

Mag-export ng mga file sa isang directory na maaaring naglalaman ng mga hindi suportadong uri ng file

Ang default na pag-uugali ng Glasswall Python wrapper ay mag-raise ng kaugnay na exception (tingnan ang: glasswall.libraries.editor.errors) kung mabigo ang pagproseso. Ang pagpasa ng raise_unsupported=False ay pipigil sa pag-raise ng exception at maaaring maging kapaki-pakinabang kapag nagtatrabaho sa isang directory na naglalaman ng halo ng parehong supported at unsupported na mga file type kapag nais na maproseso ang pinakamarami sa mga file hangga't maaari sa halip na tumigil sa unang pagkabigo.

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
)

Mag-export ng mga file sa isang directory gamit ang custom na policy sa pamamahala ng nilalaman

Gamit ang 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
)

I-export ang mga file sa isang directory nang may kondisyon batay sa format ng file

Ipinapakita ng halimbawa sa ibaba ang pagproseso lamang ng mga doc at docx file mula sa isang nested directory na naglalaman ng maraming format ng file.

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

Ang mga export archive ay maaaring i-import nang paisa-isa mula sa isang file path o sa memory gamit ang import_file method, o maaaring i-import ang lahat ng export archive mula sa isang directory gamit ang import_directory method.

Mag-import mula sa file path papunta sa file path

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",
)

Mag-import mula sa file path papunta sa memory

Ang import_file ay nagbabalik ng bytes ng na-import na file. Ipinapakita ng halimbawa sa ibaba ang pag-assign sa variable na file_bytes at ang pagsuri sa nilalaman ng simula ng isang Editor export archive.

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'

Mag-import mula sa memory

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'

Mag-import ng mga file sa isang directory

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"
)

Mag-import ng mga file sa isang directory na maaaring naglalaman ng mga hindi suportadong file type

Ang default na pag-uugali ng Glasswall Python wrapper ay mag-raise ng kaugnay na exception (tingnan ang: glasswall.libraries.editor.errors) kung mabigo ang pagproseso. Ang pagpasa ng raise_unsupported=False ay pipigil sa pag-raise ng exception at maaaring maging kapaki-pakinabang kapag nagtatrabaho sa isang directory na naglalaman ng halo ng parehong supported at unsupported na mga file type kapag nais na maproseso ang pinakamarami sa mga file hangga't maaari sa halip na tumigil sa unang pagkabigo.

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
)

Mag-import ng mga file sa isang directory gamit ang custom na policy sa pamamahala ng content

Gamit ang 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
)

Mag-import ng mga file sa isang directory nang may kondisyon batay sa format ng file

Ipinapakita ng halimbawa sa ibaba ang pagproseso lamang ng mga doc at docx file mula sa isang nested directory na naglalaman ng maraming format ng file.

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 at Analyse

Hinahayaan ka ng mga high-level function na ito na magpatakbo ng Export o Import at bumuo ng analysis report sa loob ng iisang session. Para sa higit pang impormasyon, tingnan ang mga link ng dokumentasyon sa ibaba.