-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathinterface.py
More file actions
112 lines (90 loc) · 3.79 KB
/
Copy pathinterface.py
File metadata and controls
112 lines (90 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""The data space store connects SimPhoNy to a data space."""
import os
import pathlib
from base64 import b64encode
from pathlib import Path
from typing import BinaryIO, Optional
from rdflib import Graph, URIRef
from rdflib.term import Identifier
from simphony_osp.interfaces.interface import Interface
from simphony_osp.interfaces.remote.common import get_hash
class DataspaceInterface(Interface):
"""The data space interface connects SimPhoNy to a data space."""
_identifier: Identifier = URIRef("https://www.simphony-osp.eu/SQLAlchemy")
_database_path: Optional[Path] = None
_files_path: Optional[Path] = None
_uri: Optional[str] = None
# Interface
# ↓ ----- ↓
entity_tracking: bool = False
def open(self, configuration: str, create: bool = False):
"""Open the specified dataspace."""
path = pathlib.Path(configuration).absolute()
if not create:
if not path.is_dir():
raise FileNotFoundError(f"Folder {path} not found")
if not (path / "database.db").is_file():
raise FileNotFoundError(
f'Database {path / "database.db"} ' f"not found."
)
if not (path / "files").is_dir():
raise FileNotFoundError(
f'Folder {path / "files"} ' f"not found."
)
uri = "sqlite:///" + str(path / "database.db")
if self._uri is not None and self._uri != uri:
raise RuntimeError(
f"A different dataspace {self._uri}" f"is already open!"
)
os.makedirs(path, exist_ok=True)
os.makedirs(path / "files", exist_ok=True)
self.base = Graph("SQLAlchemy", identifier=self._identifier)
self.base.open(uri, create=create)
self._uri = uri
self._database_path = path / "database.db"
self._files_path = path / "files"
def close(self):
"""Close the dataspace."""
if self.base is not None:
self.base.close(commit_pending_transaction=False)
self._uri = None
self.base = None
self._database_path = None
self._files_path = None
def commit(self):
"""Commit pending changes to the triple store."""
# The `InterfaceDriver` will simply add the triples to the base graph
# and commit them. Nothing to do here.
pass
def populate(self):
"""The base graph does not need to be populated. Nothing to do."""
pass
def save(self, key: str, file: BinaryIO) -> None:
"""Save a file."""
file_name = b64encode(bytes(key, encoding="UTF-8")).decode("UTF-8")
buf_size = 1024
with open(self._files_path / file_name, "wb") as new_file:
data = True
while data:
data = file.read(buf_size)
new_file.write(data)
def load(self, key: str) -> BinaryIO:
"""Load a file."""
file_name = b64encode(bytes(key, encoding="UTF-8")).decode("UTF-8")
return open(self._files_path / file_name, "rb")
def delete(self, key: str) -> None:
"""Delete a file."""
file_name = b64encode(bytes(key, encoding="UTF-8")).decode("UTF-8")
(self._files_path / file_name).unlink()
def hash(self, key: str) -> str:
"""Hash a file."""
file_name = b64encode(bytes(key, encoding="UTF-8")).decode("UTF-8")
return get_hash(str(self._files_path / file_name))
def rename(self, key: str, new_key: str) -> None:
"""Rename a file."""
file_name = b64encode(bytes(key, encoding="UTF-8")).decode("UTF-8")
new_file_name = b64encode(bytes(new_key, encoding="UTF-8")).decode(
"UTF-8"
)
(self._files_path / file_name).rename(self._files_path / new_file_name)
# ↑ ----- ↑