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