Skip to content

Commit

Permalink
update unpack
Browse files Browse the repository at this point in the history
  • Loading branch information
srmnitc committed Dec 4, 2023
1 parent 1049ee1 commit 43548d2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
29 changes: 25 additions & 4 deletions pyscal_rdf/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _replace_keys(refdict, indict):

def _setup_structure_store(structure_store):
if structure_store is None:
structure_store = os.path.join(os.getcwd(), 'pyscal_rdf_structure_store')
structure_store = os.path.join(os.getcwd(), 'rdf_structure_store')
if not os.path.exists(structure_store):
os.mkdir(structure_store)
return structure_store
Expand Down Expand Up @@ -832,7 +832,7 @@ def write(self, filename, format="json-ld"):
with open(filename, "w") as fout:
fout.write(self.graph.serialize(format=format))

def publish(self, package_name, format='turtle', compress=True):
def archive(self, package_name, format='turtle', compress=True):
"""
Publish a dataset from graph including per atom quantities
"""
Expand All @@ -844,7 +844,7 @@ def publish(self, package_name, format='turtle', compress=True):
raise ValueError(f'{package_name} tarball already exists')

os.mkdir(package_name)
structure_store = f'{package_name}/{os.path.basename(self.structure_store)}'
structure_store = f'{package_name}/rdf_structure_store'
os.mkdir(structure_store)

#now go through each sample, and copy the file, at the same time fix the paths
Expand All @@ -857,7 +857,7 @@ def publish(self, package_name, format='turtle', compress=True):
self.graph.remove((URIRef(f'{sample}_{val}'), CMSO.hasPath, None))

#assign corrected path
new_relpath = "/".join([os.path.basename(self.structure_store), filepath.split('/')[-1]])
new_relpath = "/".join(['rdf_structure_store', filepath.split('/')[-1]])
self.graph.add((URIRef(f'{sample}_{val}'), CMSO.hasPath, Literal(new_relpath, datatype=XSD.string)))

triple_file = os.path.join(package_name, 'triples')
Expand All @@ -866,6 +866,27 @@ def publish(self, package_name, format='turtle', compress=True):
if compress:
with tarfile.open(f'{package_name}.tar.gz', "w:gz") as tar:
tar.add(package_name, arcname=os.path.basename(package_name))
shutil.rmtree(package_name)


@classmethod
def unarchive(cls, package_name, compress=True,
store="Memory",
store_file=None,
identifier="http://default_graph",
ontology=None):
if compress:
package_base_name = ".".join(package_name.split(".")[:-2])
with tarfile.open(package_name) as fin:
fin.extractall(package_base_name)
os.remove(package_name)
print(package_base_name)
print(f'{package_base_name}/triples')
return cls(store=store, store_file=store_file,
identifier=identifier,
graph_file=f'{package_base_name}/triples',
structure_store=f'{package_base_name}/rdf_structure_store',
ontology=ontology)

def query(self, inquery):
"""
Expand Down
11 changes: 9 additions & 2 deletions pyscal_rdf/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,16 @@ class StructureGraph(RDFGraph):
def __init__(self, graph_file=None,
store="Memory",
store_file=None,
identifier="http://default_graph"):
identifier="http://default_graph",
ontology=None,
structure_store=None):

super().__init__(graph_file=graph_file, store=store, store_file=store_file, identifier=identifier)
super().__init__(graph_file=graph_file,
store=store,
store_file=store_file,
identifier=identifier,
ontology=ontology,
structure_store=structure_store)
self._element_dict = element_dict
self._structure_dict = structure_dict

Expand Down

0 comments on commit 43548d2

Please sign in to comment.