Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from subprocess import run, Popen
  3. from re import search
  4. from time import sleep
  5.  
  6. # NOTE: you will need to install `brightness` utility
  7. # `brew install brightness`
  8.  
  9.  
  10. BRIGHTNESS_STEP = 0.2
  11. MAX_BRIGHTNESS = 1
  12. MIN_BRIGHTNESS = 0
  13.  
  14. POMODORO_ITERATIONS = 3
  15. POMODORO_WORK_TIME = 0.3 * 60
  16. POMODORO_SWITCH_TIME = 0.3 * 60
  17. POMODORO_REST_TIME = 0.3 * 60
  18.  
  19.  
  20. # System utils
  21. def execBackground(command, options):
  22. return Popen([command] + options)
  23.  
  24.  
  25. def execSync(command, options):
  26. result = run(
  27. [command] + options, capture_output=True
  28. )
  29. if result.returncode:
  30. exit(result.returncode)
  31. return result
  32.  
  33.  
  34. def lockComputer():
  35. execSync('pmset', ['displaysleepnow'])
  36.  
  37.  
  38. def wakeComputer():
  39. execBackground('caffeinate', ['-u', '-t', '10'])
  40.  
  41.  
  42. def notifyUser(message):
  43. execSync(
  44. 'osascript',
  45. ['-e', 'display notification "%s" with title "Pymodoro"' % message],
  46. )
  47.  
  48.  
  49. # Brightness utils
  50. def getCurrentBrightness():
  51. brightness_call = execSync('brightness', ['-l'])
  52. brightness_output = brightness_call.stdout.decode('utf8')
  53. brightness = search("brightness ([0-9.]+)", brightness_output).group(1)
  54. return float(brightness)
  55.  
  56.  
  57. def updateBrightness(birghtness_step):
  58. current_brightness = getCurrentBrightness()
  59. calc_brightness = current_brightness + birghtness_step
  60. next_brightness = max(min(calc_brightness, MAX_BRIGHTNESS), MIN_BRIGHTNESS)
  61. execSync('brightness', ['-d', '0', str(next_brightness)])
  62.  
  63.  
  64. def main():
  65. for _ in range(POMODORO_ITERATIONS):
  66. notifyUser('It\'s time to work hard')
  67. sleep(POMODORO_WORK_TIME)
  68. notifyUser('Hey man, you need to go rest soon, wrap it up')
  69. updateBrightness(-BRIGHTNESS_STEP)
  70. sleep(POMODORO_SWITCH_TIME)
  71. notifyUser('5')
  72. sleep(1)
  73. notifyUser('4')
  74. sleep(1)
  75. notifyUser('3')
  76. sleep(1)
  77. notifyUser('2')
  78. sleep(1)
  79. notifyUser('1')
  80. sleep(1)
  81. updateBrightness(BRIGHTNESS_STEP)
  82. lockComputer()
  83. sleep(POMODORO_REST_TIME)
  84. wakeComputer()
  85. notifyUser('Go and get some reeeealy good rest')
  86.  
  87.  
  88. if (__name__ == "__main__"):
  89. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement