Skip to content

Commit

Permalink
Draft for /files support #377
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Feb 17, 2023
1 parent d58650c commit d051f8e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
2 changes: 2 additions & 0 deletions openeo/internal/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def render_component(component: str, data = None, parameters: dict = None):
# Set the data as the corresponding parameter in the Vue components
key = COMPONENT_MAP.get(component, component)
if data is not None:
if isinstance(data, list):
data = [(x.to_dict() if "to_dict" in dir(x) else x) for x in data]
parameters[key] = data

# Construct HTML, load Vue Components source files only if the openEO HTML tag is not yet defined
Expand Down
13 changes: 7 additions & 6 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from openeo.rest.datacube import DataCube
from openeo.rest.imagecollectionclient import ImageCollectionClient
from openeo.rest.mlmodel import MlModel
from openeo.rest.userfile import UserFile
from openeo.rest.job import BatchJob, RESTJob
from openeo.rest.rest_capabilities import RESTCapabilities
from openeo.rest.service import Service
Expand Down Expand Up @@ -1092,20 +1093,20 @@ def list_files(self):
"""
Lists all files that the logged in user uploaded.
:return: file_list: List of the user uploaded files.
:return: file_list: List of the user-uploaded files.
"""

files = self.get('/files', expected_status=200).json()['files']
files = [UserFile(file['path'], self, file) for file in files]
return VisualList("data-table", data=files, parameters={'columns': 'files'})

def create_file(self, path):
def get_file(self, path) -> UserFile:
"""
Creates virtual file
Gets a (virtual) file for the user workspace
:return: file object.
:return: UserFile object.
"""
# No endpoint just returns a file object.
raise NotImplementedError()
return UserFile(path, self)

def _build_request_with_process_graph(self, process_graph: Union[dict, Any], **kwargs) -> dict:
"""
Expand Down
58 changes: 58 additions & 0 deletions openeo/rest/userfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import typing
from typing import Any, Dict, List, Union
from pathlib import Path

if typing.TYPE_CHECKING:
# Imports for type checking only (circular import issue at runtime).
from openeo.rest.connection import Connection


class UserFile:
"""Represents a file in the user-workspace of openeo."""

def __init__(self, path: str, connection: 'Connection', metadata: Dict[str, Any] = {}):
self.path = path
self.metadata = metadata
self.connection = connection

def __repr__(self):
return '<{c} file={i!r}>'.format(c=self.__class__.__name__, i=self.path)

def _get_endpoint(self) -> str:
return "/files/{}".format(self.path)

def get_metadata(self, key) -> Any:
""" Get metadata about the file, e.g. file size (key: 'size') or modification date (key: 'modified')."""
if key in self.metadata:
return self.metadata[key]
else:
return None

def download_file(self, target: Union[Path, str]) -> Path:
""" Downloads a user-uploaded file."""
# GET /files/{path}
response = self.connection.get(self._get_endpoint(), expected_status=200, stream=True)

path = Path(target)
with path.open(mode="wb") as f:
for chunk in response.iter_content(chunk_size=None):
f.write(chunk)

return path


def upload_file(self, source: Union[Path, str]):
# PUT /files/{path}
""" Uploaded (or replaces) a user-uploaded file."""
path = Path(target)
with path.open(mode="rb") as f:
self.connection.put(self._get_endpoint(), expected_status=200, data=f)

def delete_file(self):
""" Delete a user-uploaded file."""
# DELETE /files/{path}
self.connection.delete(self._get_endpoint(), expected_status=204)

def to_dict(self) -> Dict[str, Any]:
""" Returns the provided metadata as dict."""
return self.metadata if "path" in self.metadata else {"path": self.path}

0 comments on commit d051f8e

Please sign in to comment.