def ParseGlobalID(inp, detect_version=False, as_field=True, fast=True): """ Split input string by parts according to different global ID formats: For such input (global resource path): "[email protected]:myfiles/animals/cat.png#F20160313043757PM" returns such dictionary object: { "user": "******", "key_alias": "group_abc", "key_id": "[email protected]", "idhost": "first-machine.com", "customer": "*****@*****.**", "idurl": b"http://first-machine.com/alice.xml", "path": "myfiles/animals/cat.png", "version": "F20160313043757PM", } For such input (global path ID) with `detect_version=True`: "[email protected]:1/2/3/F20160313043757PM/4-5-Parity" returns such dictionary object: { "user": "******", "key_alias": "group_abc", "key_id": "[email protected]", "idhost": "first-machine.com", "customer": "*****@*****.**", "idurl": b"http://first-machine.com/alice.xml", "path": "1/2/3/F20160313043757PM/4-5-Parity", "version": "F20160313043757PM", } """ result = { "user": "", "key_alias": "", "key_id": "", "idhost": "", "customer": "", "idurl": b'', "path": "", "version": "", } if not inp: if as_field: from userid import id_url result['idurl'] = id_url.field(result['idurl']) return result inp = strng.to_text(inp) if inp.count('&') == 2: # this is GLOBAL_ID_QUEUE_ID format : just need to get rid of the last supplier_id part and # translate it into GLOBAL_ID_KEY_USER format inp, _, _ = inp.strip().rpartition('&') inp = inp.replace('&', '$') if inp.count(':'): user, _, path = inp.strip().rpartition(':') else: if inp.count('@'): user = inp path = '' else: user = '' path = inp if user: user_and_key, _, idhost = user.strip().rpartition('@') if not user_and_key or not idhost: return result try: if fast: _key_alias, _, _user = user_and_key.rpartition('$') result['key_alias'] = _key_alias result['user'] = _user else: user_key = re.match(_REGEX_GLOBAL_ID_KEY_USER, user_and_key) if not user_key: user_key = re.match(_REGEX_GLOBAL_ID_USER_KEY, user_and_key) if user_key: result['user'] = user_key.group('user') result['key_alias'] = user_key.group('key_alias') else: result['user'] = user_and_key except: return result result['idhost'] = idhost if result['idhost'].count('_'): _pos = result['idhost'].rfind('_') port = result['idhost'][_pos + 1:] try: port = int(port) except: port = -1 if port >= 0: result['idhost'] = "%s:%d" % (result['idhost'][:_pos], port) if result['user'] and result['idhost']: result['idurl'] = strng.to_bin('http://{}/{}.xml'.format( result['idhost'], result['user'])) result['customer'] = '{}@{}'.format( result['user'], result['idhost'].replace(':', '_')) if path: if path.count('#'): path, _, version = path.rpartition('#') result['version'] = version result['path'] = path if detect_version: try: from lib import packetid backupID, _, fileName = path.rpartition('/') if packetid.IsPacketNameCorrect(fileName): _, _, versionName = backupID.rpartition('/') result['version'] = versionName except: pass if not result['key_alias']: result['key_alias'] = 'master' if result['customer']: result['key_id'] = MakeGlobalKeyID(result['key_alias'], result['customer']) if as_field: from userid import id_url result['idurl'] = id_url.field(result['idurl']) return result
def ParseGlobalID(inp, detect_version=False): """ Split input string by parts according to different global ID formats: For such input (global resource path): "[email protected]:myfiles/animals/cat.png#F20160313043757PM" returns such dictionary object: { "user": "******", "key_alias": "group_abc", "idhost": "first-machine.com", "customer": "*****@*****.**", "idurl": "http://first-machine.com/alice.xml", "path": "myfiles/animals/cat.png", "version": "F20160313043757PM", } For such input (global path ID) with `detect_version=True`: "[email protected]:1/2/3/F20160313043757PM/4-5-Parity" returns such dictionary object: { "user": "******", "key_alias": "group_abc", "idhost": "first-machine.com", "customer": "*****@*****.**", "idurl": "http://first-machine.com/alice.xml", "path": "1/2/3/F20160313043757PM/4-5-Parity", "version": "F20160313043757PM", } """ result = { "user": "", "key_alias": "", "idhost": "", "customer": "", "idurl": "", "path": "", "version": "", } if not inp or not str(inp): return result if inp.count(':'): user, _, path = inp.strip().rpartition(':') else: if inp.count('@'): user = inp path = '' else: user = '' path = inp if user: user_and_key, _, idhost = user.strip().rpartition('@') if not user_and_key or not idhost: return result try: user_key = re.match(_REGEX_GLOBAL_ID_USER_KEY, user_and_key) if not user_key: user_key = re.match(_REGEX_GLOBAL_ID_KEY_USER, user_and_key) if user_key: result['user'] = user_key.group('user') result['key_alias'] = user_key.group('key_alias') else: result['user'] = user_and_key except: return result result['idhost'] = idhost if result['idhost'].count('_'): _pos = result['idhost'].rfind('_') port = result['idhost'][_pos + 1:] try: port = int(port) except: port = -1 if port >= 0: result['idhost'] = "%s:%d" % (result['idhost'][:_pos], port) if result['user'] and result['idhost']: result['idurl'] = 'http://{}/{}.xml'.format(result['idhost'], result['user']) result['customer'] = '{}@{}'.format(result['user'], result['idhost'].replace(':', '_')) if path: if path.count('#'): path, _, version = path.rpartition('#') result['version'] = version result['path'] = path if detect_version: try: from lib import packetid backupID, _, fileName = path.rpartition('/') if packetid.IsPacketNameCorrect(fileName): _, _, versionName = backupID.rpartition('/') result['version'] = versionName except: pass return result