forked from smart-on-fhir/fhir-parser
-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
219 lines (173 loc) · 6.41 KB
/
utils.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import configparser
import os
import pathlib
import sys
import typing
from subprocess import check_call, CalledProcessError
from fhirspec import FHIRSpecWriter
import fhirrenderer
__author__ = "Md Nazrul Islam <[email protected]>"
INIT_TPL = (
"""
from __future__ import annotations as _annotations
from functools import lru_cache
from typing import TYPE_CHECKING, cast
from fhir_core.fhirabstractmodel import FHIRAbstractModel
__author__ = "Md Nazrul Islam"
__email__ = "[email protected]"
__fhir_version__ = "{0}"
@lru_cache(maxsize=None, typed=True)
def get_fhir_model_class(model_name: str) -> type[FHIRAbstractModel]:
"""
"""
from . import fhirtypes as ft
try:
model_type = getattr(ft, model_name + "Type")
if TYPE_CHECKING:
from fhir_core.types import FhirBase
model_type = cast(type[FhirBase], model_type)
return model_type.get_model_klass()
except AttributeError:
raise ValueError(model_name + " is not a valid FHIR Model")
"""
)
TEST_INIT_TPL = """
__author__ = "Md Nazrul Islam"
__email__ = "[email protected]"
__fhir_version__ = "{0}"
"""
def ensure_init_py(settings, version_info):
""" """
init_tpl = INIT_TPL.format(version_info.version, "element_type")
test_init_tpl = TEST_INIT_TPL.format(version_info.version, "element_type")
for file_location, tpl in [
(settings.RESOURCE_TARGET_DIRECTORY, init_tpl),
(settings.UNITTEST_TARGET_DIRECTORY, test_init_tpl),
]:
if (file_location / "__init__.py").exists():
lines = list()
has_fhir_version = False
with open((file_location / "__init__.py"), "r") as fp:
for line in fp:
if "__fhir_version__" in line:
has_fhir_version = True
parts = list()
parts.append(line.split("=")[0])
parts.append('"{0}"'.format(version_info.version))
line = "= ".join(parts)
lines.append(line.rstrip("\n"))
if not has_fhir_version:
lines.append('__fhir_version__ = "{0}"'.format(version_info.version))
txt = "\n".join(lines)
else:
txt = tpl
with open((file_location / "__init__.py"), "w") as fp:
fp.write(txt)
def update_pytest_fixture(settings):
""" """
lines = list()
fixture_file = settings.RESOURCE_TARGET_DIRECTORY / "tests" / "fixtures.py"
with open(str(fixture_file), "r", encoding="utf-8") as fp:
for line in fp:
if "ROOT_PATH =" in line:
parts = list()
parts.append(line.split("=")[0])
parts.append(
"dirname(dirname(dirname(dirname(dirname(os.path.abspath(__file__))))))\n"
)
line = "= ".join(parts)
elif "CACHE_PATH =" in line:
parts = list()
parts.append(line.split("=")[0])
parts.append(
f"os.path.join(ROOT_PATH, '.cache', '{settings.CURRENT_RELEASE_NAME}')\n"
)
line = "= ".join(parts)
lines.append(line)
# let's write
fixture_file.write_text("".join(lines))
with open(
str(settings.RESOURCE_TARGET_DIRECTORY / "tests" / "conftest.py"),
"w",
encoding="utf-8",
) as fp:
fp.write(
"# -*- coding: utf-8 _*_\n"
f"pytest_plugins = ['fhir.resources.{settings.CURRENT_RELEASE_NAME}.tests.fixtures']\n"
)
def get_cached_version_info(spec_source):
""" """
if not spec_source.exists():
return
version_file = spec_source / "version.info"
if not version_file.exists():
return
config = configparser.ConfigParser()
with open(str(version_file), "r") as fp:
txt = fp.read()
config.read_string("\n".join(txt.split("\n")[1:]))
return config["FHIR"]["version"], config["FHIR"]["fhirversion"]
def parse_path(path_str: str) -> pathlib.Path:
"""Path normalizer"""
if path_str.startswith("~"):
return pathlib.Path(os.path.expanduser(path_str))
if len(path_str) == 1 and path_str == ".":
path_str = os.getcwd()
elif path_str.startswith("." + os.sep):
path_str = os.getcwd() + path_str[1:]
if path_str.endswith(os.sep):
path_str = path_str[: -len(os.sep)]
return pathlib.Path(path_str)
class ResourceWriter(FHIRSpecWriter):
def write(self):
""" """
if self.settings.WRITE_RESOURCES:
renderer = fhirrenderer.FHIRStructureDefinitionRenderer(
self.spec, self.settings
)
renderer.render()
vsrenderer = fhirrenderer.FHIRValueSetRenderer(self.spec, self.settings)
vsrenderer.render()
if self.settings.WRITE_DEPENDENCIES:
renderer = fhirrenderer.FHIRDependencyRenderer(self.spec, self.settings)
renderer.render()
if self.settings.WRITE_UNITTESTS:
renderer = fhirrenderer.FHIRUnitTestRenderer(self.spec, self.settings)
renderer.render()
class FhirPathExpressionParserWriter:
output_dir: pathlib.Path = None
grammar_path: pathlib.Path = None
antlr4_executable: str = "antlr4"
antlr4_version: str = None
def __init__(
self, output_dir=typing.Union[pathlib.Path, str], antlr4_version: str = "4.9.3"
):
""" """
if isinstance(output_dir, str):
self.output_dir = parse_path(output_dir)
elif isinstance(output_dir, pathlib.Path):
self.output_dir = output_dir
self.grammar_path = (
pathlib.Path(os.path.abspath(__file__)).parent / "FHIRPathExpression.g4"
)
self.antlr4_version = antlr4_version
def write(self):
""" """
options = [
str(self.antlr4_executable),
"-v",
self.antlr4_version,
"-o",
str(self.output_dir),
"-Dlanguage=Python3",
str(self.grammar_path),
]
sys.stdout.write(f"Start executing command '{options}'\n")
try:
check_call(options)
sys.stdout.write(f"Files are written at {self.output_dir}" + "\n")
return 0
except CalledProcessError as exc:
sys.stderr.write("Cannot write!\n")
sys.stderr.write(str(exc) + "\n")
return 1