Skip to content

Commit

Permalink
feat: allow tweaking vocabularies when importing
Browse files Browse the repository at this point in the history
  • Loading branch information
jarno-knaw committed Jan 28, 2025
1 parent 7019391 commit e18ec6c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
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
10 changes: 7 additions & 3 deletions src/graphdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This file contains functions for interacting with GraphDB
"""
import os
import urllib
from typing import TextIO

import requests
Expand Down Expand Up @@ -124,12 +125,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 +144,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
4 changes: 2 additions & 2 deletions src/vocabularies.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ 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:
Expand All @@ -106,7 +106,7 @@ def load_vocabulary(source_data: dict, data_dir: str, graph_name: str) -> None:
: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

0 comments on commit e18ec6c

Please sign in to comment.