This repository was archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
#58: Added type hints #59
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 31 additions & 16 deletions
47
exasol_bucketfs_utils_python/abstract_bucketfs_location.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,67 @@ | ||
| import typing | ||
| from abc import ABC, abstractmethod | ||
| from typing import Any | ||
| from pathlib import Path | ||
| from typing import Any, Tuple, IO | ||
| from pathlib import PurePosixPath, Path | ||
| from urllib.parse import ParseResult | ||
|
|
||
|
|
||
| class AbstractBucketFSLocation(ABC): | ||
| """ | ||
| Abstract class for a BucketFSLocation for uploading and downloading strings, fileobjects and joblib objects. | ||
| Also able to read files from the BucketFS directly, if called from inside a UDF. | ||
| Abstract class for a BucketFSLocation for uploading and downloading strings, | ||
| fileobjects and joblib objects. Also able to read files from the BucketFS | ||
| directly, if called from inside a UDF. | ||
| """ | ||
| @abstractmethod | ||
| def download_from_bucketfs_to_string(self, bucket_file_path: str) -> str: | ||
| def download_from_bucketfs_to_string(self, | ||
| bucket_file_path: str) -> str: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def download_object_from_bucketfs_via_joblib(self, bucket_file_path: str) -> Any: | ||
| def download_object_from_bucketfs_via_joblib(self, | ||
| bucket_file_path: str) -> Any: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def upload_string_to_bucketfs(self, bucket_file_path: str, string: str): | ||
| def upload_string_to_bucketfs(self, | ||
| bucket_file_path: str, | ||
| string: str) -> \ | ||
| Tuple[ParseResult, PurePosixPath]: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def upload_object_to_bucketfs_via_joblib(self, object: Any, | ||
| def upload_object_to_bucketfs_via_joblib(self, | ||
| object: Any, | ||
| bucket_file_path: str, | ||
| **kwargs): | ||
| **kwargs) -> \ | ||
| Tuple[ParseResult, PurePosixPath]: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def upload_fileobj_to_bucketfs(self, | ||
| fileobj: typing.IO, | ||
| bucket_file_path: str): | ||
| fileobj: IO, | ||
| bucket_file_path: str) -> \ | ||
| Tuple[ParseResult, PurePosixPath]: | ||
| pass | ||
|
|
||
| # TODO add missing upload/download functions | ||
|
|
||
| @abstractmethod | ||
| def read_file_from_bucketfs_to_string(self, bucket_file_path: str) -> str: | ||
| def read_file_from_bucketfs_to_string(self, | ||
| bucket_file_path: str) -> str: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def read_file_from_bucketfs_to_file(self, bucket_file_path: str, local_file_path: Path): | ||
| def read_file_from_bucketfs_to_file(self, | ||
| bucket_file_path: str, | ||
| local_file_path: Path) -> None: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def read_file_from_bucketfs_to_fileobj(self, bucket_file_path: str, fileobj: typing.IO): | ||
| def read_file_from_bucketfs_to_fileobj(self, | ||
| bucket_file_path: str, | ||
| fileobj: IO) -> None: | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def read_file_from_bucketfs_via_joblib(self, bucket_file_path: str) -> typing.Any: | ||
| def read_file_from_bucketfs_via_joblib(self, | ||
| bucket_file_path: str) -> Any: | ||
| pass | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,92 +1,113 @@ | ||
| import typing | ||
| from typing import Any, Tuple, IO | ||
| from pathlib import PurePosixPath, Path | ||
| from typing import Any | ||
|
|
||
| from urllib.parse import ParseResult | ||
| from exasol_bucketfs_utils_python import download, upload | ||
| from exasol_bucketfs_utils_python import load_file_from_local_fs as from_BFS | ||
| from exasol_bucketfs_utils_python.bucket_config import BucketConfig | ||
|
|
||
| from exasol_bucketfs_utils_python.abstract_bucketfs_location import AbstractBucketFSLocation | ||
| from exasol_bucketfs_utils_python.abstract_bucketfs_location import \ | ||
| AbstractBucketFSLocation | ||
|
|
||
|
|
||
| class BucketFSLocation(AbstractBucketFSLocation): | ||
| """ | ||
| BucketFSLocation implements AbstractBucketFSLocation. | ||
| BucketFSLocation is used to upload fileobjects, strings or joblib objects to the BucketFS given a path and the object, | ||
| or to download objects into strings, fileobjects or joblib objects from the BucketFS given a file path. | ||
| Also able to read files from the BucketFS directly, if called from inside of an UDF. | ||
| If reading an object via joblib inside of an UDF, make sure the object type is known inside the UDF. | ||
| BucketFSLocation is used to upload fileobjects, strings or joblib objects to | ||
| the BucketFS given a path and the object, or to download objects into | ||
| strings, fileobjects or joblib objects from the BucketFS given a file path. | ||
| Also able to read files from the BucketFS directly, if called from inside of | ||
| an UDF. If reading an object via joblib inside of an UDF, make sure the | ||
| object type is known inside the UDF. | ||
| """ | ||
|
|
||
| def __init__(self, bucket_config: BucketConfig, base_path: PurePosixPath): | ||
| self.base_path = base_path | ||
| self.bucket_config = bucket_config | ||
|
|
||
| def get_complete_file_path_in_bucket(self, bucket_file_path: str) -> str: | ||
| def get_complete_file_path_in_bucket(self, | ||
| bucket_file_path: str) -> str: | ||
| return str(PurePosixPath(self.base_path, bucket_file_path)) | ||
|
|
||
| def download_from_bucketfs_to_string(self, bucket_file_path: str) -> str: | ||
| def download_from_bucketfs_to_string(self, | ||
| bucket_file_path: str) -> str: | ||
| result = download.download_from_bucketfs_to_string( | ||
| self.bucket_config, | ||
| self.get_complete_file_path_in_bucket(bucket_file_path)) | ||
| self.get_complete_file_path_in_bucket(bucket_file_path) | ||
| ) | ||
| return result | ||
|
|
||
| def download_object_from_bucketfs_via_joblib(self, bucket_file_path: str) -> Any: | ||
| def download_object_from_bucketfs_via_joblib(self, | ||
| bucket_file_path: str) -> Any: | ||
| result = download.download_object_from_bucketfs_via_joblib( | ||
| self.bucket_config, | ||
| self.get_complete_file_path_in_bucket(bucket_file_path)) | ||
| self.get_complete_file_path_in_bucket(bucket_file_path) | ||
| ) | ||
| return result | ||
|
|
||
| def upload_string_to_bucketfs(self, bucket_file_path: str, string: str): | ||
| def upload_string_to_bucketfs(self, | ||
| bucket_file_path: str, | ||
| string: str) -> \ | ||
| Tuple[ParseResult, PurePosixPath]: | ||
| result = upload.upload_string_to_bucketfs( | ||
| self.bucket_config, | ||
| self.get_complete_file_path_in_bucket(bucket_file_path), | ||
| string) | ||
| string | ||
| ) | ||
| return result | ||
|
|
||
| def upload_object_to_bucketfs_via_joblib(self, object: Any, | ||
| bucket_file_path: str, | ||
| **kwargs): | ||
| **kwargs) -> \ | ||
| Tuple[ParseResult, PurePosixPath]: | ||
| result = upload.upload_object_to_bucketfs_via_joblib( | ||
| object, | ||
| self.bucket_config, | ||
| self.get_complete_file_path_in_bucket(bucket_file_path), | ||
| **kwargs) | ||
| **kwargs | ||
| ) | ||
| return result | ||
|
|
||
| def upload_fileobj_to_bucketfs(self, | ||
| fileobj: typing.IO, | ||
| bucket_file_path: str): | ||
| fileobj: IO, | ||
| bucket_file_path: str) -> \ | ||
| Tuple[ParseResult, PurePosixPath]: | ||
| result = upload.upload_fileobj_to_bucketfs( | ||
| self.bucket_config, | ||
| self.get_complete_file_path_in_bucket(bucket_file_path), | ||
| fileobj) | ||
| return result | ||
|
|
||
| def read_file_from_bucketfs_to_string(self, bucket_file_path: str) -> str: | ||
| result = from_BFS.read_file_from_bucketfs_to_string( | ||
| self.get_complete_file_path_in_bucket(bucket_file_path), | ||
| self.bucket_config | ||
| fileobj | ||
| ) | ||
| return result | ||
|
|
||
| def read_file_from_bucketfs_via_joblib(self, bucket_file_path: str) -> typing.Any: | ||
| result = from_BFS.read_file_from_bucketfs_via_joblib( | ||
| def read_file_from_bucketfs_to_string(self, | ||
| bucket_file_path: str) -> str: | ||
| result = from_BFS.read_file_from_bucketfs_to_string( | ||
| self.get_complete_file_path_in_bucket(bucket_file_path), | ||
| self.bucket_config | ||
| ) | ||
| return result | ||
|
|
||
| def read_file_from_bucketfs_to_file(self, bucket_file_path: str, local_file_path: Path) -> None: | ||
| def read_file_from_bucketfs_to_file(self, | ||
| bucket_file_path: str, | ||
| local_file_path: Path) -> None: | ||
| from_BFS.read_file_from_bucketfs_to_file( | ||
| self.get_complete_file_path_in_bucket(bucket_file_path), | ||
| self.bucket_config, | ||
| local_file_path | ||
| ) | ||
|
|
||
| def read_file_from_bucketfs_to_fileobj(self, bucket_file_path: str, fileobj: typing.IO) -> None: | ||
| def read_file_from_bucketfs_to_fileobj(self, | ||
| bucket_file_path: str, | ||
| fileobj: IO) -> None: | ||
| from_BFS.read_file_from_bucketfs_to_fileobj( | ||
| self.get_complete_file_path_in_bucket(bucket_file_path), | ||
| self.bucket_config, | ||
| fileobj | ||
| ) | ||
|
|
||
| def read_file_from_bucketfs_via_joblib(self, | ||
| bucket_file_path: str) -> Any: | ||
| result = from_BFS.read_file_from_bucketfs_via_joblib( | ||
| self.get_complete_file_path_in_bucket(bucket_file_path), | ||
| self.bucket_config | ||
| ) | ||
| return result |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.