module documentation

_fileutil.py - ZMS File System Utilities

This module provides file-system utility functions for ZMS, including path handling, file reading/writing, ZEXP import/export, ZIP archive creation/extraction, and data size formatting.

License: GNU General Public License v2 or later, Organization: ZMS Publishing

Function absoluteOSPath Convert a relative path to an absolute path, resolving '..' segments.
Function buildZipArchive Build a ZIP archive from files matching a pattern and return data.
Function executeCommand Execute a shell command in the given directory.
Function exportObj Export an object's data to a file on disk.
Function extractFileExt Extract the file extension from a path.
Function extractFilename Extract the filename from a file path.
Function extractZipArchive Unpack a ZIP archive to the directory containing the archive file.
Function findExtension Search a directory (optionally recursively) for a file with the given extension.
Function getDataSizeStr Format a file size in bytes as a human-readable string.
Function getFilePath Extract the directory path from a full file path (removing filename).
Function getOSPath Normalize a path string to use OS-appropriate separators.
Function getZipArchive Extract files from a ZIP archive and return a list of extracted files.
Function import_zexp Import a ZEXP file into the current container and normalize sort IDs.
Function importZexp Import a ZEXP file from the INSTANCE_HOME/import folder.
Function mkDir Create a directory and any necessary parent directories.
Function readDir List directory contents with metadata.
Function readFile Read a file and return its data along with content-type information.
Function readPath Read directory contents and return a list of file/directory info dicts.
Function remove Remove a file or directory.
Function tail_lines Read the last N lines from a file, similar to 'tail -N filename'.
Function writeZipFile Recursively write files matching a filter pattern to a ZIP archive.
def absoluteOSPath(path): (source)

Convert a relative path to an absolute path, resolving '..' segments.

Parameters
path:strFile path (may contain '..' segments)
Returns
strAbsolute path with resolved parent references
def buildZipArchive(files, get_data=True): (source)

Build a ZIP archive from files matching a pattern and return data.

Parameters
files:strFile path pattern (e.g. '/path/to/dir/*.txt')
get_data:boolIf True, return archive data; if False, return temp filename
Returns
bytes or strZIP archive data (bytes) or temporary file path
def executeCommand(path, command): (source)

Execute a shell command in the given directory.

Parameters
path:strWorking directory for the command
command:strShell command to execute
def exportObj(obj, filename): (source)

Export an object's data to a file on disk.

Supports MyBlob objects, Zope objects, ImageFile objects, file-like objects, and raw data.

Parameters
objThe object to export
filename:strDestination file path
Returns
bytes or NoneThe exported data, or None if no data
def extractFileExt(path): (source)

Extract the file extension from a path.

Parameters
path:strFile path
Returns
strFile extension (without dot)
def extractFilename(path, sep=None, undoable=False): (source)

Extract the filename from a file path.

Parameters
path:strFile path
sep:str or NoneCustom separator (default: OS separator)
undoable:boolWhether special characters should be preserved
Returns
strFilename portion of the path
def extractZipArchive(file): (source)

Unpack a ZIP archive to the directory containing the archive file.

Skips macOS metadata files (__MACOSX/, .DS_Store).

Parameters
file:strPath to the ZIP archive file
Returns
listList of extracted file paths
def findExtension(extension, path, deep=1): (source)

Search a directory (optionally recursively) for a file with the given extension.

Parameters
extension:strFile extension to search for (without dot)
path:strDirectory path to search in
deep:intWhether to search subdirectories recursively
Returns
str or NoneFull path to the first matching file, or None
def getDataSizeStr(len): (source)

Format a file size in bytes as a human-readable string.

Parameters
len:int or floatFile size in bytes
Returns
strHuman-readable size string (e.g. '1KB', '2.5 MB')
def getFilePath(path): (source)

Extract the directory path from a full file path (removing filename).

Parameters
path:strFull file path
Returns
strDirectory path
def getOSPath(path, chs=list(range(32)) + [34, 39, 60, 62, 63, 127], undoable=False): (source)

Normalize a path string to use OS-appropriate separators.

Parameters
path:str or bytesFile path to normalize
chs:listList of character codes to strip (unused)
undoable:boolWhether special characters should be preserved
Returns
strNormalized path
def getZipArchive(f): (source)

Extract files from a ZIP archive and return a list of extracted files.

Creates a temporary directory, extracts the archive, reads the contents, then cleans up.

Parameters
fZIP file data or object to extract
Returns
listList of file info dicts (from readPath)
def import_zexp(self, zexp, new_id, id_prefix, _sort_id=0): (source)

Import a ZEXP file into the current container and normalize sort IDs.

Parameters
self:OFS.ObjectManagerThe container object to import into
zexpThe ZEXP file object to import
new_id:strTarget ID for the imported object
id_prefix:strID prefix for sort-ID normalization
_sort_id:intInitial sort ID
def importZexp(self, filename): (source)

Import a ZEXP file from the INSTANCE_HOME/import folder.

Parameters
self:OFS.ObjectManagerThe container object to import into
filename:strName of the ZEXP file in the import folder
def mkDir(path): (source)

Create a directory and any necessary parent directories.

Parameters
path:strDirectory path to create
def readDir(path): (source)

List directory contents with metadata.

Parameters
path:strDirectory path to list
Returns
listList of dicts with keys: path, file, mtime, size, type ('d' or 'f'), sorted by type (directories first)
def readFile(filename, mode='b', threshold=2 << 16): (source)

Read a file and return its data along with content-type information.

Uses a filestream_iterator for files larger than the threshold.

Parameters
filename:strPath to the file to read
mode:strFile open mode ('b' for binary, 't' for text)
threshold:intSize threshold in bytes for using filestream_iterator (-1 to always read into memory)
Returns
tupleTuple of (data, content_type, encoding, file_size)
def readPath(path, data=True, recursive=True): (source)

Read directory contents and return a list of file/directory info dicts.

Parameters
path:strDirectory path (may include glob wildcards)
data:boolWhether to read file data
recursive:boolWhether to recurse into subdirectories
Returns
listList of dicts with keys: local_filename, filename, mtime, size, content_type, encoding, data (if requested), isdir
def remove(path, deep=0): (source)

Remove a file or directory.

Parameters
path:strPath to remove
deep:intUnused (directories are always removed recursively)
def tail_lines(filename, linesback=10, returnlist=0): (source)

Read the last N lines from a file, similar to 'tail -N filename'.

Parameters
filename:strPath to the file to read
linesback:intNumber of lines to read from end of file
returnlist:intIf true, return a list of lines; otherwise a string
Returns
str or listLast N lines as a string or list
def writeZipFile(zf, basepath, path, filter): (source)

Recursively write files matching a filter pattern to a ZIP archive.

Parameters
zf:zipfile.ZipFileOpen ZipFile object to write to
basepath:strBase path for computing archive names
path:strCurrent directory path to scan
filter:strSemicolon-separated glob patterns to match files