Skip to content

Commit 265c9b0

Browse files
feat: Refactored utils, moved function to calculate file size (#170)
* feat: Refactored utils, moved function to calculate file size * Update src/unstract/sdk/utils/common_utils.py Co-authored-by: vishnuszipstack <[email protected]> Signed-off-by: Chandrasekharan M <[email protected]> --------- Signed-off-by: Chandrasekharan M <[email protected]> Co-authored-by: vishnuszipstack <[email protected]>
1 parent 88f2c80 commit 265c9b0

File tree

5 files changed

+42
-34
lines changed

5 files changed

+42
-34
lines changed

src/unstract/sdk/tool/stream.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from deprecated import deprecated
88

99
from unstract.sdk.constants import Command, LogLevel, LogStage, ToolEnv
10-
from unstract.sdk.utils import ToolUtils
10+
from unstract.sdk.utils import Utils
1111
from unstract.sdk.utils.common_utils import UNSTRACT_TO_PY_LOG_LEVEL
1212

1313

@@ -29,7 +29,7 @@ def __init__(self, log_level: LogLevel = LogLevel.INFO, **kwargs) -> None:
2929
3030
"""
3131
self.log_level = log_level
32-
self._exec_by_tool = ToolUtils.str_to_bool(
32+
self._exec_by_tool = Utils.str_to_bool(
3333
os.environ.get(ToolEnv.EXECUTION_BY_TOOL, "False")
3434
)
3535
if self.is_exec_by_tool:

src/unstract/sdk/tool/validator.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from unstract.sdk.constants import MetadataKey, PropKey
99
from unstract.sdk.tool.base import BaseTool
1010
from unstract.sdk.tool.mime_types import EXT_MIME_MAP
11+
from unstract.sdk.utils import Utils
1112

1213

1314
def extend_with_default(validator_class: Any) -> Any:
@@ -119,7 +120,7 @@ def _validate_file_size(self, input_file: Path) -> None:
119120
f"Checking input file size... (max file size: {max_file_size})"
120121
)
121122
file_size = self.tool.workflow_filestorage.size(path=input_file)
122-
self.tool.stream_log(f"Input file size: {self._human_readable_size(file_size)}")
123+
self.tool.stream_log(f"Input file size: {Utils.pretty_file_size(file_size)}")
123124

124125
if file_size > max_size_in_bytes:
125126
source_name = self.tool.get_exec_metadata.get(MetadataKey.SOURCE_NAME)
@@ -128,22 +129,6 @@ def _validate_file_size(self, input_file: Path) -> None:
128129
f"allowed size of {max_file_size}"
129130
)
130131

131-
def _human_readable_size(self, num: float, suffix: str = "B") -> str:
132-
"""Gets the human readable size for a file,
133-
134-
Args:
135-
num (int): Size in bytes to parse
136-
suffix (str, optional): _description_. Defaults to "B".
137-
138-
Returns:
139-
str: Human readable size
140-
"""
141-
for unit in ("", "K", "M", "G", "T"):
142-
if abs(num) < 1024.0:
143-
return f"{num:3.1f}{unit}{suffix}"
144-
num /= 1024.0
145-
return f"{num:.1f}{suffix}"
146-
147132
def _parse_size_string(self, size_string: str) -> int:
148133
"""Parses the size string for validation.
149134

src/unstract/sdk/utils/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
from .common_utils import CommonUtils, Utils # noqa
12
from .file_storage_utils import FileStorageUtils # noqa
23
from .tool_utils import ToolUtils # noqa

src/unstract/sdk/utils/common_utils.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,48 @@
99
logger = logging.getLogger(__name__)
1010

1111

12-
class CommonUtils:
12+
class Utils:
1313
@staticmethod
1414
def generate_uuid() -> str:
1515
"""Class method to get uuid."""
1616
return str(uuid.uuid4())
1717

18+
@staticmethod
19+
def str_to_bool(string: str) -> bool:
20+
"""String value of boolean to boolean.
21+
22+
Useful while parsing envs to bool.
23+
24+
Args:
25+
string (str): value like "true", "True" etc..
26+
27+
Returns:
28+
bool
29+
"""
30+
return string.lower() == "true"
31+
32+
@staticmethod
33+
def pretty_file_size(num: float, suffix: str = "B") -> str:
34+
"""Gets the human readable size for a file,
35+
36+
Args:
37+
num (int): Size in bytes to parse
38+
suffix (str, optional): _description_. Defaults to "B".
39+
40+
Returns:
41+
str: Human readable size
42+
"""
43+
for unit in ("", "K", "M", "G", "T"):
44+
if abs(num) < 1024.0:
45+
return f"{num:.2f} {unit}{suffix}"
46+
num /= 1024.0
47+
return f"{num:.2f} {suffix}"
48+
49+
50+
# TODO: Kept for backward compatibility, make use of Utils instead
51+
class CommonUtils(Utils):
52+
pass
53+
1854

1955
# Mapping from python log level to Unstract counterpart
2056
PY_TO_UNSTRACT_LOG_LEVEL = {

src/unstract/sdk/utils/tool_utils.py

-14
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,6 @@ def get_file_size(
161161
file_length = fs.size(path=input_file)
162162
return file_length
163163

164-
@staticmethod
165-
def str_to_bool(string: str) -> bool:
166-
"""String value of boolean to boolean.
167-
168-
Useful while parsing envs to bool.
169-
170-
Args:
171-
string (str): value like "true", "True" etc..
172-
173-
Returns:
174-
bool
175-
"""
176-
return string.lower() == "true"
177-
178164
# Used the same function from LLMWhisperer
179165
@staticmethod
180166
def calculate_page_count(

0 commit comments

Comments
 (0)