Skip to content

Commit

Permalink
Merge pull request #249 from okp4/fix/json-ld-generation
Browse files Browse the repository at this point in the history
Adopt a flatten representation for generated JSON-LD  schema
  • Loading branch information
ccamel authored Mar 5, 2024
2 parents 1ee2ae1 + 454d1ef commit d762bd7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ $(OBJ_ONTS_RDFXML): $(DST_ONT)/%.rdf.xml: $(DST_ONT)/%.ttl
$(OBJ_SCHEMAS_JSONLD): $(DST_SCHEMA)/%.jsonld: $(DST_SCHEMA)/%.ttl
@echo "${COLOR_CYAN}🔨 building${COLOR_RESET} schema ${COLOR_GREEN}$@${COLOR_RESET}"
@mkdir -p -m $(PERMISSION_MODE) $(@D)
@${call CLI,jsonld,convert,$<,-o,$@,--indent,$(JSONLD_INDENT)}
@${call CLI,jsonld,convert,$<,-o,$@,--flatten,--indent,$(JSONLD_INDENT)}

$(OBJ_THESAURI_JSONLD): $(DST_THESAURUS)/%.jsonld: $(DST_THESAURUS)/%.ttl
@echo "${COLOR_CYAN}🔨 building${COLOR_RESET} thesaurus ${COLOR_GREEN}$@${COLOR_RESET}"
Expand Down Expand Up @@ -227,7 +227,7 @@ $(BIN_OKP4_RDFXML): $(BIN_OKP4_NT)
$(BIN_OKP4_JSONLD): $(BIN_OKP4_NT)
@echo "${COLOR_CYAN}📦 making${COLOR_RESET} ontology ${COLOR_GREEN}$@${COLOR_RESET}"
@touch $@
@${call CLI,jsonld,convert,$<,-o,$@,--indent,$(JSONLD_INDENT)}
@${call CLI,jsonld,convert,$<,-o,$@,--flatten,--indent,$(JSONLD_INDENT)}

$(BIN_DOC_SCHEMAS): $(OBJ_ONTS_TTL) $(OBJ_EXAMPLES) $(shell find $(SRC_SCRIPT) -name "*.*") Makefile
@echo "${COLOR_CYAN}📝 generating ${COLOR_GREEN}schemas ${COLOR_RESET}documentation"
Expand Down
10 changes: 8 additions & 2 deletions script/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,20 @@ def generate(input_path: os.PathLike[str], output_path: os.PathLike[str],
default=None,
help="The number of spaces to use for indentation.",
)
def convert(input_file: t.TextIO, output_file: t.TextIO, indent: t.Optional[int], format: t.Optional[str]) -> None:
@click.option(
"--flatten",
"flatten",
is_flag=True,
help="Produces a flattened JSON-LD representation.",
)
def convert(input_file: t.TextIO, output_file: t.TextIO, flatten: bool, indent: t.Optional[int], format: t.Optional[str]) -> None:
"""Converts a schema to JSON-LD.
If the output file is not specified, the JSON-LD will be printed to the console.
You can specify the format of the input file, if it cannot be inferred from the file extension.
"""
try:
convert_jsonld(input_file, output_file, indent, format)
convert_jsonld(input_file, output_file, flatten, indent, format)
except Exception as e:
raise click.UsageError(f"{e}")

Expand Down
4 changes: 2 additions & 2 deletions script/cli/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
SCHEMA = Namespace("http://schema.org/")


def convert_jsonld(input_file: t.TextIO, output_file: t.TextIO, indent: t.Optional[int] = None,
def convert_jsonld(input_file: t.TextIO, output_file: t.TextIO, flatten: bool, indent: t.Optional[int] = None,
format: t.Optional[str] = None):
"""Converts a schema to JSON-LD."""
context = empty_context()
Expand All @@ -30,7 +30,7 @@ def convert_jsonld(input_file: t.TextIO, output_file: t.TextIO, indent: t.Option
is_a_type = (isinstance(range, term.Identifier)) and ((range == XSD.anyURI) or not range.startswith(str(XSD)))

domains: list[Node | None] = list(g.objects(s, SCHEMA.domainIncludes))
if not domains:
if not domains or flatten:
domains = [None]

for domain in domains:
Expand Down

0 comments on commit d762bd7

Please sign in to comment.