diff --git a/lib/mbs/framework/Guid.py b/lib/mbs/framework/Guid.py index d3c7dcd..447e71f 100644 --- a/lib/mbs/framework/Guid.py +++ b/lib/mbs/framework/Guid.py @@ -12,7 +12,7 @@ class UUIDParseResult: self.kind = None self.message = None - def setFailure(kind, message): + def setFailure(self, kind, message): self.kind = kind self.message = message @@ -20,12 +20,10 @@ class Guid: __urand = None - def __init__(self, val : str = None): + def __init__(self): if Guid.__urand is None: Guid.__urand = open("/dev/urandom", "rb") - self.value = val - self.__a = 0 self.__b = 0 self.__c = 0 @@ -44,9 +42,49 @@ class Guid: result = int(parseWhat, 16) parsePos += requiredLength return (parsePos, result) + + @staticmethod + def from_bytes(b : bytes): + guid = Guid() + + guid.__a = (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0] + guid.__b = (b[5] << 8) | b[4] + guid.__c = ((b[7] << 8) | b[6]) + guid.__d = b[8] + guid.__e = b[9] + guid.__f = b[10] + guid.__g = b[11] + guid.__h = b[12] + guid.__i = b[13] + guid.__j = b[14] + guid.__k = b[15] + + return guid @staticmethod def __tryParseGuidWithDashes(guidString : str) -> UUIDParseResult: + + result = UUIDParseResult() + print (guidString) + if guidString == "": + return result + + guidString = guidString.replace("{", "").replace("}", "").replace("-", "") + + import codecs + try: + myBytes = codecs.decode(guidString, 'hex') + except: + return result + + guidBytes = [ myBytes[3], myBytes[2], myBytes[1], myBytes[0], myBytes[5], myBytes[4], myBytes[7], myBytes[6], myBytes[8], + myBytes[9], myBytes[10], myBytes[11], myBytes[12], myBytes[13], myBytes[14], myBytes[15] ] + + result.parsedGuid = Guid.fromBytes(guidBytes) + return result + + """ + startPos = 0 temp = 0 templ = 0 @@ -54,11 +92,17 @@ class Guid: result = UUIDParseResult() hasDashes = True + if (len(guidString) < 1): + print(guidString) + result.setFailure(UUIDParseFailureKind.FORMAT, "Format_GuidInvLen[?]") + return result + # check to see that it's the proper length if guidString[0] == '{': if len(guidString) != 38 or guidString[37] != '}': - result.setFailure(UUIDParseFailureKind.FORMAT, "Format_GuidInvLen[{38]") + print(guidString) + result.setFailure(UUIDParseFailureKind.FORMAT, "Format_GuidInvLen[38]") return result startPos = 1 @@ -67,7 +111,8 @@ class Guid: if len(guidString) != 38 or guidString[37] != ')': - result.setFailure(UUIDParseFailureKind.FORMAT, "Format_GuidInvLen[(38]") + print(guidString) + result.setFailure(UUIDParseFailureKind.FORMAT, "Format_GuidInvLen[38]") return result startPos = 1 @@ -76,6 +121,7 @@ class Guid: if len(guidString) != 32: + print(guidString) result.setFailure(UUIDParseFailureKind.FORMAT, "Format_GuidInvLen[36]") return result @@ -118,7 +164,7 @@ class Guid: (parsePos, temp) = Guid.__stringToInt(guidString, currentPos, 4, UUIDParseNumbers.NOSPACE, temp, result) result.parsedGuid.__d = temp >> 8 - result.parsedGuid.__e = temp + result.parsedGuid.__e = temp - (temp >> 8) currentPos = parsePos if hasDashes: @@ -137,14 +183,15 @@ class Guid: # } # */ result.parsedGuid.__j = temp2 >> 8 - result.parsedGuid.__k = temp2 + result.parsedGuid.__k = temp2 - (temp2 >> 8) temp = templ result.parsedGuid.__f = temp >> 24 - result.parsedGuid.__g = temp >> 16 - result.parsedGuid.__h = temp >> 8 - result.parsedGuid.__i = temp + result.parsedGuid.__g = (temp >> 16) - (temp >> 24) + result.parsedGuid.__h = (temp >> 8) - (temp >> 16) + result.parsedGuid.__i = temp - (temp >> 8) return result + """ def parse(value : str): if value is None: @@ -284,10 +331,6 @@ class Guid: output = output[0:8] + "-" + output[8:(8+4)] + "-" + output[12:12+4] + "-" + output[16:16+4] + "-" + output[20:] return "{" + output + "}" - def get_value(self) -> str: - return self.value - - def strip(self): guidChars = "" guidChars += Guid.__hexsToChars(self.__a >> 24, self.__a >> 16) @@ -335,3 +378,40 @@ class Guid: u = uuid4() g = Guid.parse(str(u)) return g + + def __key(self): + return (self.__a, self.__b, self.__c, self.__d, self.__e, self.__f, self.__g, self.__h, self.__i, self.__j, self.__k) + + def __hash__(self): + return hash(self.__key()) + + def __eq__(self, other): + if isinstance(other, Guid): + return self.__key() == other.__key() + return NotImplemented + + def to_bytes(self) -> bytes: + a = self.__a.to_bytes(4, 'little') + b = self.__b.to_bytes(2, 'little') + c = self.__c.to_bytes(2, 'little') + + a0 = a[0] + a1 = a[1] + a2 = a[2] + a3 = a[3] + b0 = b[0] + b1 = b[1] + c0 = c[0] + c1 = c[1] + d = self.__d + e = self.__e + f = self.__f + g = self.__g + h = self.__h + i = self.__i + j = self.__j + k = self.__k + + sss = [a0, a1, a2, a3, b0, b1, c0, c1, d, e, f, g, h, i, j, k] + + return bytes(sss) \ No newline at end of file