dpmcore.services.hierarchy
Framework / module / table tree queries on the DPM structure.
Use this service to walk the hierarchy from frameworks down to individual tables, fetch a table’s headers and cells, or resolve the modelling metadata (main property + context property/item) for each header.
Filtering
The supported filter set varies by method:
HierarchyService.get_all_frameworks(),HierarchyService.get_table_details()andHierarchyService.get_table_modelling()acceptrelease_id/release_code/date(at most one).HierarchyService.get_module_version()andHierarchyService.get_tables_for_module()acceptrelease_id/release_codeonly — there is nodateparameter on these.
release_id(int)Restrict to entities valid at the given DPM release.
release_code(str)Restrict by release code (e.g.
"3.4","4.2.1"). RaisesValueErrorif the code does not match any release. Preferred for user-facing input, sinceReleaseIDvalues are opaque from DPM 4.2.1 onwards. Any release code format is accepted.date(str,YYYY-MM-DD)Restrict via
ModuleVersion.from_reference_date/to_reference_date. Useful when the calling system knows the business date but not the corresponding release.
When none is supplied, the active (non-ended) module versions are
returned. Passing more than one raises ValueError.
Releases are ordered chronologically by publication date, so a release filter returns the entities whose release-validity window contains the target release. An unpublished working release has no publication date and is treated as the latest, so filtering at it returns the current (non-ended) entities.
HierarchyService
- class dpmcore.services.hierarchy.HierarchyService(session)[source]
Bases:
objectHierarchical queries on the DPM structure.
- Parameters:
session (
Session) – An open SQLAlchemy session.
- __init__(session)[source]
Build the service bound to
session.- Parameters:
session (Session)
- Return type:
None
- get_all_frameworks(release_id=None, date=None, release_code=None, deep=False, historical=False)[source]
Return frameworks, optionally as a Framework→Module→Table tree.
- Parameters:
release_id (
Optional[int]) – Restrict module versions to a release.date (
Optional[str]) – Restrict module versions valid at a date (YYYY-MM-DD).release_code (
Optional[str]) – Restrict by release code (resolved to ID).deep (
bool) – When True, nestmodule_versionsandtable_versionsunder each framework. When False (default), return flatFrameworkrows.historical (
bool) – When True and no other filter is given, return every module version (no active-only fallback). Ignored when any ofrelease_id/release_code/dateis supplied. Only meaningful withdeep=True.
- Return type:
- Returns:
A list of framework dictionaries. With
deep=True, each framework containsmodule_versions(possibly empty when the framework has no module versions matching the filter), and each module version containstable_versions(possibly empty for the same reason). Frameworks, modules, and tables are joined with LEFT OUTER JOIN so empty slots do not silently disappear from the tree. Filter predicates are pushed into aModuleVersionsubquery so they don’t turn the outer joins into an inner filter.- Raises:
ValueError – If more than one of
release_id,date, orrelease_codeis given, or ifrelease_codedoes not match any release.
- get_module_version(module_code, release_id=None, release_code=None)[source]
Return module version info for a given module code.
When neither
release_idnorrelease_codeis supplied, only the currently-activeModuleVersion(end_release_id IS NULL) is considered, so a module that has been republished across releases resolves deterministically.
- get_table_details(table_code, release_id=None, date=None, release_code=None)[source]
Return table version with headers and cells.
All three filters resolve through the same module-version join as
get_table_modelling(), so the two methods always pick the sameTableVersionfor a given query.- Parameters:
- Return type:
- Returns:
Table version dictionary with
headersandcells, orNoneif the table does not exist for the requested filters.- Raises:
ValueError – If more than one of
release_id,date, orrelease_codeis given, or ifrelease_codedoes not match any release.
- get_table_modelling(table_code, release_id=None, date=None, release_code=None)[source]
Return modelling metadata for a table keyed by header_id.
For each header on the resolved table version, returns up to two entries:
{"main_property_code": ..., "main_property_name": ...}when the header has a property assigned.{"context_property_code": ..., "context_property_name": ..., "context_item_code": ..., "context_item_name": ...}when the header carries a context composition.
- Parameters:
- Return type:
- Returns:
Mapping
header_id→ list of property/context entries. Every header that appears on the resolved table version is present in the mapping, including ones with no joined metadata — those map to an empty list rather than being omitted. The mapping is empty only when the table has no headers at all. Raises if the table itself cannot be resolved.- Raises:
ValueError – If more than one of
release_id,date, orrelease_codeis given, ifrelease_codedoes not match any release, or if no table version matches the filters.
Examples
Fetch the framework tree consumed by a DPM browser UI:
from dpmcore import connect
with connect("postgresql://user:pass@host/dpm") as db:
tree = db.services.hierarchy.get_all_frameworks(deep=True)
for fw in tree:
print(fw["code"], len(fw["module_versions"]))
Resolve a table at a given business date:
details = db.services.hierarchy.get_table_details(
table_code="C_01.00",
date="2024-06-30",
)
Read the header-level modelling metadata for a table:
modelling = db.services.hierarchy.get_table_modelling(
table_code="C_01.00",
release_code="4.2.1",
)
for header_id, entries in modelling.items():
for entry in entries:
# entry is either {main_property_code, main_property_name}
# or {context_property_code, context_property_name,
# context_item_code, context_item_name}
...