Skip to content

Commit

Permalink
Merge pull request #8 from knaw-huc/feature/allow-tweaking-vocabs
Browse files Browse the repository at this point in the history
feat: allow tweaking vocabularies when importing
jarno-knaw authored Jan 28, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 7019391 + 91e4c4d commit 454359e
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions entrypoint.py
Original file line number Diff line number Diff line change
@@ -75,6 +75,9 @@ def main() -> None:
if reload:
print(f"Loading vocabulary {vocab}")
load_vocabulary(vocab_config['source'], data, graph)
if "tweaks" in vocab_config:
print(f"Tweaks found for {vocab}. Loading")
load_vocabulary(vocab_config['tweaks'], data, graph, True)
if graph in loaded_vocabs:
update_timestamp(graph, int(time.time()))
else:
9 changes: 6 additions & 3 deletions src/graphdb.py
Original file line number Diff line number Diff line change
@@ -124,12 +124,13 @@ def get_type(extension: str) -> str:
return "application/x-turtle"


def add_vocabulary(graph: TextIO, graph_name: str, extension: str) -> None:
def add_vocabulary(graph: TextIO, graph_name: str, extension: str, append: bool = False) -> None:
"""
Add a vocabulary to GraphDB
:param graph: File
:param graph_name: String representing the name of the graph
:param extension: String representing the extension
:param extension: String representing the extension
:param append: Append data instead of replacing
:return:
"""
print(f"Adding vocabulary {graph_name}")
@@ -142,7 +143,9 @@ def add_vocabulary(graph: TextIO, graph_name: str, extension: str) -> None:
headers = {
'Content-Type': get_type(extension),
}
response = requests.put(
method = requests.post if append else requests.put

response = method(
f"{endpoint}/statements",
data=content,
headers=headers,
12 changes: 7 additions & 5 deletions src/vocabularies.py
Original file line number Diff line number Diff line change
@@ -97,16 +97,18 @@ def get_file_from_config(config_data: dict, data_dir: str) -> TextIO:
raise InvalidConfigurationException("Unknown type")


def load_vocabulary(source_data: dict, data_dir: str, graph_name: str) -> None:
def load_vocabulary(source_data: dict, data_dir: str,
graph_name: str, append: bool = False) -> None:
"""
Load a vocabulary using the source data from the yaml.
:param source_data:
:param data_dir:
:param graph_name:
:param source_data: Dict containing the information where to find the vocab.
:param data_dir: Dir containing local files.
:param graph_name: The name of the graph to put the vocabulary into.
:param append: Boolean, when true this doesn't overwrite the graph but appends.
:return:
"""
with get_file_from_config(source_data, data_dir) as vocab_file:
add_vocabulary(vocab_file, graph_name, get_vocab_format(source_data))
add_vocabulary(vocab_file, graph_name, get_vocab_format(source_data), append)


def get_graph(fp: IO) -> str:

0 comments on commit 454359e

Please sign in to comment.