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

[MRG] switch sbt.zip over to default SBT storage #1174

Merged
merged 8 commits into from
Nov 1, 2020
Merged
45 changes: 26 additions & 19 deletions sourmash/sbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,23 +562,23 @@ def save(self, path, storage=None, sparseness=0.0, structure_only=False):
info["index_type"] = self.__class__.__name__ # TODO: check

# choose between ZipStorage and FS (file system/directory) storage.
if path.endswith(".sbt.zip"):
if not path.endswith(".sbt.json"):
kind = "Zip"
if not path.endswith('.sbt.zip'):
path += '.sbt.zip'
storage = ZipStorage(path)
backend = "FSStorage"
name = os.path.basename(path[:-8])
subdir = '.sbt.{}'.format(name)
storage_args = FSStorage("", subdir).init_args()
storage.save(subdir + "/", b"")
index_filename = os.path.abspath(path)
else:
else: # path.endswith('.sbt.json')
assert path.endswith('.sbt.json')
kind = "FS"
name = os.path.basename(path)
if path.endswith('.sbt.json'):
name = name[:-9]
index_filename = os.path.abspath(path)
else:
index_filename = os.path.abspath(path + '.sbt.json')
name = name[:-9]
index_filename = os.path.abspath(path)

if storage is None:
# default storage
Expand Down Expand Up @@ -685,22 +685,29 @@ def load(cls, location, *, leaf_loader=None, storage=None, print_version_warning
sbt_name = None
tree_data = None

if storage is None and ZipStorage.can_open(location):
storage = ZipStorage(location)

sbts = storage.list_sbts()
if len(sbts) != 1:
print("no SBT, or too many SBTs!")
if storage is None:
if ZipStorage.can_open(location):
storage = ZipStorage(location)
else:
tree_data = storage.load(sbts[0])
if not location.endswith('.sbt.zip'):
location2 = location + '.sbt.zip'
if ZipStorage.can_open(location2):
storage = ZipStorage(location2)

if storage:
sbts = storage.list_sbts()
if len(sbts) != 1:
print("no SBT, or too many SBTs!")
else:
tree_data = storage.load(sbts[0])

tempfile = NamedTemporaryFile()
tempfile = NamedTemporaryFile()

tempfile.write(tree_data)
tempfile.flush()
tempfile.write(tree_data)
tempfile.flush()

dirname = os.path.dirname(tempfile.name)
sbt_name = os.path.basename(tempfile.name)
dirname = os.path.dirname(tempfile.name)
sbt_name = os.path.basename(tempfile.name)

if sbt_name is None:
dirname = os.path.dirname(os.path.abspath(location))
Expand Down
16 changes: 8 additions & 8 deletions tests/test_sbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ def test_sbt_fsstorage():
print(*old_result, sep='\n')

with FSStorage(location, '.fstree') as storage:
tree.save(os.path.join(location, 'tree'), storage=storage)
tree.save(os.path.join(location, 'tree.sbt.json'), storage=storage)

tree = SBT.load(os.path.join(location, 'tree'), leaf_loader=SigLeaf.load)
tree = SBT.load(os.path.join(location, 'tree.sbt.json'), leaf_loader=SigLeaf.load)
print('*' * 60)
print("{}:".format(to_search.metadata))
new_result = {str(s) for s in tree.find(search_minhashes,
Expand Down Expand Up @@ -352,10 +352,10 @@ def test_sbt_zipstorage(tmpdir):
print(*old_result, sep='\n')

with ZipStorage(str(tmpdir.join("tree.sbt.zip"))) as storage:
tree.save(str(tmpdir.join("tree")), storage=storage)
tree.save(str(tmpdir.join("tree.sbt.json")), storage=storage)

with ZipStorage(str(tmpdir.join("tree.sbt.zip"))) as storage:
tree = SBT.load(str(tmpdir.join("tree")),
tree = SBT.load(str(tmpdir.join("tree.sbt.json")),
leaf_loader=SigLeaf.load,
storage=storage)

Expand Down Expand Up @@ -390,12 +390,12 @@ def test_sbt_ipfsstorage():

try:
with IPFSStorage() as storage:
tree.save(os.path.join(location, 'tree'), storage=storage)
tree.save(os.path.join(location, 'tree.sbt.json'), storage=storage)
except ipfshttpclient.exceptions.ConnectionError:
pytest.xfail("ipfs not installed/functioning probably")

with IPFSStorage() as storage:
tree = SBT.load(os.path.join(location, 'tree'),
tree = SBT.load(os.path.join(location, 'tree.sbt.json'),
leaf_loader=SigLeaf.load,
storage=storage)

Expand Down Expand Up @@ -429,12 +429,12 @@ def test_sbt_redisstorage():

try:
with RedisStorage() as storage:
tree.save(os.path.join(location, 'tree'), storage=storage)
tree.save(os.path.join(location, 'tree.sbt.json'), storage=storage)
except redis.exceptions.ConnectionError:
pytest.xfail("Couldn't connect to redis server")

with RedisStorage() as storage:
tree = SBT.load(os.path.join(location, 'tree'),
tree = SBT.load(os.path.join(location, 'tree.sbt.json'),
leaf_loader=SigLeaf.load,
storage=storage)

Expand Down
Loading