Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow tweaking vocabularies when importing #8

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions src/graphdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions src/vocabularies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading