Skip to content

Commit

Permalink
Updated code to use local json storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jvsoest committed Jun 9, 2024
1 parent 520f47f commit dbeb1c0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 25 deletions.
57 changes: 57 additions & 0 deletions src/persistance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os, json

class Persistance:
def __init__(self):
print("Initializing persistance")

class FilePersistance(Persistance):
def __init__(self, folder_location: str, title_uri: str, base_url='http://localhost/template-instances'):
Persistance.__init__(self)
self.__folder_location = folder_location
self.__title_uri = title_uri
self.__base_url = base_url
self.__cached_items = { }
self.__create_folder_if_not_exists()
self.__scan_folder(action=self.__parse_jsonld_file)

def __create_folder_if_not_exists(self):
"""
Create the persistance folder if it does not exist.
"""
if not os.path.exists(self.__folder_location):
os.makedirs(self.__folder_location)

def __scan_folder(self, action):
"""
Loop over folders and detect files which are .jsonld files. In that specific case, execute the passed function
"""
self.__cached_items = { }
for root, dirs, files in os.walk(self.__folder_location, topdown=True):
for myFile in files:
if myFile.endswith(".jsonld"):
action(filename=os.path.join(root, myFile))

def __parse_jsonld_file(self, filename: str):
"""
Read JSON-LD object from filename, and parse the title and identifier of the object.
input:
- filename: the path of the file to parse
"""
with open(filename, 'r') as f:
metadata = json.load(f)
id = metadata["@id"].replace(self.__base_url + "/", "")
titleTag = None
print(metadata["@context"])
for key in metadata["@context"]:
value = metadata["@context"][key]
if value == self.__title_uri:
titleTag = key

self.__cached_items[id] = {
"title": metadata[titleTag]['@value'],
"filename": filename,
"time": metadata['pav:createdOn']
}

def list_instances(self):
return self.__cached_items.copy()
34 changes: 14 additions & 20 deletions src/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import datetime
from tzlocal import get_localzone
from endpoint_service import SPARQLEndpoint
from persistance import Persistance, FilePersistance

app = Flask(__name__)
CORS(app)
Expand All @@ -37,22 +38,15 @@ def loadConfig(pathString):
logging.error("Could not find config.yaml file. System will exit")
sys.exit(-1)

# Create storage folder
if not os.path.exists(config['server']['storageFolder']):
os.makedirs(config['server']['storageFolder'])
persistance = FilePersistance(folder_location=config['server']['storageFolder'],
title_uri=config['template']['title_predicate'],
base_url=config['template']['instance_base_url'])

sparqlEndpoint = SPARQLEndpoint(config["server"]["server_url"], config["server"]["repository_name"], update_endpoint_suffix=config["server"]["update_endpoint_suffix"])
# sparqlEndpoint = SPARQLEndpoint(config["server"]["server_url"], config["server"]["repository_name"], update_endpoint_suffix=config["server"]["update_endpoint_suffix"])

@app.route("/")
def index():
instances = sparqlEndpoint.list_instances(titlePredicate=config["template"]["title_predicate"])
for idx, val in enumerate(instances):
if "title" not in instances[idx]:
instances[idx]["title"] = {
"value": instances[idx]["instance"]["value"].replace(config["template"]["instance_base_url"] + "/", ""),
"type": "literal"
}

instances = persistance.list_instances()

if config["template"]["storage"]=="cedar":
return render_template("index.html", instances=instances, template_id=config["template"]["templateId"])
Expand Down Expand Up @@ -98,8 +92,8 @@ def delete_instance():
@app.route("/instance")
def showInstance():
identifier = request.args.get("uri")
properties = sparqlEndpoint.describe_instance(identifier)
references = sparqlEndpoint.get_instance_links(identifier)
# properties = sparqlEndpoint.describe_instance(identifier)
# references = sparqlEndpoint.get_instance_links(identifier)
return render_template("instance.html", properties=properties, references=references, instance_uri=identifier)

@app.route("/api/cedar/template.json")
Expand Down Expand Up @@ -175,7 +169,7 @@ def store():
data_to_store_meta["schema:isBasedOn"] = data_to_store_info["isBasedOn"]
data_to_store_meta["pav:createdOn"] = data_to_store_info["createdOn"]
fileNameJson = data_to_store_info["fileName"]
fileNameTurtle = fileNameJson.replace(".jsonld", ".ttl")
# fileNameTurtle = fileNameJson.replace(".jsonld", ".ttl")
else:
print("new profile")
data_to_store_meta["schema:isBasedOn"] = template['@id']
Expand All @@ -186,12 +180,12 @@ def store():
with open(fileNameJson, "w") as f:
json.dump(data_to_store_meta, f, indent=4)

g = Graph()
g.parse(data=json.dumps(data_to_store_meta), format='json-ld')
g.serialize(destination=fileNameTurtle)
# g = Graph()
# g.parse(data=json.dumps(data_to_store_meta), format='json-ld')
# g.serialize(destination=fileNameTurtle)

turtleData = g.serialize(format='nt')
sparqlEndpoint.store_instance(turtleData, data_to_store_meta["@id"])
# turtleData = g.serialize(format='nt')
# sparqlEndpoint.store_instance(turtleData, data_to_store_meta["@id"])

return {"id": f"{session_id}", "message": "Hi there!"}

Expand Down
10 changes: 5 additions & 5 deletions src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ <h1>Filled Templates</h1>
</tr>
</thead>
<tbody>
{%for instance in instances %}
{%for key in instances %}
<tr>
<td><a href="./instance?uri={{instance['instance']['value']}}">{{instance["title"]["value"]}}</a></td>
<td><time datetime="{{instance['time']['value']}}"/></td>
<td><a href="./instance?uri={{ key }}">{{instances[key]["title"]}}</a></td>
<td><time datetime="{{instances[key]['time']}}"/></td>
{% if template_id is defined %}
<td><a href="https://cedar.metadatacenter.org/instances/edit/{{instance['instance']['value']}}" role="button" class="btn btn-secondary float-right">Edit</a></td>
<td><a href="https://cedar.metadatacenter.org/instances/edit/{{ key }}" role="button" class="btn btn-secondary float-right">Edit</a></td>
{% else %}
<td><a href="./edit?uri={{instance['instance']['value']}}" role="button" class="btn btn-secondary float-right">Edit</a></td>
<td><a href="./edit?uri={{ key }}" role="button" class="btn btn-secondary float-right">Edit</a></td>
{% endif %}
</tr>
{%endfor%}
Expand Down

0 comments on commit dbeb1c0

Please sign in to comment.