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 datetime 

2from pi3bar.plugins.base import Plugin 

3from pi3bar.utils import split_seconds 

4 

5 

6def get_uptime_seconds(): 

7 with open('/proc/uptime', 'r') as f: 

8 return float(f.readline().split()[0]) 

9 

10 

11def uptime_format(seconds, fmt='%d:%H:%M:%S'): 

12 uptime = datetime.timedelta(seconds=seconds) 

13 

14 hours, minutes, seconds = split_seconds(uptime.seconds) 

15 

16 if '%d' in fmt: 

17 fmt = fmt.replace('%d', str(uptime.days)) 

18 else: 

19 hours += 24 * uptime.days 

20 

21 if '%H' in fmt: 

22 fmt = fmt.replace('%H', '%02d' % hours) 

23 else: 

24 minutes += 60 * hours 

25 

26 fmt = fmt.replace('%M', '%02d' % minutes) 

27 fmt = fmt.replace('%S', '%02d' % seconds) 

28 

29 return fmt 

30 

31 

32class Uptime(Plugin): 

33 """ 

34 :class:`pi3bar.app.Pi3Bar` plugin to show uptime. 

35 

36 Available format replacements: 

37 

38 * ``%d`` : days 

39 * ``%H`` : hours 

40 * ``%M`` : minutes 

41 * ``%S`` : seconds 

42 

43 :param full_format: :class:`str` - Format string (default: '%d days %H:%M:%S up') 

44 :param short_format: :class:`str` - Short format string (default: '%dd %H:%M up') 

45 

46 Examples: 

47 

48 .. code-block:: python 

49 

50 Uptime(full_format='%d days %H:%M:%S up', short_format='%dd %H:%M up') 

51 """ 

52 

53 #: Refresh every second 

54 ticks = 1 

55 

56 def __init__(self, full_format='%d days %H:%M:%S up', short_format='%dd %H:%M up', **kwargs): 

57 self.full_format = full_format 

58 self.short_format = short_format 

59 super(Uptime, self).__init__(**kwargs) 

60 

61 def cycle(self): 

62 uptime_seconds = get_uptime_seconds() 

63 self.full_text = uptime_format(uptime_seconds, self.full_format) 

64 self.short_text = uptime_format(uptime_seconds, self.short_format)