<commit_before>from ofp.v0x02.types import UBInt8, UBInt8Array, UBInt16, UBInt32, Char
from ofp.v0x02.exceptions import OFPException
from ofp.v0x02.enums import OFPActionType
from ofp.v0x02.consts import *

import collections

class MetaStruct(type):
    @classmethod
    def __prepare__(self, name, bases):
        return collections.OrderedDict()

    def __new__(self, name, bases, classdict):
        classdict['__ordered__'] = [(key, type(value)) for key, value in
                            classdict.items() if key not in
                            ('__module__','__qualname__')]
        return type.__new__(self, name, bases, classdict)

class GenericStruct(metaclass=MetaStruct):
    def __init__(self, *args, **kwargs):
        for _attr, _class in self.__ordered__:
            if not callable(getattr(self, _attr)):
                # TODO: Validade data
                try:
                    setattr(self, _attr, kwargs[_attr])
                except KeyError:
                    pass

    def get_size(self):
        tot = 0
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                tot += (getattr(self, _attr).get_size())
            elif not callable(attr):
                tot += (_class(attr).get_size())
        return tot

    def pack(self):
        hex = b''
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                hex += getattr(self, _attr).build()
                print("{} {} {}".
                      format(_attr, attr,getattr(self, _attr).build()))
            elif not callable(attr):
                hex += _class(attr).build()
                print("{} {} {}"
                      .format(_attr, attr,_class(attr).build()))
        return hex

    def unpack(self, buff):
        begin = 0
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                size = (getattr(self, _attr).get_size())
                getattr(self,_attr).unpack(buff, offset=begin)
            elif not callable(attr):
                size = (_class(attr).get_size())
                getattr(self,_attr).unpack(buff, offset=begin)
            begin += size


class OFPHeader(GenericStruct):
    version = UBInt8()
    ofp_type = UBInt8()
    length = UBInt16()
    xid = UBInt32()


class OFPPhyPort(metaclass=GenericStruct):
    """
    Description of a physical port.

    The port_no field is a value the datapath associates with a physical port.
    The hw_addr field typically is the MAC address for the port;<commit_msg>Changed build to pack inside other classes.<commit_after>from ofp.v0x02.types import UBInt8, UBInt8Array, UBInt16, UBInt32, Char
from ofp.v0x02.exceptions import OFPException
from ofp.v0x02.enums import OFPActionType
from ofp.v0x02.consts import *

import collections

class MetaStruct(type):
    @classmethod
    def __prepare__(self, name, bases):
        return collections.OrderedDict()

    def __new__(self, name, bases, classdict):
        classdict['__ordered__'] = [(key, type(value)) for key, value in
                            classdict.items() if key not in
                            ('__module__','__qualname__')]
        return type.__new__(self, name, bases, classdict)

class GenericStruct(metaclass=MetaStruct):
    def __init__(self, *args, **kwargs):
        for _attr, _class in self.__ordered__:
            if not callable(getattr(self, _attr)):
                # TODO: Validade data
                try:
                    setattr(self, _attr, kwargs[_attr])
                except KeyError:
                    pass

    def get_size(self):
        tot = 0
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                tot += (getattr(self, _attr).get_size())
            elif not callable(attr):
                tot += (_class(attr).get_size())
        return tot

    def pack(self):
        hex = b''
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                hex += getattr(self, _attr).pack()
                print("{} {} {}".
                      format(_attr, attr,getattr(self, _attr).pack()))
            elif not callable(attr):
                hex += _class(attr).pack()
                print("{} {} {}"
                      .format(_attr, attr,_class(attr).pack()))
        return hex

    def unpack(self, buff):
        begin = 0
        for _attr, _class in self.__ordered__:
            attr = getattr(self, _attr)
            if _class is OFPHeader:
                size = (getattr(self, _attr).get_size())
                getattr(self,_attr).unpack(buff, offset=begin)
            elif not callable(attr):
                size = (_class(attr).get_size())
                getattr(self,_attr).unpack(buff, offset=begin)
            begin += size


class OFPHeader(GenericStruct):
    version = UBInt8()
    ofp_type = UBInt8()
    length = UBInt16()
    xid = UBInt32()


class OFPPhyPort(metaclass=GenericStruct):
    """
    Description of a physical port.

    The port_no field is a value the datapath associates with a physical port.
    The hw_addr field typically is the MAC address for the port;
{'fraction_non_alphanumeric': 0.10300668151447662, 'fraction_numerical': 0.009651076466221232, 'mean_word_length': 1.948030634573304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2717144', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1541, 'n_words': 423}