Skip to content

A Python FHIR specification parser and class generator

License

Notifications You must be signed in to change notification settings

josealmeida/fhir-parser

 
 

Repository files navigation

Python FHIR Parser

A Python FHIR specification parser for model class generation. If you've come here because you want Swift or Python classes for FHIR data models, look at our client libraries instead:

This work is licensed under the APACHE license.

Tech

The generate.py script downloads FHIR specification files, parses the profiles (using fhirspec.py) and represents them as FHIRClass instances with FHIRClassProperty properties (found in fhirclass.py). Additionally, FHIRUnitTest (in fhirunittest.py) instances get created that can generate unit tests from provided FHIR examples. These representations are then used by Jinja templates to create classes in certain programming languages, mentioned below.

This script does its job for the most part, but it doesn't yet handle all FHIR pecularities and there's no guarantee the output is correct or complete. Unless you have a desire to understand how parsing works, you should be able to play with Lang/settings.py, Lang/mappings.py and Lang/templates* to achieve what you need.

The master branch is currently on DSTU 2, v1.0.2.
The develop branch is on par with master, but points to the continuous integration build, currently on 1.3.0.

See tags for specific FHIR versions.

Use

  1. Copy the file settings.py from the language's subdirectory into the project's root directory, then

  2. Adjust settings, especially those determining where the generated classes will be copied to, found at the top of settings.py.

  3. Install requirements by running pip3 (or pip):

    pip3 install -r requirements.txt
  4. Run the script:

    ./generate.py

    This will use Python 3, issue python generate.py if you don't have Python 3 yet. Supply the -f flag to force a re-download of the spec.

NOTE that the script currently overwrites existing files without asking and without regret.

Languages

This repo currently supports class generation in the following languages:

Swift

Swift, Apple's new programming language for OS X and iOS. See the Swift-FHIR repo for details.

Python

Classes for Python are targeted towards Python 3 but will support the later 2.x versions – at least they should. All resource classes will inherit from the FHIRResource base class, which is a subclass of FHIRElement. Dates are expressed as FHIRDate instances which can parse valid ISO dates.

The generated classes will use relative imports in the form from . import medication. This avoids namespaces clashes but requires that you use them from within a package. See the SMART Python Client for a setup that works (all model classes are in a models subdirectory).

Python TODO:

[x] Generate Python classes
[x] Deserialize from JSON
[x] Implement reference resolver (for contained resources)
[ ] Implement reference resolver (for remote resources)
[ ] Implement working with references in code
[x] Serialize to JSON
[x] Generate search parameter builder
[x] Generate unit tests from JSON example files

Requirements

The generated Python classes require the following packages to be installed:

isodate

Tech Details

This parser still applies some tricks, stemming from the evolving nature of FHIR's profile definitions. Some tricks may have become obsolete and should be cleaned up.

How are property names determined?

Every “property” of a class, meaning every element in a profile snapshot, is represented as a FHIRStructureDefinitionElement instance. If an element itself defines a class, e.g. Patient.animal, calling the instance's as_properties() method returns a list of FHIRClassProperty instances – usually only one – that indicates a class was found in the profile. The class of this property is derived from element.type, which is expected to only contain one entry, in this matter:

  • If type is BackboneElement, a class name is constructed from the parent element (in this case Patient) and the property name (in this case animal), camel-cased (in this case PatientAnimal).
  • If type is *, a class for all classes found in settings star_expand_types` is created
  • Otherwise, the type is taken as-is (e.g. CodeableConcept) and mapped according to mappings' classmap, which is expected to be a valid FHIR class.

TODO: should http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name be respected?

About

A Python FHIR specification parser and class generator

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 63.1%
  • Swift 36.9%