ahvn.ukf.templates.basic.resource moduleΒΆ
- class ahvn.ukf.templates.basic.resource.ResourceUKFT(*, name, notes='', short_description='', description='', type='general', version='v0.1.0', version_notes='', variant='default', variant_notes='', content='', content_resources=<factory>, content_composers=<factory>, source='unknown', parents=<factory>, creator='unknown', owner='unknown', workspace='unknown', collection='general', tags=<factory>, synonyms=<factory>, triggers=<factory>, priority=0, related=<factory>, auths=<factory>, timefluid=False, timestamp=<factory>, last_verified=<factory>, expiration=-1, inactive_mark=False, metadata=<factory>, profile=<factory>)[source]ΒΆ
Bases:
BaseUKFResource class for storing file/folder contents as base64 encoded data.
UKF Type: resource Recommended Components of content_resources:
path (str): The original file/directory path.
data (Dict[str, Optional[str]]): Serialized file/directory structure from serialize_path.
annotations (Dict[str, str]): File-level annotations for context.
- Recommended Composers:
- Parameters:
name (UKFMediumTextType)
notes (UKFMediumTextType)
short_description (UKFMediumTextType)
description (UKFMediumTextType)
type (UKFShortTextType)
version (UKFShortTextType)
version_notes (UKFMediumTextType)
variant (UKFShortTextType)
variant_notes (UKFMediumTextType)
content (UKFLongTextType)
content_resources (UKFJsonType)
content_composers (UKFJsonType)
source (UKFShortTextType)
parents (UKFJsonType)
creator (UKFShortTextType)
owner (UKFShortTextType)
workspace (UKFShortTextType)
collection (UKFShortTextType)
tags (UKFTagsType)
synonyms (UKFSynonymsType)
triggers (UKFJsonType)
priority (UKFIntegerType)
related (UKFRelatedType)
auths (UKFAuthsType)
timefluid (UKFBooleanType)
timestamp (UKFTimestampType)
last_verified (UKFTimestampType)
expiration (UKFDurationType)
inactive_mark (UKFBooleanType)
metadata (UKFJsonType)
profile (UKFJsonType)
- classmethod from_path(path, name=None, keep_path=True, **updates)[source]ΒΆ
Create a ResourceUKFT instance from a file or directory path.
Serializes the file or directory contents using serialize_path and automatically configures the resource with appropriate metadata and composers. Only stores essential information (file names and paths) without redundant metadata.
- Parameters:
path (str) β Path to the file or directory to serialize.
name (str, optional) β ResourceUKFT name. If None and path is a directory, generates name from the basename of the path. Required if path is a file.
keep_path (bool) β Whether to keep the original path in content_resources. Defaults to True.
**updates β Additional keyword arguments to update the ResourceUKFT instance attributes.
- Returns:
- New ResourceUKFT instance with pre-configured composers:
diagram: diagram_composer for LLM-friendly folder structure diagram
- Return type:
Example
>>> resource = ResourceUKFT.from_path("/path/to/project") >>> resource.name "project" >>> resource.text("diagram") ''' project/ βββ file1.py βββ src/ βββ main.py '''
- classmethod from_data(data, name=None, path=None, **updates)[source]ΒΆ
Create a ResourceUKFT instance from serialized data.
Allows creating a ResourceUKFT from pre-serialized data without requiring the original file/directory to exist.
- Parameters:
data (Dict[str, Any]) β Serialized file/directory structure from serialize_path.
name (str, optional) β ResourceUKFT name. If None, generates from path or uses βresourceβ.
path (str, optional) β Original path for reference. Used in diagram generation.
**updates β Additional keyword arguments to update the ResourceUKFT instance attributes.
- Returns:
New ResourceUKFT instance with pre-configured composers.
- Return type:
Example
>>> data = {"file.txt": "base64content", "folder/": None} >>> resource = ResourceUKFT.from_data(data, name="my_resource") >>> resource.name "my_resource"
- annotate(file_path, annotation)[source]ΒΆ
Add an annotation to a specific file in the resource.
Allows adding contextual information about files that will appear in the diagram tree, making it more useful for LLM consumption.
- Parameters:
- Returns:
A new ResourceUKFT instance with the annotation added.
- Return type:
Example
>>> resource = ResourceUKFT.from_path("/path/to/project") >>> annotated = resource.annotate("src/main.py", "Main entry point") >>> annotated.text("diagram") ''' project/ βββ src/ βββ main.py # Main entry point '''
- to_path(path)[source]ΒΆ
Extract the resource contents to a specified path.
Deserializes the stored data and recreates the original file or directory structure at the specified destination.
- Parameters:
path (str) β Path where the resource should be extracted.
Example
>>> resource = ResourceUKFT.from_path("/source/data/") >>> resource.to_path("/destination/restored_data/") # Recreates the original directory structure at destination
- __call__(path=None, overwrite=False, cleanup=False)[source]ΒΆ
Return a context manager that extracts the resource to disk.
- Parameters:
path (str, optional) β Destination directory for the extracted files. Defaults to a temp location.
overwrite (bool) β Whether to overwrite existing files at the destination. Defaults to False.
cleanup (bool) β Whether to delete the extracted files upon exiting the context. Defaults to False. Notice that when a user-specified path is provided and the path already exists, cleanup will not delete the existing path to avoid data loss.
- Returns:
Context manager handling extraction and optional cleanup.
- Return type:
_ResourceTempContext
- __enter__()[source]ΒΆ
Extract the resource to a temporary directory using default settings.
- Returns:
Self with _temp_path set during the context lifetime.
- Return type:
- model_config = {'validate_assignment': True, 'validate_default': True}ΒΆ
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_post_init(context, /)ΒΆ
This function is meant to behave like a BaseModel method to initialise private attributes.
It takes context as an argument since thatβs what pydantic-core passes when calling it.
- ahvn.ukf.templates.basic.resource.diagram_composer(kl, **kwargs)[source]ΒΆ
Compose a well-structured folder/tree diagram for LLM consumption.
Creates a hierarchical tree structure showing the file/folder organization with optional annotations using the folder_diagram utility.
- Recommended Knowledge Types:
Resource
- Parameters:
kl (BaseUKF) β Knowledge object containing resource data.
**kwargs β Optional keyword arguments to override content_resources: - path (str): Original file/directory path. - annotations (Dict): File-level annotations.
- Returns:
Formatted tree structure diagram with optional annotations.
- Return type:
Example
>>> kl.content_resources = { ... "path": "project", ... "annotations": {"src/main.py": "Main entry point"} ... } >>> diagram_composer(kl) ''' project/ βββ src/ β βββ main.py # Main entry point '''
- ahvn.ukf.templates.basic.resource.list_composer(kl, ext=None, **kwargs)[source]ΒΆ
Compose a simple list of file paths in the resource with optional extension filtering.
Creates a flat listing of files (not directories) in the resource, with optional annotations and extension filtering.
- Recommended Knowledge Types:
ResourceUKFT
- Parameters:
kl (BaseUKF) β Knowledge object containing resource data.
ext (Union[None, str, List[str]]) β File extension filter. Defaults to None. - If None (default): List all files. - If a string: Filter by that extension (e.g., βpyβ, βmdβ). - If a list: Filter by any of the extensions in the list. Multiple extensions can be separated by commas or semicolons.
**kwargs β Optional keyword arguments to override content_resources: - data (Dict): Serialized file/directory structure. - annotations (Dict): File-level annotations.
- Returns:
Formatted list of file paths with annotations.
- Return type:
Example
>>> kl.content_resources = { ... "data": {"file1.py": "...", "src/main.py": "...", "src/": None}, ... "annotations": {"src/main.py": "Main entry point"} ... } >>> list_composer(kl) ''' - file1.py - src/main.py # Main entry point ''' >>> list_composer(kl, ext="py") ''' - file1.py - src/main.py # Main entry point '''