Skip to content

Commit f1402c0

Browse files
Refactoring for changed file names (#132)
* Refactoring changed file names * Roll version * Update src/unstract/sdk/utils/tool_utils.py Co-authored-by: Chandrasekharan M <[email protected]> Signed-off-by: Gayathri <[email protected]> * Update tests/test_fs_permanent.py Co-authored-by: Chandrasekharan M <[email protected]> Signed-off-by: Gayathri <[email protected]> * Update tests/test_fs_permanent.py Co-authored-by: Chandrasekharan M <[email protected]> Signed-off-by: Gayathri <[email protected]> * Update src/unstract/sdk/utils/tool_utils.py Co-authored-by: Chandrasekharan M <[email protected]> Signed-off-by: Gayathri <[email protected]> * Address review comments --------- Signed-off-by: Gayathri <[email protected]> Co-authored-by: Chandrasekharan M <[email protected]>
1 parent a1399a6 commit f1402c0

File tree

6 files changed

+59
-18
lines changed

6 files changed

+59
-18
lines changed

src/unstract/sdk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.54.0rc4"
1+
__version__ = "0.54.0rc5"
22

33

44
def get_sdk_version():

src/unstract/sdk/file_storage/env_helper.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,24 @@
1313

1414

1515
class EnvHelper:
16+
ENV_CONFIG_FORMAT = (
17+
'{"provider": "gcs", ' '"credentials": {"token": "/path/to/google/creds.json"}}'
18+
)
19+
1620
@staticmethod
1721
def get_storage(storage_type: StorageType, env_name: str) -> FileStorage:
22+
"""Helper function for clients to pick up remote storage configuration
23+
from env, initialise the file storage for the same and return the
24+
instance.
25+
26+
Args:
27+
storage_type: Permanent / Temporary file storage
28+
env_name: Name of the env which has the file storage config
29+
30+
Returns:
31+
FileStorage: FIleStorage instance initialised using the provider
32+
and credentials configured in the env
33+
"""
1834
try:
1935
file_storage_creds = json.loads(os.environ.get(env_name))
2036
provider = FileStorageProvider(
@@ -31,7 +47,8 @@ def get_storage(storage_type: StorageType, env_name: str) -> FileStorage:
3147
raise NotImplementedError()
3248
return file_storage
3349
except KeyError as e:
34-
logger.error(f"Required credentials is missing in the env: {str(e)}")
50+
logger.error(f"Required credentials are missing in the env: {str(e)}")
51+
logger.error(f"The configuration format is {EnvHelper.ENV_CONFIG_FORMAT}")
3552
raise e
3653
except FileStorageError as e:
3754
raise e

src/unstract/sdk/file_storage/helper.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,46 @@ def local_file_system_init() -> AbstractFileSystem:
7070

7171

7272
def skip_local_cache(func):
73+
"""Helper function/decorator for handling FileNotFound exception and making
74+
sure that the error is not because of stale cache.
75+
76+
Args:
77+
func: The original function that is called in the context
78+
79+
Returns:
80+
NA
81+
"""
82+
7383
def wrapper(*args, **kwargs):
7484
try:
7585
return func(*args, **kwargs)
7686
except FileNotFoundError:
77-
try:
78-
# FileNotFound could have been caused by stale cache.
79-
# Hence invalidate cache and retry again
80-
args[0].fs.invalidate_cache()
81-
return func(*args, **kwargs)
82-
except Exception as e:
83-
if isinstance(e, FileNotFoundError):
84-
raise e
85-
else:
86-
raise FileOperationError(str(e)) from e
87+
_handle_file_not_found(func, *args, **kwargs)
8788
except Exception as e:
8889
raise FileOperationError(str(e)) from e
8990

9091
return wrapper
92+
93+
94+
def _handle_file_not_found(func, *args, **kwargs):
95+
"""Helper function for handling FileNotFound exception and making sure that
96+
the error is not because of stale cache.
97+
98+
Args:
99+
func: The original function that is called in the context
100+
args: The context of the function call as an array
101+
kwargs: args to the function being called in this context
102+
103+
Returns:
104+
NA
105+
"""
106+
try:
107+
# FileNotFound could have been caused by stale cache.
108+
# Hence invalidate cache and retry again
109+
args[0].fs.invalidate_cache()
110+
return func(*args, **kwargs)
111+
except Exception as e:
112+
if isinstance(e, FileNotFoundError):
113+
raise e
114+
else:
115+
raise FileOperationError(str(e)) from e

src/unstract/sdk/tool/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ToolEnv,
1515
ToolExecKey,
1616
)
17-
from unstract.sdk.file_storage.fs_shared_temporary import SharedTemporaryFileStorage
17+
from unstract.sdk.file_storage.shared_temporary import SharedTemporaryFileStorage
1818
from unstract.sdk.tool.mixin import ToolConfigHelper
1919
from unstract.sdk.tool.parser import ToolArgsParser
2020
from unstract.sdk.tool.stream import StreamMixin
@@ -61,7 +61,8 @@ def __init__(self, log_level: LogLevel = LogLevel.INFO) -> None:
6161
"Please check your settings."
6262
)
6363
self.workflow_filestorage = SharedTemporaryFileStorage(
64-
provider=self.filestorage_provider, **self.filestorage_credentials
64+
provider=self.filestorage_provider,
65+
**self.filestorage_credentials,
6566
)
6667

6768
@classmethod

src/unstract/sdk/utils/tool_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
import magic
1010

1111
from unstract.sdk.exceptions import FileStorageError
12-
from unstract.sdk.file_storage import FileStorage, FileStorageProvider
13-
from unstract.sdk.file_storage.fs_shared_temporary import SharedTemporaryFileStorage
12+
from unstract.sdk.file_storage import FileStorage, FileStorageProvider, SharedTemporaryFileStorage
1413

1514
logger = logging.getLogger(__name__)
1615

tests/test_fs_permanent.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import pytest
66
from dotenv import load_dotenv
77

8-
from unstract.sdk.file_storage import FileStorageProvider
9-
from unstract.sdk.file_storage.fs_permanent import PermanentFileStorage
8+
from unstract.sdk.file_storage import FileStorageProvider, PermanentFileStorage
109

1110
load_dotenv()
1211

0 commit comments

Comments
 (0)