intellicrack.core.template_manager
Template file management for the hex editor pattern system.
Manages template storage, loading, saving, and directory structure for both built-in and user-defined binary structure templates.
- class TemplateInfo[source]
Bases:
objectMetadata about a template file.
- Variables:
name (str) – Template name.
description (str) – Human-readable description.
category (str) – Category grouping (e.g. PE, ELF, Custom).
is_builtin (bool) – Whether this is a built-in template.
json_path (Path) – Path to the JSON template file.
dsl_path (Path | None) – Path to the DSL source file, if available.
- exception TemplateBootstrapError[source]
Bases:
RuntimeErrorRaised when built-in template bootstrap encounters failures.
Aggregates per-template failures encountered while exporting built-in templates or parsing template files so callers can react to or report them without losing information.
- Variables:
failed_templates (Sequence[tuple[Path, str]]) – Sequence of
(path, error_message)pairs describing each failure encountered during bootstrap.
- class TemplateManager[source]
Bases:
objectManages template files on disk for the hex editor.
Maintains a directory structure under config_dir/templates/ with builtin and user subdirectories.
- __init__(config_dir)[source]
Initialize the TemplateManager with a configuration directory.
- Parameters:
config_dir (Path) – Base configuration directory.
- Return type:
None
- ensure_directories()[source]
Create the template directory structure if it doesn’t exist.
- Return type:
None
- bootstrap_builtins(document)[source]
Export all built-in templates as JSON files.
Skips export only when every expected built-in template already exists in the registry on disk. Otherwise iterates over the document’s template list, exporting any missing entries via
export_template_jsonand aggregating per-template failures. Failures are logged atwarningand appended toself.failed_templates; if any failures occurred aTemplateBootstrapErroris raised after all templates have been processed.- Parameters:
document (HexDocumentFull) – HexDocument instance exposing
list_templates_detailedandexport_template_json.- Raises:
TemplateBootstrapError – If one or more built-in templates failed to export, or if the document is missing the required template methods while bootstrap is still required.
- Return type:
None
- list_all_templates()[source]
List all available templates (built-in and user).
- Returns:
List of template metadata sorted by name.
- Return type:
- save_user_template(name, json_str, dsl_source=None)[source]
Save a user-defined template.
Writes the JSON template (and the optional DSL source) to disk and only emits
user_template_file_written/user_template_dsl_file_writtenafter the correspondingPath.write_textcall returns successfully. Failures are logged aterrorasuser_template_write_failed/user_template_dsl_write_failedand re-raised so callers can react.- Parameters:
- Returns:
Path to the saved JSON file.
- Return type:
Path
- Raises:
ValueError – If the name is empty or produces an empty filename.
OSError – If writing the JSON or DSL file fails.
- static load_template(path)[source]
Load a template JSON from disk.
- Parameters:
path (Path) – Path to the JSON template file.
- Returns:
JSON content string.
- Return type:
- Raises:
FileNotFoundError – If the file does not exist.
- delete_user_template(name)[source]
Delete a user-defined template.
- Parameters:
name (str) – Template name.
- Returns:
True if the template was deleted.
- Return type:
- Raises:
ValueError – If the name is empty.
- property patterns_dir: Path
The community .hexpat patterns directory.
- Returns:
The vendor community patterns directory path.
- Return type:
Path
- get_pattern_registry()[source]
Get or create the PatternRegistry for .hexpat pattern discovery.
- Returns:
- A PatternRegistry instance, or None if
unavailable.
- Return type:
PatternRegistry | None