From 701e56121e3d4e1a642d609fb6de80b4ef8e2ca2 Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Thu, 10 May 2018 23:22:15 -0800 Subject: [PATCH] Parse string to Guid --- exts/imports/guid.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/exts/imports/guid.py b/exts/imports/guid.py index 38c207b..ddefd4c 100644 --- a/exts/imports/guid.py +++ b/exts/imports/guid.py @@ -1,6 +1,7 @@ from struct import Struct, pack from math import log +# 0012d687-d687-0012-0000-000000000000 # 16 bytes, 11 arguments MAXINT64 = 0xFFFFFFFFFFFFFFFF MAXINT8 = 0xFF @@ -15,7 +16,7 @@ class Guid: self.guid = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) def get_guid(self, args) -> None: - s = Struct('i2h8B') + s = Struct('I2H8B') if len(args) == 1 and isinstance(args[0], bytes) and len(args[0]) == 16: b = args[0] guid = s.pack((int(b[3]) << 24) | (int(b[2]) << 16) | (int(b[1]) << 8) | b[0], @@ -43,7 +44,7 @@ class Guid: raise TypeError('Arguments don\'t match any allowed configuration') guid = s.pack((int(b[3]) << 24) | (int(b[2]) << 16) | (int(b[1]) << 8) | b[0], ((int(b[5]) << 8) | b[4]), - (int(b[7]) << 8) | b[6], + ((int(b[7]) << 8) | b[6]), *b[8:16] ) self.guid = s.unpack(guid) @@ -60,11 +61,32 @@ class Guid: *b[8:16] ) self.guid = s.unpack(guid) - elif len(args) == 1 and isinstance(args[0], str): - pass # TODO Parse string input + elif len(args) == 1 and isinstance(args[0], str) and len(args[0]) == 36: + self.parse_string(args[0]) else: raise TypeError('Arguments don\'t match any allowed configuration') + def parse_string(self, string): + if len(string) == 36: + if string[8] == '-' and string[13] == '-' and string[18] == '-' and string[23] == '-': + guid_str = string.split('-') + try: + self.guid = (int(guid_str[0], 16), + int(guid_str[1], 16), + int(guid_str[2], 16), + int(guid_str[3], 16) >> 8, + int(guid_str[3], 16) & MAXINT8, + int(guid_str[4], 16) >> 40, + int(guid_str[4], 16) >> 32, + int(guid_str[4], 16) >> 24, + int(guid_str[4], 16) >> 16, + int(guid_str[4], 16) >> 8, + int(guid_str[4], 16) & MAXINT8, + ) + except ValueError: + raise TypeError('String is not formatted properly must be in format ' + '00000000-0000-0000-0000-000000000000') + def to_string(self): output = '' output += self.hexs_to_chars(self.guid[0] >> 24, self.guid[0] >> 16)