summaryrefslogtreecommitdiffstats
path: root/python/netlink/util.py
blob: 0f2e54759b93271cf78d8056f6a46d88bf7098fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#
# Utilities
#
# Copyright (c) 2011 Thomas Graf <tgraf@suug.ch>
#

"""utility module for netlink

"""

from __future__ import absolute_import

from . import core as netlink
from . import capi as capi
from string import Formatter
import types

__version__ = '1.0'

def _color(t, c):
    return b'{esc}[{color}m{text}{esc}[0m'.format(esc=b'\x1b', color=c, text=t)

def black(t):
    return _color(t, 30)

def red(t):
    return _color(t, 31)

def green(t):
    return _color(t, 32)

def yellow(t):
    return _color(t, 33)

def blue(t):
    return _color(t, 34)

def magenta(t):
    return _color(t, 35)

def cyan(t):
    return _color(t, 36)

def white(t):
    return _color(t, 37)

def bold(t):
    return _color(t, 1)

def kw(t):
    return yellow(t)

def num(t):
    return str(t)

def string(t):
    return t

def addr(t):
    return str(t)

def bad(t):
    return red(t)

def good(t):
    return green(t)

def title(t):
    return t

def boolean(t):
    return str(t)

def handle(t):
    return str(t)

class MyFormatter(Formatter):
    def __init__(self, obj, indent=''):
        self._obj = obj
        self._indent = indent

    def _nlattr(self, key):
        value = getattr(self._obj, key)
        title_ = None

        if isinstance(value, types.MethodType):
            value = value()

        try:
            d = netlink.attrs[self._obj._name + '.' + key]

            if 'fmt' in d:
                value = d['fmt'](value)

            if 'title' in d:
                title_ = d['title']
        except KeyError:
            pass
        except AttributeError:
            pass

        return title_, str(value)

    def get_value(self, key, args, kwds):
        # Let default get_value() handle ints
        if not isinstance(key, str):
            return Formatter.get_value(self, key, args, kwds)

        # HACK, we allow defining strings via fields to allow
        # conversions
        if key[:2] == 's|':
            return key[2:]

        if key[:2] == 't|':
            # title mode ("TITLE ATTR")
            include_title = True
        elif key[:2] == 'a|':
            # plain attribute mode ("ATTR")
            include_title = False
        else:
            # No special field, have default get_value() get it
            return Formatter.get_value(self, key, args, kwds)

        key = key[2:]
        (title_, value) = self._nlattr(key)

        if include_title:
            if not title_:
                title_ = key    # fall back to key as title
            value = '{0} {1}'.format(kw(title_), value)

        return value

    def convert_field(self, value, conversion):
        if conversion == 'r':
            return repr(value)
        elif conversion == 's':
            return str(value)
        elif conversion == 'k':
            return kw(value)
        elif conversion == 'b':
            return bold(value)
        elif conversion is None:
            return value

        raise ValueError('Unknown converion specifier {0!s}'.format(conversion))

    def nl(self, format_string=''):
        return '\n' + self._indent + self.format(format_string)

NL_BYTE_RATE = 0
NL_BIT_RATE = 1

class Rate(object):
    def __init__(self, rate, mode=NL_BYTE_RATE):
        self._rate = rate
        self._mode = mode

    def __str__(self):
        return capi.nl_rate2str(self._rate, self._mode, 32)[1]

    def __int__(self):
        return self._rate

    def __cmp__(self, other):
        return int(self) - int(other)

class Size(object):
    def __init__(self, size):
        self._size = size

    def __str__(self):
        return capi.nl_size2str(self._size, 32)[0]

    def __int__(self):
        return self._size

    def __cmp__(self, other):
        return int(self) - int(other)