_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 | absolute |
Convert a relative path to an absolute path, resolving '..' segments. |
| Function | build |
Build a ZIP archive from files matching a pattern and return data. |
| Function | execute |
Execute a shell command in the given directory. |
| Function | export |
Export an object's data to a file on disk. |
| Function | extract |
Extract the file extension from a path. |
| Function | extract |
Extract the filename from a file path. |
| Function | extract |
Unpack a ZIP archive to the directory containing the archive file. |
| Function | find |
Search a directory (optionally recursively) for a file with the given extension. |
| Function | get |
Format a file size in bytes as a human-readable string. |
| Function | get |
Extract the directory path from a full file path (removing filename). |
| Function | get |
Normalize a path string to use OS-appropriate separators. |
| Function | get |
Extract files from a ZIP archive and return a list of extracted files. |
| Function | import |
Import a ZEXP file into the current container and normalize sort IDs. |
| Function | import |
Import a ZEXP file from the INSTANCE_HOME/import folder. |
| Function | mk |
Create a directory and any necessary parent directories. |
| Function | read |
List directory contents with metadata. |
| Function | read |
Read a file and return its data along with content-type information. |
| Function | read |
Read directory contents and return a list of file/directory info dicts. |
| Function | remove |
Remove a file or directory. |
| Function | tail |
Read the last N lines from a file, similar to 'tail -N filename'. |
| Function | write |
Recursively write files matching a filter pattern to a ZIP archive. |
Convert a relative path to an absolute path, resolving '..' segments.
| Parameters | |
| path:str | File path (may contain '..' segments) |
| Returns | |
| str | Absolute path with resolved parent references |
Build a ZIP archive from files matching a pattern and return data.
| Parameters | |
| files:str | File path pattern (e.g. '/path/to/dir/*.txt') |
| get | If True, return archive data; if False, return temp filename |
| Returns | |
| bytes or str | ZIP archive data (bytes) or temporary file path |
Execute a shell command in the given directory.
| Parameters | |
| path:str | Working directory for the command |
| command:str | Shell command to execute |
Export an object's data to a file on disk.
Supports MyBlob objects, Zope objects, ImageFile objects, file-like objects, and raw data.
| Parameters | |
| obj | The object to export |
| filename:str | Destination file path |
| Returns | |
| bytes or None | The exported data, or None if no data |
Extract the file extension from a path.
| Parameters | |
| path:str | File path |
| Returns | |
| str | File extension (without dot) |
Extract the filename from a file path.
| Parameters | |
| path:str | File path |
| sep:str or None | Custom separator (default: OS separator) |
| undoable:bool | Whether special characters should be preserved |
| Returns | |
| str | Filename portion of the path |
Unpack a ZIP archive to the directory containing the archive file.
Skips macOS metadata files (__MACOSX/, .DS_Store).
| Parameters | |
| file:str | Path to the ZIP archive file |
| Returns | |
| list | List of extracted file paths |
Search a directory (optionally recursively) for a file with the given extension.
| Parameters | |
| extension:str | File extension to search for (without dot) |
| path:str | Directory path to search in |
| deep:int | Whether to search subdirectories recursively |
| Returns | |
| str or None | Full path to the first matching file, or None |
Format a file size in bytes as a human-readable string.
| Parameters | |
| len:int or float | File size in bytes |
| Returns | |
| str | Human-readable size string (e.g. '1KB', '2.5 MB') |
Extract the directory path from a full file path (removing filename).
| Parameters | |
| path:str | Full file path |
| Returns | |
| str | Directory path |
Normalize a path string to use OS-appropriate separators.
| Parameters | |
| path:str or bytes | File path to normalize |
| chs:list | List of character codes to strip (unused) |
| undoable:bool | Whether special characters should be preserved |
| Returns | |
| str | Normalized path |
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 | |
| f | ZIP file data or object to extract |
| Returns | |
| list | List of file info dicts (from readPath) |
Import a ZEXP file into the current container and normalize sort IDs.
| Parameters | |
| self:OFS.ObjectManager | The container object to import into |
| zexp | The ZEXP file object to import |
| new | Target ID for the imported object |
| id | ID prefix for sort-ID normalization |
| _sort | Initial sort ID |
Import a ZEXP file from the INSTANCE_HOME/import folder.
| Parameters | |
| self:OFS.ObjectManager | The container object to import into |
| filename:str | Name of the ZEXP file in the import folder |
List directory contents with metadata.
| Parameters | |
| path:str | Directory path to list |
| Returns | |
| list | List of dicts with keys: path, file, mtime, size, type ('d' or 'f'), sorted by type (directories first) |
Read a file and return its data along with content-type information.
Uses a filestream_iterator for files larger than the threshold.
| Parameters | |
| filename:str | Path to the file to read |
| mode:str | File open mode ('b' for binary, 't' for text) |
| threshold:int | Size threshold in bytes for using filestream_iterator (-1 to always read into memory) |
| Returns | |
| tuple | Tuple of (data, content_type, encoding, file_size) |
Read directory contents and return a list of file/directory info dicts.
| Parameters | |
| path:str | Directory path (may include glob wildcards) |
| data:bool | Whether to read file data |
| recursive:bool | Whether to recurse into subdirectories |
| Returns | |
| list | List of dicts with keys: local_filename, filename, mtime, size, content_type, encoding, data (if requested), isdir |
Remove a file or directory.
| Parameters | |
| path:str | Path to remove |
| deep:int | Unused (directories are always removed recursively) |
Read the last N lines from a file, similar to 'tail -N filename'.
| Parameters | |
| filename:str | Path to the file to read |
| linesback:int | Number of lines to read from end of file |
| returnlist:int | If true, return a list of lines; otherwise a string |
| Returns | |
| str or list | Last N lines as a string or list |