Add packaging directory
[platform/upstream/libnl3.git] / python / netlink / util.py
1 #
2 # Utilities
3 #
4 # Copyright (c) 2011 Thomas Graf <tgraf@suug.ch>
5 #
6
7 """utility module for netlink
8
9 """
10
11 from __future__ import absolute_import
12
13 from . import core as netlink
14 from . import capi as capi
15 from string import Formatter
16 import types
17
18 __version__ = '1.0'
19
20 def _color(t, c):
21     return b'{esc}[{color}m{text}{esc}[0m'.format(esc=b'\x1b', color=c, text=t)
22
23 def black(t):
24     return _color(t, 30)
25
26 def red(t):
27     return _color(t, 31)
28
29 def green(t):
30     return _color(t, 32)
31
32 def yellow(t):
33     return _color(t, 33)
34
35 def blue(t):
36     return _color(t, 34)
37
38 def magenta(t):
39     return _color(t, 35)
40
41 def cyan(t):
42     return _color(t, 36)
43
44 def white(t):
45     return _color(t, 37)
46
47 def bold(t):
48     return _color(t, 1)
49
50 def kw(t):
51     return yellow(t)
52
53 def num(t):
54     return str(t)
55
56 def string(t):
57     return t
58
59 def addr(t):
60     return str(t)
61
62 def bad(t):
63     return red(t)
64
65 def good(t):
66     return green(t)
67
68 def title(t):
69     return t
70
71 def boolean(t):
72     return str(t)
73
74 def handle(t):
75     return str(t)
76
77 class MyFormatter(Formatter):
78     def __init__(self, obj, indent=''):
79         self._obj = obj
80         self._indent = indent
81
82     def _nlattr(self, key):
83         value = getattr(self._obj.__class__, key)
84         if not isinstance(value, property):
85             raise ValueError('Invalid formatting string {0}'.format(key))
86
87         d = getattr(value.fget, 'formatinfo', dict())
88
89         # value = value.fget() is exactly the same
90         value = getattr(self._obj, key)
91
92         if 'fmt' in d:
93             value = d['fmt'](value)
94
95         title_ = d.get('title', None)
96
97         return title_, str(value)
98
99     def get_value(self, key, args, kwds):
100         # Let default get_value() handle ints
101         if not isinstance(key, str):
102             return Formatter.get_value(self, key, args, kwds)
103
104         # HACK, we allow defining strings via fields to allow
105         # conversions
106         if key[:2] == 's|':
107             return key[2:]
108
109         if key[:2] == 't|':
110             # title mode ("TITLE ATTR")
111             include_title = True
112         elif key[:2] == 'a|':
113             # plain attribute mode ("ATTR")
114             include_title = False
115         else:
116             # No special field, have default get_value() get it
117             return Formatter.get_value(self, key, args, kwds)
118
119         key = key[2:]
120         (title_, value) = self._nlattr(key)
121
122         if include_title:
123             if not title_:
124                 title_ = key    # fall back to key as title
125             value = '{0} {1}'.format(kw(title_), value)
126
127         return value
128
129     def convert_field(self, value, conversion):
130         if conversion == 'r':
131             return repr(value)
132         elif conversion == 's':
133             return str(value)
134         elif conversion == 'k':
135             return kw(value)
136         elif conversion == 'b':
137             return bold(value)
138         elif conversion is None:
139             return value
140
141         raise ValueError('Unknown converion specifier {0!s}'.format(conversion))
142
143     def nl(self, format_string=''):
144         return '\n' + self._indent + self.format(format_string)
145
146 NL_BYTE_RATE = 0
147 NL_BIT_RATE = 1
148
149 class Rate(object):
150     def __init__(self, rate, mode=NL_BYTE_RATE):
151         self._rate = rate
152         self._mode = mode
153
154     def __str__(self):
155         return capi.nl_rate2str(self._rate, self._mode, 32)[1]
156
157     def __int__(self):
158         return self._rate
159
160     def __cmp__(self, other):
161         return int(self) - int(other)
162
163 class Size(object):
164     def __init__(self, size):
165         self._size = size
166
167     def __str__(self):
168         return capi.nl_size2str(self._size, 32)[0]
169
170     def __int__(self):
171         return self._size
172
173     def __cmp__(self, other):
174         return int(self) - int(other)