Skip to content

Commit

Permalink
fix(sdk, uuidt): use constants where possible
Browse files Browse the repository at this point in the history
This will avoid filesystem and socket syscalls at each init.

Signed-off-by: Fatih Acar <[email protected]>
  • Loading branch information
fatih-acar committed Jul 2, 2024
1 parent 764cf31 commit 700f5b3
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions python_sdk/infrahub_sdk/uuidt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
BASE = 16
DIVISOR = BASE - 1
CHARACTERS = list("0123456789abcdefghijklmnopqrstuvwxyz")[:BASE]
HOSTNAME = None
DEFAULT_NAMESPACE = None

# Code inspired from https://github.com/isaacharrisholt/uuidt

Expand All @@ -32,16 +34,36 @@ def encode_number(number: int, min_length: int) -> str:


class UUIDT:
namespace: str
timestamp: int
hostname: str
random_chars: str

def __init__(
self,
namespace: Optional[str] = None,
timestamp: Optional[int] = None,
hostname: Optional[str] = None,
random_chars: Optional[str] = None,
):
self.namespace = namespace or str(Path(__file__).parent.resolve())
global DEFAULT_NAMESPACE
global HOSTNAME

if namespace:
self.namespace = namespace
else:
if not DEFAULT_NAMESPACE:
DEFAULT_NAMESPACE = str(Path(__file__).parent.resolve())
self.namespace = DEFAULT_NAMESPACE

if hostname:
self.hostname = hostname
else:
if not HOSTNAME:
HOSTNAME = socket.gethostname()
self.hostname = HOSTNAME

self.timestamp = timestamp or time.time_ns()
self.hostname = hostname or socket.gethostname()
self.random_chars = random_chars or "".join(random.choices(CHARACTERS, k=8))

def __str__(self) -> str:
Expand Down

0 comments on commit 700f5b3

Please sign in to comment.