Debian Codegraph @

python/netlink/util.py

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