hrcp¶
Main module exporting the public API.
Usage¶
Classes¶
Resource
¶
A node in the HRCP configuration tree.
Each Resource represents a single configurable entity with: - A unique name within its parent's children - A dictionary of configuration attributes - Optional parent reference (None for root) - Dictionary of child Resources
Example
root = Resource(name="region", attributes={"provider": "aws"}) dc = Resource(name="us-east-1") root.add_child(dc) dc.path '/region/us-east-1'
Source code in src/hrcp/core.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
name
property
¶
The resource's name (unique within parent).
path
property
¶
The full path from root to this Resource.
Returns:
| Type | Description |
|---|---|
str
|
Path string like '/region/datacenter/host'. |
parent
property
¶
The parent Resource, or None if this is a root.
children
property
¶
Dictionary of child Resources keyed by name.
attributes
property
¶
The resource's configuration attributes.
__init__(name, attributes=None)
¶
Create a new Resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique identifier for this resource within its parent. Cannot be empty or contain '/'. |
required |
attributes
|
dict[str, Any] | None
|
Initial configuration key-value pairs. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If name is empty or contains '/'. |
Source code in src/hrcp/core.py
add_child(child)
¶
Add a child Resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
Resource
|
The Resource to add as a child. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a child with the same name already exists. |
Source code in src/hrcp/core.py
remove_child(name)
¶
Remove and return a child Resource by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the child to remove. |
required |
Returns:
| Type | Description |
|---|---|
Resource
|
The removed Resource. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no child with that name exists. |
Source code in src/hrcp/core.py
get_child(name)
¶
Get a child Resource by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the child to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Resource | None
|
The child Resource, or None if not found. |
set_attribute(key, value)
¶
Set an attribute value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The attribute name. |
required |
value
|
Any
|
The attribute value. |
required |
get_attribute(key, default=None)
¶
Get an attribute value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The attribute name. |
required |
default
|
Any
|
Value to return if attribute doesn't exist. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The attribute value, or default if not found. |
Source code in src/hrcp/core.py
delete_attribute(key)
¶
Delete an attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The attribute name to delete. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the attribute doesn't exist. |
ResourceTree
¶
Container and manager for an HRCP resource hierarchy.
The ResourceTree provides a convenient interface for working with a tree of Resources, including path-based access, creation, and traversal operations.
Example
tree = ResourceTree(root_name="infrastructure") tree.create("/infrastructure/us-east/dc1", attributes={"region": "us-east-1"}) host = tree.get("/infrastructure/us-east/dc1") host.attributes
Source code in src/hrcp/core.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
root
property
¶
The root Resource of the tree.
__init__(root_name='root')
¶
Create a new ResourceTree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_name
|
str
|
Name for the root Resource. |
'root'
|
create(path, attributes=None)
¶
Create a Resource at the specified path.
Creates any intermediate Resources needed to build the path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path where the Resource should be created. |
required |
attributes
|
dict[str, Any] | None
|
Initial attributes for the new Resource. |
None
|
Returns:
| Type | Description |
|---|---|
Resource
|
The newly created Resource. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If path doesn't start with root name or if Resource already exists at path. |
Source code in src/hrcp/core.py
get(path)
¶
Get a Resource by its path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to the Resource (e.g., '/root/child/grandchild'). Use '/' to get the root. |
required |
Returns:
| Type | Description |
|---|---|
Resource | None
|
The Resource at the path, or None if not found. |
Source code in src/hrcp/core.py
delete(path)
¶
Delete a Resource and its subtree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to the Resource to delete. |
required |
Returns:
| Type | Description |
|---|---|
Resource
|
The deleted Resource. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If attempting to delete the root. |
KeyError
|
If no Resource exists at the path. |
Source code in src/hrcp/core.py
walk(start_path='/')
¶
Iterate over all Resources in the tree (depth-first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_path
|
str
|
Path to start walking from (default: root). |
'/'
|
Yields:
| Type | Description |
|---|---|
Resource
|
Each Resource in depth-first order. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If start_path doesn't exist. |
Source code in src/hrcp/core.py
query(pattern)
¶
Query resources matching a wildcard pattern.
Supports: - Single wildcard (): matches any single path segment - Double wildcard (*): matches any number of segments (including zero)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Path pattern like '/root//child' or '/root/*/leaf' |
required |
Returns:
| Type | Description |
|---|---|
list[Resource]
|
List of matching Resources. |
Source code in src/hrcp/core.py
query_values(pattern, key, mode)
¶
Get attribute values from resources matching a pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Path pattern for matching resources. |
required |
key
|
str
|
The attribute key to retrieve. |
required |
mode
|
PropagationMode
|
Propagation mode for value resolution. |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
List of values from matching resources (excludes None values). |
Source code in src/hrcp/core.py
to_dict()
¶
Serialize the tree to a dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dict representation of the tree that can be serialized to JSON. |
from_dict(data)
classmethod
¶
Create a ResourceTree from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
A dict representation of the tree. |
required |
Returns:
| Type | Description |
|---|---|
ResourceTree
|
A new ResourceTree with the data. |
Source code in src/hrcp/core.py
to_json(path, indent=2)
¶
Save the tree to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the output JSON file. |
required |
indent
|
int
|
Indentation level for human-readable output. |
2
|
from_json(path)
classmethod
¶
Load a ResourceTree from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the JSON file. |
required |
Returns:
| Type | Description |
|---|---|
ResourceTree
|
A new ResourceTree loaded from the file. |
PropagationMode
¶
Bases: Enum
Defines how attribute values propagate through the resource hierarchy.
Attributes:
| Name | Type | Description |
|---|---|---|
NONE |
No propagation - only the resource's own value is used. |
|
INHERIT |
Inheritance - values propagate from ancestors to descendants. The closest ancestor with the value wins. |
|
AGGREGATE |
Aggregation - values are collected from all descendants. Returns a list of all values found in the subtree. |
|
MERGE |
Deep merge - dict values are recursively merged from ancestors, with descendant values taking precedence. |
|
REQUIRE_PATH |
All ancestors (including self) must have a truthy value. Returns the local value if ALL nodes from self to root have the attribute set to a truthy value, else None. |
|
COLLECT_ANCESTORS |
Collect values from self up to root into a list. Useful for custom AND/OR logic across the path. |
Source code in src/hrcp/propagation.py
INHERIT = auto()
class-attribute
instance-attribute
¶
AGGREGATE = auto()
class-attribute
instance-attribute
¶
MERGE = auto()
class-attribute
instance-attribute
¶
REQUIRE_PATH = auto()
class-attribute
instance-attribute
¶
COLLECT_ANCESTORS = auto()
class-attribute
instance-attribute
¶
NONE = auto()
class-attribute
instance-attribute
¶
Provenance
dataclass
¶
Records the origin and resolution path of a configuration value.
Attributes:
| Name | Type | Description |
|---|---|---|
value |
Any
|
The resolved configuration value. |
source_path |
str
|
The path of the resource that provided the value. For INHERIT/NONE, this is the single source. For AGGREGATE, this is the root of the aggregation. For MERGE, this is the deepest resource. |
mode |
PropagationMode
|
The propagation mode used to resolve the value. |
key_sources |
dict[str, str]
|
For MERGE with dict values, maps each key to the path of the resource that provided it. Uses dot notation for nested keys (e.g., "logging.level"). |
contributing_paths |
list[str]
|
For AGGREGATE/COLLECT_ANCESTORS, lists all resource paths that contributed values. |
Source code in src/hrcp/provenance.py
Functions¶
get_value(resource, key, mode, default=None, *, with_provenance=False)
¶
Get a configuration value with optional provenance information.
This is the unified API for retrieving values based on propagation mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource
|
Resource
|
The Resource to get the value for. |
required |
key
|
str
|
The attribute key. |
required |
mode
|
PropagationMode
|
The propagation mode to use. |
required |
default
|
Any
|
Default value if attribute not found (not used for AGGREGATE mode or when with_provenance=True). |
None
|
with_provenance
|
bool
|
If True, return a Provenance object with source tracking. If False, return just the value. |
False
|
Returns:
| Type | Description |
|---|---|
Any | Provenance | None
|
If with_provenance=False: The effective value based on propagation mode, or the default if not found. |
Any | Provenance | None
|
If with_provenance=True: Provenance object containing the value and its origin information, or None if value doesn't exist (except for AGGREGATE mode which returns Provenance with empty list). |