FHIR IG generation (dhis2w_fhir)¶
dhis2w_fhir is the package behind d2w fhir: the fhir.toml
document, the emitters that turn DHIS2 metadata into an Implementation Guide - FSH for the
definitional artifacts, pre-built R4 JSON for the organisation-unit registry, the
option-set terminology, and the category terminology - and the DHIS2 period grammar they
share. It mounts onto the CLI
and the MCP server through the dhis2.plugins entry point, and every component symbol
re-exports from the top-level package, so
from dhis2w_fhir import GenerateConfig, parse_period keeps working however
the components are arranged internally. The R4 resource models are the one exception: they
live in dhis2w_fhir.r4, which is FHIR's own vocabulary rather than part of the plugin
surface.
When to reach for it¶
- Read or write a project's
fhir.tomlfrom Python (load_project,load_fhir_config,find_project_fhir_config,write_fhir_config). - Parse a DHIS2 ISO period into its type and date range, or walk backwards from a date
(
parse_period,recent_periods,PERIOD_TYPE_DEFINITIONS). - Carry DHIS2 attribute values onto a generated resource (
AttributeValueIn,AttributeCodeIndex,resolve_attribute_code_index,attribute_value_extensions). - Build FSH artifacts without the CLI (
build_foundation_artifacts,build_questionnaire_artifacts,build_page_artifacts) and sync them to disk (sync_artifacts). - Build the pre-built R4 documents - the registry (
dhis2w_fhir.r4.Organization,dhis2w_fhir.r4.Location), the option-set terminology (build_option_set_artifacts), and the category terminology (build_category_artifacts), the last two ondhis2w_fhir.r4.CodeSystemanddhis2w_fhir.r4.ValueSet- and sync them to disk (sync_json_artifacts), one owned directory per source.
Worked example — parse a period, then walk backwards¶
import datetime
from dhis2w_fhir import parse_period, recent_periods
parse_period("2024BiW2")
# PeriodValue(iso='2024BiW2', period_type='BiWeekly',
# start_date=date(2024, 1, 15), end_date=date(2024, 1, 28))
recent_periods("Monthly", 3, datetime.date(2026, 8, 2))
# ['202607', '202606', '202605']
Reference¶
Periods¶
period
¶
DHIS2 reporting periods: the ISO period grammar, its period-type catalogue, the parser, and its inverse.
Classes¶
PeriodTypeDefinition
¶
Bases: BaseModel
One DHIS2 period type as terminology: its name, its ISO format, and a display phrase.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/period/schemas.py
PeriodValue
¶
Bases: BaseModel
One DHIS2 reporting period: its ISO identifier, its period type, and the dates it covers.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/period/schemas.py
Functions¶
parse_period(iso)
¶
Parse a DHIS2 ISO period string into its period type, start date, and end date.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/period/parser.py
recent_periods(period_type, count, today)
¶
The count most recent completed ISO periods of period_type, newest first.
A period counts as completed when its end date falls strictly before today. An
unregistered period type, or a non-positive count, yields an empty list rather than
raising - the caller is discovering data, not validating configuration.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/period/recent.py
Project configuration¶
config
¶
The fhir.toml document: its models plus discovery, load, and save.
A FHIR IG project is any directory holding a fhir.toml (scaffolded by
d2w fhir init). Discovery walks up from the working directory, mirroring
how .dhis2/profiles.toml is found.
The document composes the per-component selection tables, so this module depends on the components and never the other way round - an emitter receives its config as a parameter.
Classes¶
NoFhirProjectError
¶
IgConfig
¶
Bases: BaseModel
SUSHI IG identity - the [ig] table of fhir.toml.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/config.py
NamingConfig
¶
Bases: BaseModel
Configurable FSH naming tokens - the [generate.naming] table of fhir.toml.
Artifact names merge the prefix and kind tokens and underscore the rest
(D2 + OS + _BirthType + _CS); ids join the kebab of each non-empty token
(d2-os-birth-type-cs). prefix, option_set, category, data_set, program, and
program_stage may be empty to drop them; organisation_unit must stay non-empty or the
org-unit artifact names would degenerate to bare _CS/_Level_CS. Future group /
group-set artifacts follow the same scheme (OUG, OUGS).
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/config.py
GenerateConfig
¶
Bases: BaseModel
Generation behaviour - the [generate] table of fhir.toml.
The three data-definition tables select the three questionnaire form kinds: data_sets
picks aggregate data sets, event_programs picks programs without registration, and
tracker_programs picks programs with registration, one Questionnaire per program stage.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/config.py
FhirProjectConfig
¶
Bases: BaseModel
The full parsed fhir.toml document.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/config.py
FhirProject
¶
Bases: BaseModel
A discovered FHIR IG project: parsed config plus where it lives on disk.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/config.py
Attributes¶
project_root
property
¶
Directory containing fhir.toml.
ig_directory
property
¶
The SUSHI IG directory (<project_root>/ig).
fsh_directory
property
¶
The FSH source directory (<project_root>/ig/input/fsh).
resources_directory
property
¶
The predefined-resource directory (<project_root>/ig/input/resources), loaded without a FSH compile.
Functions¶
find_project_fhir_config(start=None)
¶
Walk up from start (defaulting to $PWD) looking for fhir.toml.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/config.py
load_fhir_config(path)
¶
Parse and validate a fhir.toml file.
write_fhir_config(path, config)
¶
Write a fhir.toml with default permissions - it is committed project config, not a credential store.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/config.py
load_project(start=None)
¶
Discover and load the nearest FHIR project, raising NoFhirProjectError when there is none.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/config.py
DHIS2 attribute values¶
The projection every generated resource carries its DHIS2 attribute values on, plus
the uid -> code index a generate run resolves once against /api/attributes and
every emitter joins against. DHIS2 sends an attribute value as an attribute UID and a
string, so the code is a lookup rather than part of the value.
attributes
¶
DHIS2 attribute values shared by every component: the projection plus the emit-time code index.
Classes¶
AttributeValueIn
¶
Bases: BaseModel
One DHIS2 attribute value: the attribute it belongs to, and the value the instance holds.
DHIS2 sends {"attribute": {"id": "ihn1wb9eho8"}, "value": "KE03"} and nothing else - no
code, no name, no value type - so the projection carries the UID and the value alone and
the attribute's code is joined from AttributeCodeIndex at emit time. The value is a
string whatever the attribute's declared DHIS2 value type; a GeoJSON-valued attribute
arrives as the serialised document.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/attributes.py
AttributeCodeIndex
¶
Bases: BaseModel
Every DHIS2 attribute's code keyed by its UID - the join one generate run resolves once.
An attribute DHIS2 left without a code is absent from the mapping rather than present with
an empty one: most instances code few of their attributes, and some code none of them, so
every consumer reads through code_for and decides what a missing code means for it.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/attributes.py
FHIR R4 resource schemas¶
The models every pre-built JSON document is serialised from - Organization and
Location for the registry, CodeSystem and ValueSet for the option-set and
category terminology. Every one is frozen, alias-aware, and closed to unknown keys, so
Model.model_validate(payload).model_dump_json(exclude_none=True, by_alias=True)
reproduces the input document key for key.
schemas
¶
FHIR R4 schemas for the resources this package emits - Organization, Location, CodeSystem, ValueSet - plus elements.
The models mirror the JSON SUSHI produces for the generated implementation guide, so every
model round-trips: Model.model_validate(payload).model_dump_json(exclude_none=True, by_alias=True)
reproduces the input document key for key. The primitive-extension keys _name and _title are not
legal Pydantic field names, so they are carried by name_element and title_element: validation
accepts either the underscore key or the field name, and serialisation under by_alias=True writes
the underscore key back.
Classes¶
FhirBase
¶
Bases: BaseModel
Pydantic carrier for every schema here - frozen, alias-aware, closed to unknown keys. Not a FHIR type.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
Element
¶
Bases: FhirBase
Element - the R4 root for datatypes, and the _x sibling a primitive's extensions hang from.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
BackboneElement
¶
Bases: Element
BackboneElement - an element defined inside a resource rather than as a reusable datatype.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
Resource
¶
DomainResource
¶
Bases: Resource
DomainResource - a resource carrying narrative and extensions; every resource emitted here is one.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
Meta
¶
Identifier
¶
Bases: Element
A business identifier: the DHIS2 UID or code under its identifier system.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
Coding
¶
Bases: Element
One code drawn from a code system, with the display the system gives it.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
CodeableConcept
¶
Bases: Element
A concept expressed as one or more codings, optionally with free text.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
Reference
¶
Bases: Element
A literal reference to another resource, such as Organization/mOsABqg3Cqw.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
ContactPoint
¶
Bases: Element
A telecom contact point - the phone number or email address of an organisation unit.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
HumanName
¶
Attachment
¶
Bases: Element
Attached content - the base64 GeoJSON boundary a Location extension carries.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
Extension
¶
Bases: Element
One extension: either a nested set of extensions or a single value[x] choice.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
OrganizationContact
¶
Bases: BackboneElement
Organization.contact - a contact party for the organisation unit.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
LocationPosition
¶
Bases: BackboneElement
Location.position - the WGS84 point of an organisation unit.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
CodeSystemProperty
¶
Bases: BackboneElement
CodeSystem.property - the declaration of a property the concepts in the code system may carry.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
CodeSystemConceptProperty
¶
Bases: BackboneElement
CodeSystem.concept.property - one declared property carried by a single concept.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
CodeSystemConceptDesignation
¶
Bases: BackboneElement
CodeSystem.concept.designation - the translation of a concept display into one locale.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
CodeSystemConcept
¶
Bases: BackboneElement
CodeSystem.concept - one DHIS2 option, keyed by its option UID.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
ValueSetInclude
¶
Bases: BackboneElement
ValueSet.compose.include - one code system the value set draws its codes from.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
ValueSetCompose
¶
Bases: BackboneElement
ValueSet.compose - the content logic that builds the expansion of the value set.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
Organization
¶
Bases: DomainResource
A FHIR R4 Organization as generated from one DHIS2 organisation unit.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
Location
¶
Bases: DomainResource
A FHIR R4 Location as generated from the physical place of one DHIS2 organisation unit.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
CodeSystem
¶
Bases: DomainResource
A FHIR R4 CodeSystem as generated from one DHIS2 option set, one concept per option.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
ValueSet
¶
Bases: DomainResource
A FHIR R4 ValueSet as generated from one DHIS2 option set, composing the whole matching CodeSystem.
Source code in packages/dhis2w-fhir/src/dhis2w_fhir/r4/schemas.py
Package surface¶
The names below re-export from dhis2w_fhir itself; the guide's
generate targets section covers what each emitter
produces.
dhis2w_fhir
¶
Version-neutral FHIR IG generation: fhir.toml config, FSH emission, and project scaffolding.
Each component owns its schemas; this module is the one stable import
surface over them, so from dhis2w_fhir import GenerateConfig keeps working
however the components are arranged internally.