Skip to content

Instantly share code, notes, and snippets.

@johnxx
Last active July 4, 2024 09:56
Show Gist options
  • Save johnxx/8012a0b65c766c7fef4b0f7514785e1f to your computer and use it in GitHub Desktop.
Save johnxx/8012a0b65c766c7fef4b0f7514785e1f to your computer and use it in GitHub Desktop.
repro of collections listing issue
import os
import shutil
from iroh import IrohNode, SetTagOption, WrapOption, AddProgressType
class AddCallback:
hash = None
def progress(self, event):
if event.type() == AddProgressType.ALL_DONE:
all_done = event.as_all_done()
self.hash = all_done.hash
# Create folder
os.mkdir("tmp")
try:
path = os.path.abspath(os.path.join("tmp"))
print("Created dir \"tmp\"")
file_names = ["foo", "bar", "bat"]
# Create three files in the folder
for file_name in file_names:
file_path = os.path.join("tmp", file_name)
with open(file_path, "w") as f:
f.write(f"{file_name}")
print(f"Created file {file_path}")
# Create an Iroh node
node = IrohNode("iroh_data_dir")
# Options
in_place = False
tag = SetTagOption.named(b"my_collection")
wrap = WrapOption.no_wrap()
# Callback setup
callback = AddCallback()
# Import the directory, creating one blob for each file, and one metadata
# blob that stores the file names for each blob
# also creates a 'collection' from the directory, grouping together the
# blobs
node.blobs_add_from_path(path, in_place, tag, wrap, callback)
hash_val = callback.hash
print("Added collection", str(hash_val))
print("collections list:")
coll_res = node.blobs_list_collections()
for res in coll_res:
print(f"\thash: {str(res.hash)} tag: {hash(res.tag)}")
c = node.blobs_get_collection(res.hash)
for ln in c.blobs():
contents = node.blobs_read_to_bytes(ln.link)
print(f"\t\t{ln.name}: {contents}")
for ll in c.links():
contents = node.blobs_read_to_bytes(ll)
print(f"\t\t{str(ll)}: {contents}")
except Exception as e:
print("error: ", e)
# cleanup dir
shutil.rmtree("tmp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment