Skip to content

Commit

Permalink
Option for ignoring existing annotated properties
Browse files Browse the repository at this point in the history
Allows for annotating an already annotated schema
with a new context.
  • Loading branch information
avillar committed Mar 19, 2024
1 parent e21dda1 commit db6bcee
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions ogc/na/annotate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,15 @@ class SchemaAnnotator:
"""

def __init__(self, schema_resolver: SchemaResolver | None = None,
ref_mapper: Callable[[str, Any], str] | None = None):
ref_mapper: Callable[[str, Any], str] | None = None,
ignore_existing: bool = False):
"""
:schema_resolver: an optional SchemaResolver to resolve references
:ref_mapper: an optional function to map JSON `$ref`'s before resolving them
"""
self.schema_resolver = schema_resolver or SchemaResolver()
self._ref_mapper = ref_mapper
self.ignore_existing = ignore_existing

def process_schema(self, location: Path | str | None,
default_context: str | Path | dict | None = None,
Expand Down Expand Up @@ -516,6 +518,11 @@ def process_properties(obj: dict, context_stack: list[dict[str, Any]],
# skip JSON-LD keywords
continue
prop_value = properties[prop]

for key in list(prop_value.keys()):
if self.ignore_existing and key.startswith(ANNOTATION_PREFIX):
prop_value.pop(key, None)

prop_ctx = find_prop_context(prop, context_stack)
if prop_ctx:
used_terms.add(prop)
Expand Down Expand Up @@ -949,6 +956,12 @@ def _main():
help='Dump visited properties and their ids to a file',
)

parser.add_argument(
'--ignore-existing',
help="Ignore existing x-jsonld- properties when annotating",
action='store_true',
)

args = parser.parse_args()

if not args.schema:
Expand Down Expand Up @@ -976,7 +989,7 @@ def write_visited(stream):
with open(args.dump_visited, 'w', newline='') as f:
write_visited(f)
else:
annotator = SchemaAnnotator()
annotator = SchemaAnnotator(ignore_existing=args.ignore_existing)
annotated = annotator.process_schema(args.schema, args.context)
print(dump_yaml(annotated.schema))

Expand Down

0 comments on commit db6bcee

Please sign in to comment.