-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerateBundles.py
138 lines (121 loc) · 3.38 KB
/
generateBundles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import json
import sys
import getopt
from glob import glob
exampleFolderLocation = r""
outputLocation = r""
baseUrl = ""
recursive = False
httpVerb = ""
arg_help = "{0} -f <folder> -u <url> -o <output> -r <recursive> -v <verb>".format(sys.argv[0])
try:
opts, args = getopt.getopt(sys.argv[1:], "hf:u:o:r:v:", ["help", "folder=",
"url=", "output=", "recursive=, verb="])
except:
print(arg_help)
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print(arg_help) # print the help message
sys.exit(2)
elif opt in ("-f", "--folder"):
exampleFolderLocation = arg
elif opt in ("-u", "--url"):
baseUrl = arg
elif opt in ("-o", "--output"):
outputLocation = arg
elif opt in ("-r", "--recursive"):
recursive = bool(arg)
elif opt in ("-v", "--verb"):
httpVerb = opt
if not outputLocation:
outputLocation = os.getcwd()
if not baseUrl:
baseUrl = "{{FHIR_BASE_URL}}"
if not exampleFolderLocation:
print("Need to provide a directory with sample data")
sys.exit(2)
if not httpVerb:
httpVerb = "PUT"
#build entry list and returns list
def build_entries(resources):
entries = []
#loop through resources, pull id and populate fullUrl and request:url
for x in resources:
loadedJsonDict = json.loads(x)
id = loadedJsonDict["id"]
resourceType = loadedJsonDict["resourceType"]
entry = {
"fullUrl": f"{baseUrl}/{resourceType}/{id}",
"resource": loadedJsonDict,
"request": {
"method": "PUT",
"url": f"{resourceType}/{id}"
}
}
entries.append(entry)
return entries
#loads resource into string and adds to list
def extractFiles(directory):
resources = []
for x in directory:
f = str
if not recursive:
f = open(rf"{exampleFolderLocation}\{x}", encoding="utf8").read()
else:
f = open(rf"{x}", encoding="utf8").read()
#resources.append(json.loads(f))
resources.append(f)
return resources
folder = list[str];
if not recursive:
folder = os.listdir(exampleFolderLocation)
else:
print('Recursive selected: getting files....')
folder = [y for x in os.walk(exampleFolderLocation) for y in glob(os.path.join(x[0], '*.json'))]
resources = extractFiles(folder)
entries = build_entries(resources)
#example entries
"""
{
"fullUrl": "http://example.org/fhir/Patient/123",
"resource": {
"resourceType": "Patient",
"id": "123",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">Some narrative</div>"
},
"active": true,
"name": [
{
"use": "official",
"family": "Chalmers",
"given": [
"Peter",
"James"
]
}
],
"gender": "male",
"birthDate": "1974-12-25"
},
"request": {
"method": "PUT",
"url": "Patient/123"
}
}
"""
requestBody = {
"resourceType": "Bundle",
"id": "bundle-transaction",
"meta": {
"lastUpdated": "2022-12-22T01:43:30Z"
},
"type": "transaction",
"entry": entries
}
newFile = open(outputLocation,'w')
newFile.write(json.dumps(requestBody, indent = 4))
newFile.close()