First crack at improving performance of JSON and YAML formatting in the CLI
# See the License for the specific language governing permissions and
# limitations under the License.
-from jsonpath_rw import parse
+import functools
+import operator
def get_value(doc, key):
+ """
+ Extracts a value from a nested set of dictionaries 'doc' based on
+ a 'key' string.
+ The key string is expected to be of the format 'x.y.z'
+ where each component in the string is a key in a dictionary separated
+ by '.' to denote the next key is in a nested dictionary.
+
+ Returns the extracted value from the key specified (if found)
+ Returns None if the key can not be found
+ """
if not key or not isinstance(doc, dict):
raise ValueError()
- jsonpath_expr = parse(key)
- matches = jsonpath_expr.find(doc)
- value = None if len(matches) < 1 else matches[0].value
- return value
+ split_key = key.split('.')
+ if not split_key:
+ return None
+
+ value = doc
+ for k in split_key:
+ if isinstance(value, dict) and key in value:
+ value = value[key]
+ else:
+ return None
+ return value
def get_kvps(doc, keys):
+ """
+ Extracts one or more keys ('keys' can be a string or list of strings)
+ from the dictionary 'doc'.
+ The key string is expected to be of the format 'x.y.z'
+ where each component in the string is a key in a dictionary separated
+ by '.' to denote the next key is in a nested dictionary.
+
+ Return a subset of 'doc' with only the 'keys' specified as members, all
+ other data in the dictionary will be filtered out.
+ Return an empty dict if no keys are found.
+ """
+ if not isinstance(keys, list):
+ keys = [keys]
+
new_doc = {}
for key in keys:
value = get_value(doc, key)
@@ -38,4 +70,5 @@ def get_kvps(doc, keys):
nested = nested[attr]
key = key[key.index('.') + 1:]
nested[key] = value
+
return new_doc
