# Path: shapy/framework/netlink/message.py
# class Attr(object):
#     """Represents a single attribute."""
#     #struct rtattr {
#     #   unsigned short rta_len;    /* Length of option */
#     #   unsigned short rta_type;   /* Type of option */
#     #   /* Data follows */
#     #};
#     
#     rtattr = Struct('HH')
#     
#     @classmethod
#     def unpack(cls, data):
#         length, type = cls.rtattr.unpack(data[:cls.rtattr.size])
#         data_len = length-cls.rtattr.size
#         attr_data = struct.unpack('{0}s'.format(data_len),
#                              data[cls.rtattr.size:cls.rtattr.size+data_len])[0]
#         return cls(type, attr_data), data[align(length):]
#     
#     def __init__(self, rta_type, payload):
#         self.rta_type = rta_type
#         self.payload = payload
#         self.data = self.unpack_data()
#         
#     def pack(self):
#         data = struct.pack('{0}s'.format(len(self.payload)), self.payload)
#         rta_len = self.rtattr.size+len(data)
#         return struct.pack('{0}{1}s'.format(self.rtattr.format, align(len(data))),
#                            rta_len, self.rta_type, data)
#     
#     def unpack_data(self):
#         if hasattr(self, 'data_format') and hasattr(self, 'data_struct'):
#             return self.data_struct._make(self.data_format.unpack(self.payload))
#         return None
#     
#     def __repr__(self):
#         return """<rtattr datalen=%d rta_type=%d data="%s">""" % (
#             align(len(self.payload)), self.rta_type,
#             self.payload.encode('string_escape'))


from shapy.framework.netlink.message import Attr
from .constants import *
from struct import Struct
from collections import namedtuple
    #define TC_PRIO_INTERACTIVE     6
    #define TC_PRIO_CONTROL         7
    
    #define TC_PRIO_MAX         15
    
    #define TCQ_PRIO_BANDS  16
    #define TCQ_MIN_PRIO_BANDS 2
    
    #struct tc_prio_qopt {
    #    int bands;          /* Number of bands */
    #    __u8    priomap[TC_PRIO_MAX+1]; /* Map: logical priority -> PRIO band */
    #};

    data_format = Struct("i16B4x")
    data_struct = namedtuple('tc_prio_qopt', "bands priomap")
    
    @classmethod
    def unpack(cls, data):
        attr, rest = Attr.unpack(data)
        data = self.data_format.unpack(attr.payload)
        opts = cls.data_struct._make((data[0], data[1:]))
        return cls(opts.bands, opts.priomap), rest
    
    def unpack_data(self):
        data = self.data_format.unpack(self.payload)
        return self.data_struct._make(self.data_struct._make((data[0], data[1:])))
    
    def __init__(self, bands=3, priomap="1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1"):
        pm = [ int(p) for p in priomap.split() ]
        data = self.data_format.pack(bands, *pm)

# Path: shapy/framework/netlink/message.py
# class Attr(object):
#     """Represents a single attribute."""
#     #struct rtattr {
#     #   unsigned short rta_len;    /* Length of option */
#     #   unsigned short rta_type;   /* Type of option */
#     #   /* Data follows */
#     #};
#     
#     rtattr = Struct('HH')
#     
#     @classmethod
#     def unpack(cls, data):
#         length, type = cls.rtattr.unpack(data[:cls.rtattr.size])
#         data_len = length-cls.rtattr.size
#         attr_data = struct.unpack('{0}s'.format(data_len),
#                              data[cls.rtattr.size:cls.rtattr.size+data_len])[0]
#         return cls(type, attr_data), data[align(length):]
#     
#     def __init__(self, rta_type, payload):
#         self.rta_type = rta_type
#         self.payload = payload
#         self.data = self.unpack_data()
#         
#     def pack(self):
#         data = struct.pack('{0}s'.format(len(self.payload)), self.payload)
#         rta_len = self.rtattr.size+len(data)
#         return struct.pack('{0}{1}s'.format(self.rtattr.format, align(len(data))),
#                            rta_len, self.rta_type, data)
#     
#     def unpack_data(self):
#         if hasattr(self, 'data_format') and hasattr(self, 'data_struct'):
#             return self.data_struct._make(self.data_format.unpack(self.payload))
#         return None
#     
#     def __repr__(self):
#         return """<rtattr datalen=%d rta_type=%d data="%s">""" % (
#             align(len(self.payload)), self.rta_type,
#             self.payload.encode('string_escape'))


# Path: shapy/framework/netlink/prio.py
from shapy.framework.netlink.message import Attr
from .constants import *
from struct import Struct
from collections import namedtuple

    #define TC_PRIO_INTERACTIVE     6
    #define TC_PRIO_CONTROL         7
    
    #define TC_PRIO_MAX         15
    
    #define TCQ_PRIO_BANDS  16
    #define TCQ_MIN_PRIO_BANDS 2
    
    #struct tc_prio_qopt {
    #    int bands;          /* Number of bands */
    #    __u8    priomap[TC_PRIO_MAX+1]; /* Map: logical priority -> PRIO band */
    #};

    data_format = Struct("i16B4x")
    data_struct = namedtuple('tc_prio_qopt', "bands priomap")
    
    @classmethod
    def unpack(cls, data):
        attr, rest = Attr.unpack(data)
        data = self.data_format.unpack(attr.payload)
        opts = cls.data_struct._make((data[0], data[1:]))
        return cls(opts.bands, opts.priomap), rest
    
    def unpack_data(self):
        data = self.data_format.unpack(self.payload)
        return self.data_struct._make(self.data_struct._make((data[0], data[1:])))
    
    def __init__(self, bands=3, priomap="1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1"):
        pm = [ int(p) for p in priomap.split() ]
        data = self.data_format.pack(bands, *pm)

        Attr.__init__(self, TCA_OPTIONS, data)