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

1import os 

2from pi3bar.plugins.base import Plugin 

3from pi3bar.utils import humanize_size_bytes 

4 

5 

6class Disk(Plugin): 

7 """ 

8 :class:`pi3bar.app.Pi3Bar` plugin to disk usage. 

9 

10 Available format replacements (``*_p`` = percentage): 

11 

12 * ``%(size)s`` E.g. '100GB' 

13 * ``%(free)s`` E.g. '70GB' 

14 * ``%(free_p)f`` E.g. 70.0 

15 * ``%(available)s`` E.g. '65GB' 

16 * ``%(available_p)f`` E.g. 65.0 

17 * ``%(usage)s`` E.g. '30GB' 

18 * ``%(usage_p)f`` E.g. 30.0 

19 

20 :param full_format: :class:`str` - Format string (default: '%(usage_p).2f%% (%(size)s)') 

21 :param short_format: :class:`str` - Short format string (default: '%(usage_p).2f%%') 

22 :param warning_usage: :class:`int` - Warning breakpoint (default: 90) 

23 :param warning_color: :class:`str` - Warning color (default: '#ffff00') 

24 :param warning_background: :class:`str` - Warning background color (default: None) 

25 :param critical_usage: :class:`int` - Critical breakpoint (default: 95) 

26 :param critical_color: :class:`str` - Critical color (default: None) 

27 :param critical_background: :class:`str` - Critical background color (default: '#ff0000') 

28 

29 Examples: 

30 

31 .. code-block:: python 

32 

33 # root 

34 Disk('/') 

35 

36 # custom format (escape '%' with '%') 

37 Disk('/', full_format='%(usage)s / %(size)s', short_format='%(free_p)f%%') 

38 

39 # adjust warning/critical switches 

40 Disk('/mnt', warning_usage=80, critical_usage=90) 

41 """ 

42 def __init__(self, mount_path, **kwargs): 

43 self.instance = mount_path 

44 self.mount_path = mount_path 

45 

46 self.full_format = kwargs.pop('full_format', '%(usage_p).2f%% (%(size)s)') 

47 self.short_format = kwargs.pop('short_format', '%(usage_p).2f%%') 

48 

49 self.warning_usage = kwargs.pop('warning_usage', 90) 

50 self.warning_color = kwargs.pop('warning_color', '#ffff00') 

51 self.warning_background = kwargs.pop('warning_background', None) 

52 

53 self.critical_usage = kwargs.pop('critical_usage', 95) 

54 self.critical_color = kwargs.pop('critical_color', None) 

55 self.critical_background = kwargs.pop('critical_background', '#ff0000') 

56 

57 super(Disk, self).__init__(**kwargs) 

58 

59 def get_stats(self): 

60 statvfs = os.statvfs(self.mount_path) 

61 

62 size_bytes = statvfs.f_frsize * statvfs.f_blocks 

63 

64 free_bytes = statvfs.f_frsize * statvfs.f_bfree # with reserved space 

65 free_percent = 100.0 / size_bytes * free_bytes 

66 

67 available_bytes = statvfs.f_frsize * statvfs.f_bavail # without reserved space 

68 available_percent = 100.0 / size_bytes * available_bytes 

69 

70 usage_bytes = size_bytes - free_bytes 

71 usage_percent = 100.0 / size_bytes * usage_bytes 

72 

73 return { 

74 'size': humanize_size_bytes(size_bytes), # 100GB 

75 'free': humanize_size_bytes(free_bytes), # 70GB 

76 'available': humanize_size_bytes(available_bytes), # 65GB 

77 'usage': humanize_size_bytes(usage_bytes), # 30GB 

78 'free_p': free_percent, # 70.0 

79 'available_p': available_percent, # 65.0 

80 'usage_p': usage_percent, # 30.0 

81 } 

82 

83 def cycle(self): 

84 stats = self.get_stats() 

85 prefix = '%s ' % self.mount_path 

86 self.full_text = prefix + self.full_format % stats 

87 self.short_text = prefix + self.short_format % stats 

88 

89 if float(stats['usage_p']) > self.critical_usage: 

90 self.color = self.critical_color 

91 self.background = self.critical_background 

92 elif float(stats['usage_p']) > self.warning_usage: 

93 self.color = self.warning_color 

94 self.background = self.warning_background 

95 else: 

96 self.color = None 

97 self.background = None