Snippet to decode base64 UUIDs in .proj files

In case anyone else finds this useful…

In .proj export files, resources are linked to folders by UUID - but the folder UUID is encoded differently, which makes it hard to match them up:

<resource id='20439' name='Folder A' module='fpmi' modver='' type='__folder' ver='0' dirty='false' editcount='0' parent='' oemlocked='false' scope='4' protected='false'>
<resource id='20414' name='Folder B' module='fpmi' modver='' type='__folder' ver='0' dirty='false' editcount='0' parent='d14d721f-0ea4-4131-9572-bc7bd34cf581' oemlocked='false' scope='4' protected='false'>

Strangely, the base64 encoded version is also reversed from the UUID string.

You can convert between the UUID string and the base64 representation with the following:

#!/usr/bin/env python
import uuid
from base64 import b64encode, b64decode

def encode(text):
    return b64encode(uuid.UUID(text).bytes[::-1])

def decode(text):
    return uuid.UUID(bytes=b64decode(text)[::-1])

if __name__ == '__main__':
    import sys
    for arg in sys.argv[1:]:
        if len(arg) == 24:
            print decode(arg)
        elif len(arg) == 36:
            print encode(arg)
            sys.stderr.write('Invalid length\n')

Example usage:

$ ./ d14d721f-0ea4-4131-9572-bc7bd34cf581
$ ./ gfVM03u8cpUxQaQOH3JN0Q==