Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.44 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. #       shutdown timer version 1.0 stable
  5. #      
  6. #       Copyright 2009 Aneesh <aneesh.nl@gmail.com>
  7. #      
  8. #       This program is free software; you can redistribute it and/or modify
  9. #       it under the terms of the GNU General Public License as published by
  10. #       the Free Software Foundation; either version 2 of the License, or
  11. #       (at your option) any later version.
  12. #      
  13. #       This program is distributed in the hope that it will be useful,
  14. #       but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #       GNU General Public License for more details.
  17. #      
  18. #       You should have received a copy of the GNU General Public License
  19. #       along with this program; if not, write to the Free Software
  20. #       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. #       MA 02110-1301, USA.
  22.  
  23. '''
  24. Shut down the system after specified time.
  25. usage : shutdown.py <hours> <minutes> <seconds>
  26. This currently works only on GNU/Linux
  27. '''
  28. import time, sys, os
  29.  
  30. def clearscreen(numlines=100):
  31.     '''
  32.    Clears the screen.
  33.    This function clears the screen according to OS of the user.
  34.    Credits : Balamurugan S balamurugan.sekar <at> gmail <dot> com.
  35.    '''
  36.     if os.name == 'posix': # eg : Unix or GNU/Linux
  37.         os.system('clear')
  38.     elif os.name in ('nt', 'dos', 'ce'): # Windows, Dos etc.
  39.         os.system('cls')
  40.     else:
  41.         # Other OS. New line 100 times, in effect clear the screen.
  42.         print('\n' * numlines)
  43.  
  44. def main():
  45.     '''Main function
  46.    calculate the variables hours, minutes, seconds from
  47.    command line arguments and pass to timer().
  48.    After timer() has compleated, it runs the command 'init 0' to
  49.    shutdown system.'''
  50.     try:
  51.         hours   = int(sys.argv[1]) # This 3 lines calculates hours,
  52.         minutes = int(sys.argv[2]) # minutes, seconds from command
  53.         seconds = int(sys.argv[3]) # line arguments
  54.     except:
  55.         print('''
  56. Usage : shutdowntimer <hours> <minutes> <seconds>
  57.  
  58. Copyright 2009 Aneesh <aneesh.nl@gmail.com>
  59. For more details, view licence''')
  60.         exit()
  61.     # provides time delay and prints out time remaining.
  62.     timer(hours, minutes, seconds)
  63.     print('Shuting down ...')
  64.     os.system('init 0') # shut down the system
  65.     return 0
  66.  
  67. def timer(hours, minutes, seconds):
  68.     '''Provides time delay
  69.    Accepts hours, minutes and seconds as int.
  70.    Prints the remaining time in each second.
  71.    '''
  72.     try:
  73.         while True:
  74.             clearscreen()
  75.             print('Shutting down in {0} hours {1} minutes {2} seconds'\
  76.         .format(hours, minutes, seconds))
  77.             # provides a time delay of 1 second
  78.             time.sleep(1)
  79.             # if...elif...else ladder.
  80.             if seconds > 0:
  81.                 seconds -= 1  # decrement seconds
  82.             elif minutes > 0: # when seconds = 0, this condition become true
  83.                 minutes -= 1  # decement minutes
  84.                 seconds = 59  # reset seconds
  85.             elif hours > 0:   # when seconds = 0 & minutes =0,
  86.                               # this condition become true
  87.                 hours -=1     # decrement hours
  88.                 minutes = 59  # reset minutes
  89.             else: # if hours, minutes & seconds = 0
  90.                 return None # exit from function
  91.  
  92.     except KeyboardInterrupt:
  93.         print('''
  94. You cancelled the shutdown.. Thanks for using.
  95.      
  96.       shutdowntimer version 1.0 stable
  97.      
  98.       Copyright 2009 Aneesh <aneesh.nl@gmail.com>
  99.      
  100.       This program is free software; you can redistribute it and/or modify
  101.       it under the terms of the GNU General Public License as published by
  102.       the Free Software Foundation; either version 3 of the License, or
  103.       (at your option) any later version.
  104.      
  105.       This program is distributed in the hope that it will be useful,
  106.       but WITHOUT ANY WARRANTY; without even the implied warranty of
  107.       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  108.       GNU General Public License for more details.
  109.      
  110.       You should have received a copy of the GNU General Public License
  111.       along with this program; if not, write to the Free Software
  112.       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  113.       MA 02110-1301, USA.''')
  114.         exit()
  115.  
  116. if __name__ == '__main__':
  117.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement