#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Copyright (C) 2017  Nexedi SA and Contributors.
#                     Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
"""generate registry of neo/py packet codes for tests"""

# things that are renamed a bit
# pyname -> goname
renames = {}

def noask(name):    renames['Ask'+name] = name
def nonotify(name): renames['Notify'+name] = name

noask('Recovery')
noask('LastIDs')
noask('UnfinishedTransactions')
noask('LockedTransactions')
noask('FinalTID')
noask('BeginTransaction')
noask('FinishTransaction')
noask('LockInformation')
#nonotify('UnlockInformation')   # XXX ok?
noask('RebaseTransaction')
noask('RebaseObject')
noask('StoreObject')
noask('StoreTransaction')
noask('VoteTransaction')
noask('TransactionInformation')
noask('ObjectHistory')
noask('PartitionList')
noask('NodeList')
noask('ObjectUndoSerial')
noask('Pack')
noask('CheckTIDRange')
noask('CheckSerialRange')
nonotify('PartitionCorrupted')
noask('LastTransaction')
noask('CheckCurrentSerial')
nonotify('ReplicationDone')
noask('FetchTransactions')
noask('FetchObjects')

_ = renames
_['AskPrimary']                 = 'PrimaryMaster'
_['AskObject']                  = 'GetObject'
_['NotifyRepair']               = 'RepairOne'           # XXX ok?
_['NotifyClusterInformation']   = 'NotifyClusterState'


def main():
    pyprotog = {}
    execfile('../../../neo/lib/protocol.py', pyprotog)
    pypacket  = pyprotog['Packet']
    pypackets = pyprotog['Packets']

    # dump to go what to expect
    with open("ztestdata_proto_py_test.go", "w") as f:
        def emit(v):
            print >>f, v
        emit("// Code generated by %s; DO NOT EDIT." % __file__)
        emit("package proto")

        emit("\nvar pyMsgRegistry = map[uint16]string{")

        # access packet structures by names they are defined under Packets
        # class and thus in neo/py code which uses them.
        #
        # ( this is not the same as packet class name as e.g. currently
        #   AcceptIdentification has class name AnswerRequestIdentification )
        pypktv = []
        for k in dir(pypackets):
            v = getattr(pypackets, k)
            if isinstance(v, type) and issubclass(v, pypacket):
                v._x_use_name = k
                pypktv.append(v)

        # order by message code
        pypktv.sort(key=lambda pkt: pkt._code)

        for pypkt in pypktv:
            name = pypkt._x_use_name
            name = renames.get(name, name)
            emit('\t%d:\t"%s",' % (pypkt._code, name))

        #pycodev = pypackets.keys()
        #pycodev.sort()
        #for pycode in pycodev:
        #    pymsg = pypackets[pycode]
        #    emit('\t%d:\t"%s",' % (pycode, pymsg.__name__))

        emit("}")

if __name__ == '__main__':
    main()
