Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Unified Diff: tools/json_schema_compiler/json_schema.py

Issue 344453003: Docserver: separate models for apps and extensions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/json_schema_compiler/json_schema.py
diff --git a/tools/json_schema_compiler/json_schema.py b/tools/json_schema_compiler/json_schema.py
index ae126c2de911251418b608558d6c5b027e81b42b..bb4e9c4bc5d6f0c9e556ef40cb06cefa23232ad8 100644
--- a/tools/json_schema_compiler/json_schema.py
+++ b/tools/json_schema_compiler/json_schema.py
@@ -6,25 +6,32 @@ import copy
import json_parse
-def DeleteNodes(item, delete_key):
- """Deletes the given nodes in item, recursively, that have |delete_key| as
- an attribute.
+
+def DeleteNodes(item, delete_key=None, matcher=None):
+ """Deletes certain nodes in item, recursively. If |delete_key| is set, all
+ dicts with |delete_key| as an attribute are deleted. If a callback is passed
+ as |matcher|, |DeleteNodes| will delete all dicts for which matcher(dict)
+ returns True.
"""
- def HasKey(thing):
- return json_parse.IsDict(thing) and thing.get(delete_key, False)
+ assert (delete_key is not None) != (matcher is not None)
+
+ def ShouldDelete(thing):
+ return json_parse.IsDict(thing) and (
+ delete_key is not None and delete_key in thing or
+ matcher is not None and matcher(thing))
if json_parse.IsDict(item):
toDelete = []
for key, value in item.items():
- if HasKey(value):
+ if ShouldDelete(value):
toDelete.append(key)
else:
- DeleteNodes(value, delete_key)
+ DeleteNodes(value, delete_key, matcher)
for key in toDelete:
del item[key]
elif type(item) == list:
- item[:] = [DeleteNodes(thing, delete_key)
- for thing in item if not HasKey(thing)]
+ item[:] = [DeleteNodes(thing, delete_key, matcher)
+ for thing in item if not ShouldDelete(thing)]
return item

Powered by Google App Engine
This is Rietveld 408576698