Skip to content

Commit c68d2d0

Browse files
committed
Merge dev into main
2 parents 73645d0 + 1a5a7de commit c68d2d0

File tree

17 files changed

+1562
-40
lines changed

17 files changed

+1562
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OSBot-Utils
22

3-
![Current Release](https://img.shields.io/badge/release-v3.47.0-blue)
3+
![Current Release](https://img.shields.io/badge/release-v3.47.2-blue)
44
![Python](https://img.shields.io/badge/python-3.8+-green)
55
![Type-Safe](https://img.shields.io/badge/Type--Safe-✓-brightgreen)
66
![Caching](https://img.shields.io/badge/Caching-Built--In-orange)

osbot_utils/type_safe/Type_Safe__Primitive.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def __to_primitive__(self): # Convert this T
7676
return self.__primitive_base__(self)
7777
return str(self) # fallback
7878

79+
def json(self):
80+
import json
81+
return json.dumps(self.__to_primitive__())
82+
7983
def obj(self): # Get configuration as namespace object
8084
from osbot_utils.testing.__helpers import dict_to_obj
8185
return dict_to_obj(self.__cls_kwargs__())
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from osbot_utils.type_safe.primitives.domains.identifiers.Random_Guid import Random_Guid
2+
3+
4+
class Cache_Id(Random_Guid): # helper class so that we don't use Random_Guid to represent the cache_id class
5+
def __new__(cls, value=None):
6+
if value is None or value == '':
7+
return str.__new__(cls, '')
8+
else:
9+
return super().__new__(cls, value)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from osbot_utils.type_safe.primitives.domains.identifiers.Obj_Id import Obj_Id
2+
3+
4+
class Edge_Id(Obj_Id): # helper class so that we don't use Obj_Id to represent the graph_id class
5+
def __new__(cls, value=None):
6+
if value is None or value == '':
7+
return str.__new__(cls, '')
8+
else:
9+
return super().__new__(cls, value)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from osbot_utils.type_safe.primitives.domains.identifiers.Obj_Id import Obj_Id
2+
3+
4+
class Graph_Id(Obj_Id): # helper class so that we don't use Obj_Id to represent the graph_id class
5+
def __new__(cls, value=None):
6+
if value is None or value == '':
7+
return str.__new__(cls, '')
8+
else:
9+
return super().__new__(cls, value)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from osbot_utils.type_safe.primitives.domains.identifiers.Obj_Id import Obj_Id
2+
3+
4+
class Node_Id(Obj_Id): # helper class so that we don't use Obj_Id to represent the graph_id class
5+
def __new__(cls, value=None):
6+
if value is None or value == '':
7+
return str.__new__(cls, '')
8+
else:
9+
return super().__new__(cls, value)
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1+
import re
12
import random
23
from osbot_utils.type_safe.Type_Safe__Primitive import Type_Safe__Primitive
34

4-
_hex_table = [f"{i:02x}" for i in range(256)]
5+
_regex_obj_id = re.compile(r'^[0-9a-f]{8}$')
56

6-
def is_obj_id(value: str):
7+
def is_obj_id(value):
78
if isinstance(value, Obj_Id):
89
return True
9-
if isinstance(value, str):
10-
if len(value) == 8: # todo: add efficient check if we only have hex values
11-
return True
10+
if isinstance(value, str) and len(value) == 8:
11+
return _regex_obj_id.match(value) is not None
1212
return False
1313

1414
def new_obj_id():
15-
return hex(random.getrandbits(32))[2:].zfill(8) # slice off '0x' and pad
15+
return hex(random.getrandbits(32))[2:].zfill(8)
1616

17-
class Obj_Id(Type_Safe__Primitive,str):
18-
def __new__(cls, value: str=None):
17+
class Obj_Id(Type_Safe__Primitive, str):
18+
def __new__(cls, value: str = None):
1919
if value:
2020
if is_obj_id(value):
2121
obj_id = value
2222
else:
23-
raise ValueError(f'in Obj_Id: value provided was not a valid Obj_Id: {value}')
23+
raise ValueError(f'in {cls.__name__}: value provided was not a valid {cls.__name__}: {value}')
2424
else:
2525
obj_id = new_obj_id()
26-
return super().__new__(cls, obj_id) # Return a new instance of Guid initialized with the string version of the UUID
26+
return super().__new__(cls, obj_id)
2727

2828
def __str__(self):
29-
return self
29+
return str.__str__(self)
30+
31+
def __add__(self, other): # Concatenation returns plain str, not Obj_Id
32+
return str.__str__(self) + other
33+
34+
def __radd__(self, other): # Reverse concatenation returns plain str
35+
return other + str.__str__(self)

osbot_utils/type_safe/primitives/domains/identifiers/Random_Guid.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,14 @@ def __new__(cls, value=None):
99
if is_guid(value):
1010
return str.__new__(cls, value)
1111
raise ValueError(f'in Random_Guid: value provided was not a Guid: {value}')
12+
13+
14+
def __str__(self):
15+
return str.__str__(self)
16+
17+
def __add__(self, other): # Concatenation returns plain str, not Obj_Id
18+
return str.__str__(self) + other
19+
20+
def __radd__(self, other): # Reverse concatenation returns plain str
21+
return other + str.__str__(self)
22+

osbot_utils/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v3.47.0
1+
v3.47.2

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 = "v3.47.0"
3+
version = "v3.47.2"
44
description = "OWASP Security Bot - Utils"
55
authors = ["Dinis Cruz <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)