• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import fhirspec_pb2
2import sys
3import os
4import json
5from fhir_spec_extractor import FhirSpecExtractor, HC_SUPPORTED_RESOURCE_SET
6from google.protobuf import text_format
7from typing import List, Mapping
8
9_TEXTPROTO_EXT = "textproto"
10_BINARY_PB_EXT = "binarypb"
11
12if __name__ == '__main__':
13    output_file_name = sys.argv[1]
14    input_file_names = sys.argv[2:]
15
16    output_file_extension = output_file_name.split('.')[-1]
17    if output_file_extension != _TEXTPROTO_EXT and output_file_extension != _BINARY_PB_EXT:
18        raise ValueError(f"Output file extension must be '{_TEXTPROTO_EXT}' or '{_BINARY_PB_EXT}'")
19
20    resource_definitions_file_name = None
21    type_definitions_file_name = None
22
23    for path in input_file_names:
24        base_file_name = os.path.basename(path)
25        if base_file_name == "profiles-resources.json":
26            resource_definitions_file_name = path
27        elif base_file_name == "profiles-types.json":
28            type_definitions_file_name = path
29
30    if not resource_definitions_file_name:
31        raise ValueError("Expected profiles-resources.json")
32
33    if not type_definitions_file_name:
34        raise ValueError("Expected profiles-types.json")
35
36    with open(resource_definitions_file_name, 'r') as resources_file, open(
37            type_definitions_file_name, 'r') as types_file:
38        profiles_resources_json = json.load(resources_file)
39        profiles_types_json = json.load(types_file)
40
41        fhirSpecExtractor = FhirSpecExtractor(profiles_resources_json, HC_SUPPORTED_RESOURCE_SET)
42
43        r4_resource_spec = fhirSpecExtractor.generate_r4_fhir_spec_proto_message(
44            profiles_types_json)
45
46    if output_file_extension == _TEXTPROTO_EXT:
47        with open(output_file_name, 'w') as f:
48            f.write(text_format.MessageToString(r4_resource_spec))
49    else:
50        with open(output_file_name, 'wb') as f:
51            f.write(r4_resource_spec.SerializeToString())
52