Skip to content

Commit 3a21886

Browse files
committed
Auto-update schema cache
1 parent 6bbe665 commit 3a21886

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

emod_api/schema_to_class.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections import OrderedDict
55

66
schema_cache = None
7+
_schema_path = None
78

89

910
class ReadOnlyDict(OrderedDict):
@@ -207,26 +208,28 @@ def get_class_with_defaults(classname, schema_path=None, schema_json=None, show_
207208

208209
def get_schema(schema_path=None, schema_json=None):
209210
global schema_cache
211+
global _schema_path
210212
schema_ret = None
211213

212214
# Prefer schema-as-dict if provided
213215
if schema_json:
214-
schema_cache = schema_json
215216
schema_ret = schema_json
216-
# Check cache
217-
elif schema_cache:
218-
schema_ret = schema_cache
219217
# Then check file path
220218
elif schema_path:
221219
if not os.path.exists(schema_path):
222220
raise ValueError(f"ERROR: No file found at {schema_path}. "
223221
f"A valid schema path needs to exist at the path specified.")
224-
with open(schema_path) as file:
225-
schema_val = json.load(file)
226-
schema_cache = schema_val
227-
schema_ret = schema_val
222+
223+
if schema_cache is None or _schema_path != schema_path:
224+
with open(schema_path) as file:
225+
schema_val = json.load(file)
226+
schema_cache = schema_val
227+
schema_ret = schema_val
228+
_schema_path = schema_path
229+
else:
230+
schema_ret = schema_cache
228231
else:
229-
raise ValueError("A valid schema path needs to be specified.")
232+
raise ValueError("A valid schema_path or schema_json needs to be specified.")
230233

231234
return schema_ret
232235

0 commit comments

Comments
 (0)