Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1try: 

2 import netifaces 

3except ImportError: 

4 netifaces = None 

5from pi3bar.plugins.base import Plugin 

6 

7 

8class NetIFace(Plugin): 

9 """ 

10 :class:`pi3bar.app.Pi3Bar` plugin to show your current IP address. 

11 

12 .. Note:: requires `netifaces`_ (install with ``pip install netifaces``) 

13 

14 .. _netifaces: https://pypi.python.org/pypi/netifaces 

15 

16 :param interface: :class:`str` - Network interface name to watch. 

17 :param connected_color: :class:`str` - Hex color to use when connected. (default: '#00ff00') 

18 :param connected_background: :class:`str` - Hex background color to use when connected. (default: None) 

19 :param disconnected_color: :class:`str` - Hex color to use when disconnected. (default: None) 

20 :param disconnected_background: :class:`str` - Hex background color to use when disconnected. (default: None) 

21 

22 Examples: 

23 

24 .. code-block:: python 

25 

26 NetIface('eth0') 

27 """ 

28 

29 #: Refresh every 5 seconds 

30 ticks = 5 

31 

32 def __init__(self, interface, **kwargs): 

33 self.instance = interface 

34 self.interface = interface 

35 self.short_text = interface 

36 self.connected_color = kwargs.pop('connected_color', '#00ff00') 

37 self.connected_background = kwargs.pop('connected_background', None) 

38 self.disconnected_color = kwargs.pop('disconnected_color', None) 

39 self.disconnected_background = kwargs.pop('disconnected_background', None) 

40 super(NetIFace, self).__init__(**kwargs) 

41 

42 def cycle(self): 

43 if netifaces is None: 

44 self.error('netifaces is not installed') 

45 self.full_text = 'not installed' 

46 self.color = None 

47 self.background = '#ff0000' 

48 return 

49 

50 interfaces = netifaces.interfaces() 

51 

52 if self.interface not in interfaces: 

53 self.error("'%s' not found in %s" % (self.interface, interfaces)) 

54 self.full_text = '%s not found' % (self.interface) 

55 self.color = None 

56 self.background = '#ff0000' 

57 return 

58 

59 addresses = netifaces.ifaddresses(self.interface) 

60 

61 try: 

62 addresses = addresses[netifaces.AF_INET] 

63 except KeyError: 

64 self.color = self.disconnected_color 

65 self.background = self.disconnected_background 

66 self.full_text = self.interface 

67 return 

68 

69 ips = ', '.join([address['addr'] for address in addresses]) 

70 

71 self.color = self.connected_color 

72 self.background = self.connected_background 

73 self.full_text = '%s %s' % (self.interface, ips) 

74