Skip to content

Commit e39d1e1

Browse files
committed
clarify naming, add docstrings
1 parent afa9cbd commit e39d1e1

File tree

1 file changed

+65
-8
lines changed

1 file changed

+65
-8
lines changed

asdf/_node_info.py

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,78 @@ def _filter_tree(info, filters):
2020
return len(info.children) > 0 or all(f(info.node, info.identifier) for f in filters)
2121

2222

23-
def _get_matching_schema_property(schema, key):
23+
def _get_matching_schema_property(schema, property_name):
24+
"""
25+
Extract a property subschema for a given property_name.
26+
This function does not descend into the schema (beyond
27+
looking for a "properties" key) and does not support
28+
schema combiners.
29+
30+
Parameters
31+
----------
32+
schema : dict
33+
A dictionary containing a JSONSCHEMA
34+
property_name : str
35+
The name of the property to extract
36+
37+
Returns
38+
-------
39+
dict or None
40+
The property subschema at the provided name or
41+
``None`` if the property doesn't exist.
42+
"""
2443
if "properties" in schema:
2544
props = schema["properties"]
26-
if key in props:
27-
return props[key]
45+
if property_name in props:
46+
return props[property_name]
2847
if "patternProperties" in props:
2948
patterns = props["patternProperties"]
3049
for regex in patterns:
31-
if re.search(regex, key):
50+
if re.search(regex, property_name):
3251
return patterns[regex]
3352
return None
3453

3554

36-
def _get_subschema_for_property(schema, key):
55+
def _get_subschema_for_property(schema, property_name):
56+
"""
57+
Extract a property subschema for a given property_name.
58+
This function will attempt to consider schema combiners
59+
and will return None on an ambiguous result.
60+
61+
Parameters
62+
----------
63+
schema : dict
64+
A dictionary containing a JSONSCHEMA
65+
property_name : str
66+
The name of the property to extract
67+
68+
Returns
69+
-------
70+
dict or None
71+
The property subschema at the provided name or
72+
``None`` if the property doesn't exist or is
73+
ambiguous (has more than one subschema or is nested in a not).
74+
"""
3775
# This does NOT handle $ref the expectation is that the schema
3876
# is loaded with resolve_references=True
3977
applicable = []
4078

4179
# first check properties and patternProperties
42-
subschema = _get_matching_schema_property(schema, key)
80+
subschema = _get_matching_schema_property(schema, property_name)
4381
if subschema is not None:
4482
applicable.append(subschema)
4583

4684
# next handle schema combiners
4785
if "not" in schema:
48-
subschema = _get_subschema_for_property(schema["not"], key)
86+
subschema = _get_subschema_for_property(schema["not"], property_name)
4987
if subschema is not None:
5088
# We can't resolve a valid subschema under a "not" since
5189
# we'd have to know how to invert a schema
5290
return None
5391

5492
for combiner in ("allOf", "oneOf", "anyOf"):
5593
for combined_schema in schema.get(combiner, []):
56-
subschema = _get_subschema_for_property(combined_schema, key)
94+
subschema = _get_subschema_for_property(combined_schema, property_name)
5795
if subschema is not None:
5896
applicable.append(subschema)
5997

@@ -64,6 +102,25 @@ def _get_subschema_for_property(schema, key):
64102

65103

66104
def _get_schema_key(schema, key):
105+
"""
106+
Extract a subschema at a given key.
107+
This function will attempt to consider schema combiners
108+
(allOf, oneOf, anyOf) and will return None on an
109+
ambiguous result (where more than 1 match is found).
110+
111+
Parameters
112+
----------
113+
schema : dict
114+
A dictionary containing a JSONSCHEMA
115+
key : str
116+
The key under which the subschema is stored
117+
118+
Returns
119+
-------
120+
dict or None
121+
The subschema at the provided key or
122+
``None`` if the key doesn't exist or is ambiguous.
123+
"""
67124
applicable = []
68125
if key in schema:
69126
applicable.append(schema[key])

0 commit comments

Comments
 (0)