import base64
import logging

import msgpack

logger = logging.getLogger(__name__)

loadargs = {'use_list': False, 'raw': False}
if msgpack.version < (1, 0, 0):
    loadargs['encoding'] = 'utf-8'
else:
    loadargs['strict_map_key'] = False

VSIG = b'MSGVIV'.ljust(8, b'\x00')


def vivEventsAppendFile(filename, events):
    with open(filename, 'ab') as f:
        for event in events:
            if event[0] == 20:
                mape = base64.b64encode(event[1][3])
                event = (event[0], (event[1][0], event[1][1], event[1][2], mape))
            msgpack.pack(event, f, use_bin_type=False)


def saveWorkspaceChanges(vw, filename):
    events = vw.exportWorkspaceChanges()
    vivEventsAppendFile(filename, events)


def vivEventsToFile(filename, events):
    with open(filename, 'wb') as f:
        msgpack.pack(VSIG, f, use_bin_type=False)
        for event in events:
            if event[0] == 20:
                mape = base64.b64encode(event[1][3])
                event = (event[0], (event[1][0], event[1][1], event[1][2], mape))
            msgpack.pack(event, f, use_bin_type=False)


def saveWorkspace(vw, filename):
    events = vw.exportWorkspace()
    vivEventsToFile(filename, events)


def vivEventsFromFile(filename):
    events = []
    with open(filename, 'rb') as f:
        unpacker = msgpack.Unpacker(f, **loadargs)
        siggy = next(unpacker)
        if siggy.encode('utf-8') != VSIG:
            logger.warning('Invalid file signature of %s', str(siggy))
            return
        for event in unpacker:
            if event[0] == 20:
                mape = base64.b64decode(event[1][3])
                event = (event[0], (event[1][0], event[1][1], event[1][2], mape))
            events.append(event)
    return events


def loadWorkspace(vw, filename):
    events = vivEventsFromFile(filename)
    vw.importWorkspace(events)

[((55, 82), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (72, 82), False, 'import logging\n'), ((818, 859), 'msgpack.pack', 'msgpack.pack', (['VSIG', 'f'], {'use_bin_type': '(False)'}), '(VSIG, f, use_bin_type=False)\n', (830, 859), False, 'import msgpack\n'), ((1323, 1354), 'msgpack.Unpacker', 'msgpack.Unpacker', (['f'], {}), '(f, **loadargs)\n', (1339, 1354), False, 'import msgpack\n'), ((565, 607), 'msgpack.pack', 'msgpack.pack', (['event', 'f'], {'use_bin_type': '(False)'}), '(event, f, use_bin_type=False)\n', (577, 607), False, 'import msgpack\n'), ((1067, 1109), 'msgpack.pack', 'msgpack.pack', (['event', 'f'], {'use_bin_type': '(False)'}), '(event, f, use_bin_type=False)\n', (1079, 1109), False, 'import msgpack\n'), ((441, 470), 'base64.b64encode', 'base64.b64encode', (['event[1][3]'], {}), '(event[1][3])\n', (457, 470), False, 'import base64\n'), ((943, 972), 'base64.b64encode', 'base64.b64encode', (['event[1][3]'], {}), '(event[1][3])\n', (959, 972), False, 'import base64\n'), ((1603, 1632), 'base64.b64decode', 'base64.b64decode', (['event[1][3]'], {}), '(event[1][3])\n', (1619, 1632), False, 'import base64\n')]