util.py 3.7 KB

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