Skip to content

Commit cd034bd

Browse files
committed
Merge dev into main
2 parents 7d2a2d7 + dfb0a61 commit cd034bd

File tree

12 files changed

+139
-11
lines changed

12 files changed

+139
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Powerful Python util methods and classes that simplify common apis and tasks.
44

5-
![Current Release](https://img.shields.io/badge/release-v1.87.0-blue)
5+
![Current Release](https://img.shields.io/badge/release-v1.87.1-blue)
66
[![codecov](https://codecov.io/gh/owasp-sbot/OSBot-Utils/graph/badge.svg?token=GNVW0COX1N)](https://codecov.io/gh/owasp-sbot/OSBot-Utils)
77

88

osbot_utils/helpers/Guid.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import uuid
2+
3+
from osbot_utils.utils.Misc import is_guid
4+
5+
GUID__NAMESPACE = uuid.UUID('2cfec064-537a-4ff7-8fdc-2fc9e2606f3d')
6+
7+
class Guid(str):
8+
def __new__(cls, value: str):
9+
if not isinstance(value, str): # Check if the value is a string
10+
raise ValueError(f'in Guid: value provided was not a string: {value}') # if not raise a ValueError
11+
if is_guid(value):
12+
guid = value
13+
else:
14+
guid = uuid.uuid5(GUID__NAMESPACE, value) # Generate a UUID5 using the namespace and value
15+
return super().__new__(cls, str(guid)) # Return a new instance of Guid initialized with the string version of the UUID
16+
17+
def __str__(self):
18+
return self

osbot_utils/helpers/Random_Guid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ def __new__(cls, value=None):
1111
raise ValueError(f'in Random_Guid: value provided was not a Guid: {value}')
1212

1313
def __str__(self):
14-
return self
14+
return self

osbot_utils/helpers/Safe_Id.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from osbot_utils.utils.Str import safe_id
33

44
class Safe_Id(str):
5-
def __new__(cls, value=None):
5+
def __new__(cls, value=None, max_length=36):
66
if value is None:
77
value = safe_id(random_id_short('safe-id'))
8-
sanitized_value = safe_id(value)
8+
sanitized_value = safe_id(value, max_length=max_length)
99
return str.__new__(cls, sanitized_value)
1010

1111
def __str__(self):

osbot_utils/helpers/Str_ASCII.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import re
2+
3+
REGEX__ASCII_VALUE = re.compile(r'[^a-zA-Z0-9_\s!@#$%^&*()\[\]{}\-+=:;,.?]')
4+
5+
class Str_ASCII(str):
6+
"""
7+
A string subclass that ensures values only contain safe ASCII characters.
8+
Replaces any unsafe characters with underscores.
9+
"""
10+
def __new__(cls, value=None, max_length=None):
11+
if value is None:
12+
value = ""
13+
14+
if not isinstance(value, str):
15+
value = str(value)
16+
17+
if max_length and len(value) > max_length:
18+
raise ValueError(f"Value length exceeds maximum of {max_length} characters (was {len(value)})")
19+
20+
sanitized_value = REGEX__ASCII_VALUE.sub('_', value)
21+
22+
return super().__new__(cls, sanitized_value)

osbot_utils/utils/Misc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ def is_guid(value):
178178
except Exception:
179179
return False
180180

181-
182181
def ignore_warning__unclosed_ssl():
183182
import warnings
184183
warnings.filterwarnings("ignore", category=ResourceWarning, message="unclosed.*<ssl.SSLSocket.*>")

osbot_utils/utils/Objects.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ def convert_dict_to_value_from_obj_annotation(target, attr_name, value):
109109

110110
def convert_to_value_from_obj_annotation(target, attr_name, value): # todo: see the side effects of doing this for all ints and floats
111111

112-
from osbot_utils.helpers.Safe_Id import Safe_Id
112+
from osbot_utils.helpers.Guid import Guid
113113
from osbot_utils.helpers.Timestamp_Now import Timestamp_Now
114114
from osbot_utils.helpers.Random_Guid import Random_Guid
115+
from osbot_utils.helpers.Safe_Id import Safe_Id
116+
from osbot_utils.helpers.Str_ASCII import Str_ASCII
115117

116-
TYPE_SAFE__CONVERT_VALUE__SUPPORTED_TYPES = [Safe_Id, Random_Guid, Timestamp_Now]
118+
TYPE_SAFE__CONVERT_VALUE__SUPPORTED_TYPES = [Guid, Random_Guid, Safe_Id, Str_ASCII, Timestamp_Now]
117119

118120
if target is not None and attr_name is not None:
119121
if hasattr(target, '__annotations__'):

osbot_utils/utils/Str.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ def strip_quotes(value: str): # Remove surrounding quo
3434
return value[1:-1]
3535
return value
3636

37-
def safe_id(value):
37+
def safe_id(value, max_length=36):
3838
if value is None or value == "":
3939
raise ValueError("Invalid ID: The ID must not be empty.")
4040

4141
if not isinstance(value, str):
4242
value = str(value)
4343

44-
if len(value) > 36:
44+
if len(value) > max_length:
4545
raise ValueError(f"Invalid ID: The ID must not exceed 36 characters (was {len(value)}).")
4646

4747
sanitized_value = REGEX__SAFE_ID_REGEX.sub('_', value)

osbot_utils/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.87.0
1+
v1.87.1

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "osbot_utils"
3-
version = "v1.87.0"
3+
version = "v1.87.1"
44
description = "OWASP Security Bot - Utils"
55
authors = ["Dinis Cruz <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)