• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #rename into colored_output
21 def _color(t, c):
22     return '{esc}[{color}m{text}{esc}[0m'.format(esc=b'\x1b'.decode(), color=c, text=t)
23 
24 def black(t):
25     return _color(t, 30)
26 
27 def red(t):
28     return _color(t, 31)
29 
30 def green(t):
31     return _color(t, 32)
32 
33 def yellow(t):
34     return _color(t, 33)
35 
36 def blue(t):
37     return _color(t, 34)
38 
39 def magenta(t):
40     return _color(t, 35)
41 
42 def cyan(t):
43     return _color(t, 36)
44 
45 def white(t):
46     return _color(t, 37)
47 
48 def bold(t):
49     return _color(t, 1)
50 
51 def kw(t):
52     return yellow(t)
53 
54 def num(t):
55     return str(t)
56 
57 def string(t):
58     return t
59 
60 def addr(t):
61     return str(t)
62 
63 def bad(t):
64     return red(t)
65 
66 def good(t):
67     return green(t)
68 
69 def title(t):
70     return t
71 
72 def boolean(t):
73     return str(t)
74 
75 def handle(t):
76     return str(t)
77 
78 class 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 
147 NL_BYTE_RATE = 0
148 NL_BIT_RATE = 1
149 
150 class 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 
164 class 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)
176