Skip to main content

Hierarchical Resource Configuration with Provenance

Project description

HRCP

Hierarchical Resource Configuration with Provenance

A minimal Python library for hierarchical configuration where you always know where values came from.

The Problem

Configuration in hierarchical systems (orgs → teams → projects, regions → clusters → services) needs:

  • Inheritance: Set defaults at the top, override below
  • Aggregation: Roll up values from children
  • Traceability: Know exactly which node provided each value

HRCP solves this with ~2000 lines of dependency-free Python.

Installation

pip install hrcp

Quick Example

from hrcp import ResourceTree, PropagationMode, get_value

# Build a hierarchy
tree = ResourceTree(root_name="platform")
tree.root.set_attribute("timeout", 30)
tree.root.set_attribute("env", "prod")

tree.create("/platform/us-east/api", attributes={"timeout": 60})
tree.create("/platform/us-east/db")
tree.create("/platform/eu-west/api")

# Inheritance: values flow DOWN
api = tree.get("/platform/us-east/api")
timeout = get_value(api, "timeout", PropagationMode.DOWN)
# timeout == 60 (local override)

db = tree.get("/platform/us-east/db")
timeout = get_value(db, "timeout", PropagationMode.DOWN)
# timeout == 30 (inherited from root)

# Provenance: know where it came from
prov = get_value(db, "timeout", PropagationMode.DOWN, with_provenance=True)
print(prov.value)        # 30
print(prov.source_path)  # "/platform" - the root provided this value

Propagation Modes

DOWN - Inherit from Ancestors

Values cascade from parent to children. Closest ancestor wins.

tree.root.set_attribute("tier", "premium")
tree.create("/org/team/project")

project = tree.get("/org/team/project")
tier = get_value(project, "tier", PropagationMode.DOWN)
# "premium" - inherited from root

UP - Aggregate from Descendants

Collect all values from the subtree.

tree.create("/org/team1", attributes={"headcount": 5})
tree.create("/org/team2", attributes={"headcount": 8})

counts = get_value(tree.root, "headcount", PropagationMode.UP)
# [5, 8]

MERGE_DOWN - Deep Dict Merge

Recursively merge dicts through the hierarchy.

tree.root.set_attribute("config", {"db": {"host": "localhost", "port": 5432}})
tree.create("/org/prod", attributes={"config": {"db": {"host": "prod.db.internal"}}})

prod = tree.get("/org/prod")
config = get_value(prod, "config", PropagationMode.MERGE_DOWN)
# {"db": {"host": "prod.db.internal", "port": 5432}}

NONE - Local Only

Only return the value if set directly on the resource.

Provenance

The killer feature. Always know where a value came from:

from hrcp import get_value

prov = get_value(resource, "timeout", PropagationMode.DOWN, with_provenance=True)
prov.value        # The resolved value
prov.source_path  # Path of the resource that provided it
prov.mode         # The propagation mode used

For MERGE_DOWN, provenance tracks which resource contributed each key via prov.key_sources.

Wildcards

Query multiple resources at once:

# * matches one segment
tree.query("/platform/*/api")

# ** matches any depth
tree.query("/platform/**/config")

# Get values across matches
tree.query_values("/platform/*/api", "port", PropagationMode.NONE)

Serialization

# JSON
tree.to_json("config.json")
tree = ResourceTree.from_json("config.json")

# YAML
tree.to_yaml("config.yaml")
tree = ResourceTree.from_yaml_file("config.yaml")

# Dict
data = tree.to_dict()
tree = ResourceTree.from_dict(data)

Use Cases

Multi-tenant SaaS

/platform
  timeout: 30
  /tenant-a        # inherits timeout=30
    /project-1
  /tenant-b
    timeout: 60    # override for this tenant
    /project-2     # inherits timeout=60

Infrastructure Config

/infra
  region: us-east-1
  /prod
    /api
      replicas: 3
    /worker
      replicas: 5
  /staging          # inherits region
    /api
      replicas: 1

Feature Flags

/features
  dark_mode: false
  /beta_users
    dark_mode: true   # beta users get dark mode
    /user-123         # inherits dark_mode=true

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hrcp-0.3.0.tar.gz (94.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hrcp-0.3.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file hrcp-0.3.0.tar.gz.

File metadata

  • Download URL: hrcp-0.3.0.tar.gz
  • Upload date:
  • Size: 94.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hrcp-0.3.0.tar.gz
Algorithm Hash digest
SHA256 29cbe1d2733739cdf793df8dd60007e81506bb25808f5b7f11f53e3ecc49c5eb
MD5 4e8bbdf29236378c3c0db4157fba4a97
BLAKE2b-256 54452fe5717ba91b1612a6f431b1471f4262511dbb115197c0bc0bd2da64c40d

See more details on using hashes here.

Provenance

The following attestation bundles were made for hrcp-0.3.0.tar.gz:

Publisher: pypi-package.yaml on keongalvin/hrcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hrcp-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: hrcp-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hrcp-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ccb39b603a05d7a119c0246d3179e680afc070a060005b365084afc958ca5775
MD5 a357d91d4433568e03f380db2e429e00
BLAKE2b-256 5c4c5baccf4355a8970832464d8e27b16b2b997f9eabddfe412f01c29f2814cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for hrcp-0.3.0-py3-none-any.whl:

Publisher: pypi-package.yaml on keongalvin/hrcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page