From b60d064e281243088d3dd63fcb99478344b2a8de Mon Sep 17 00:00:00 2001 From: Peter Amstutz Date: Fri, 23 Mar 2018 10:34:29 -0400 Subject: [PATCH] Add cookbook examples for uploading and downloading a file from a Collection in Python, closes #13253 Arvados-DCO-1.1-Signed-off-by: Peter Amstutz --- doc/sdk/python/cookbook.html.textile.liquid | 42 +++++++++++++++++++++ sdk/python/arvados/collection.py | 6 +++ 2 files changed, 48 insertions(+) diff --git a/doc/sdk/python/cookbook.html.textile.liquid b/doc/sdk/python/cookbook.html.textile.liquid index a046654a64..4a6c453cae 100644 --- a/doc/sdk/python/cookbook.html.textile.liquid +++ b/doc/sdk/python/cookbook.html.textile.liquid @@ -164,3 +164,45 @@ for u in collection_uuids: newcol = arvados.collection.Collection(combined_manifest) newcol.save_new(name="My combined collection", owner_uuid=project_uuid) {% endcodeblock %} + +h2. Upload a file into a new collection + +{% codeblock as python %} +import arvados +import arvados.collection + +project_uuid = "qr1hi-j7d0g-zzzzzzzzzzzzzzz" +collection_name = "My collection" +filename = "file1.txt" + +api = arvados.api() +c = arvados.collection.Collection() +with open(filename, "rb") as reader: + with c.open(filename, "wb") as writer: + content = reader.read(128*1024) + while content: + writer.write(content) + content = reader.read(128*1024) +c.save_new(name=collection_name, owner_uuid=project_uuid) +print("Saved %s to %s" % (collection_name, c.manifest_locator())) +{% endcodeblock %} + +h2. Download a file from a collection + +{% codeblock as python %} +import arvados +import arvados.collection + +collection_uuid = "qr1hi-4zz18-zzzzzzzzzzzzzzz" +filename = "file1.txt" + +api = arvados.api() +c = arvados.collection.CollectionReader(collection_uuid) +with c.open(filename, "rb") as reader: + with open(filename, "wb") as writer: + content = reader.read(128*1024) + while content: + writer.write(content) + content = reader.read(128*1024) +print("Finished downloading %s" % filename) +{% endcodeblock %} diff --git a/sdk/python/arvados/collection.py b/sdk/python/arvados/collection.py index 33333ee865..8fb90c9443 100644 --- a/sdk/python/arvados/collection.py +++ b/sdk/python/arvados/collection.py @@ -34,6 +34,8 @@ from arvados.retry import retry_method _logger = logging.getLogger('arvados.collection') class CollectionBase(object): + """Abstract base class for Collection classes.""" + def __enter__(self): return self @@ -91,6 +93,8 @@ class _WriterFile(_FileLikeObjectBase): class CollectionWriter(CollectionBase): + """Deprecated, use Collection instead.""" + def __init__(self, api_client=None, num_retries=0, replication=None): """Instantiate a CollectionWriter. @@ -396,6 +400,8 @@ class CollectionWriter(CollectionBase): class ResumableCollectionWriter(CollectionWriter): + """Deprecated, use Collection instead.""" + STATE_PROPS = ['_current_stream_files', '_current_stream_length', '_current_stream_locators', '_current_stream_name', '_current_file_name', '_current_file_pos', '_close_file', -- 2.30.2