Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero committed Apr 8, 2024
1 parent 77ddefb commit 65f4d75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
30 changes: 18 additions & 12 deletions lean/commands/cloud/object_store/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from uuid import uuid4
from os import path, getcwd, unlink, mkdir

from click import command, argument
from click import command, option
from lean.click import LeanCommand
from lean.container import container


@command(cls=LeanCommand)
@argument("key", type=str, multiple=True, help=f"The desired key to fetch")
@argument("destination_folder", type=str, default="",
@option("--key", type=str, multiple=True, help=f"The desired key to fetch")
@option("--destination_folder", type=str, default="",
help=f"The destination folder to download the object store values,"
f" if not provided will use to current directory")
def get(key: [str], destination_folder: str):
Expand All @@ -30,24 +32,28 @@ def get(key: [str], destination_folder: str):
api_client = container.api_client
logger = container.logger

logger.debug(f"Fetch object store download url")
url = api_client.object_store.get(key, organization_id)
logger.info(f"Fetching object store download url")
url = api_client.object_store.get(key, organization_id, logger)

logger.debug(f"Start downloading: {url}")
progress = logger.progress(suffix="{task.percentage:0.0f}% ({task.completed:,.0f}/{task.total:,.0f})")
progress_task = progress.add_task("", total=1)

from uuid import uuid4
temp_file = f"{str(uuid4())}.zip"
api_client.data.download_url(url, temp_file, lambda advance: progress.update(progress_task, advance=advance))

if not destination_folder:
from os import getcwd
destination_folder = getcwd()

logger.debug(f"Unzipping object store keys values into: '{destination_folder}'")
if not path.exists(destination_folder):
mkdir(destination_folder)

temp_file = path.join(destination_folder, f"{str(uuid4())}.zip")
logger.info(f"Start downloading: {url} into {temp_file}")
api_client.data.download_url(url, temp_file, lambda advance: progress.update(progress_task, advance=advance))

logger.info(f"Unzipping object store keys values into: '{destination_folder}'")
from zipfile import ZipFile
with ZipFile(temp_file, 'r') as zip_ref:
zip_ref.extractall(destination_folder)

logger.info(f"Deleting temp file: '{temp_file}'")
unlink(temp_file)


2 changes: 1 addition & 1 deletion lean/commands/cloud/object_store/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@command(cls=LeanCommand)
@argument("key", type=str)
def properties(key: str) -> str:
def properties(key: str):
"""
Get a value properties from the organization's cloud object store.
Expand Down
6 changes: 4 additions & 2 deletions lean/components/api/object_store_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# limitations under the License.

from lean.components.api.api_client import *

from lean.components.util.logger import Logger

class ObjectStoreClient:
"""The ObjectStoreClient class contains methods to interact with object/* API endpoints."""
Expand All @@ -38,11 +38,12 @@ def properties(self, key: str, organization_id: str) -> str:

return self._api.post("object/properties", payload)

def get(self, keys: [str], organization_id: str) -> str:
def get(self, keys: [str], organization_id: str, logger: Logger) -> str:
"""Will fetch an url to download the requested keys
:param keys: keys of the object to retrieve
:param organization_id: the id of the organization who's object store to retrieve from
:param logger: the logger instance to use
:return: the url to download the requested keys
"""

Expand All @@ -58,6 +59,7 @@ def get(self, keys: [str], organization_id: str) -> str:

job_id = data["jobId"]
if job_id:
logger.info(f"Waiting for files to be ready for download...")
from time import sleep
payload = {
"organizationId": organization_id,
Expand Down

0 comments on commit 65f4d75

Please sign in to comment.