Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- #
- # shutdown timer version 1.0 stable
- #
- # Copyright 2009 Aneesh <aneesh.nl@gmail.com>
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- # MA 02110-1301, USA.
- '''
- Shut down the system after specified time.
- usage : shutdown.py <hours> <minutes> <seconds>
- This currently works only on GNU/Linux
- '''
- import time, sys, os
- def clearscreen(numlines=100):
- '''
- Clears the screen.
- This function clears the screen according to OS of the user.
- Credits : Balamurugan S balamurugan.sekar <at> gmail <dot> com.
- '''
- if os.name == 'posix': # eg : Unix or GNU/Linux
- os.system('clear')
- elif os.name in ('nt', 'dos', 'ce'): # Windows, Dos etc.
- os.system('cls')
- else:
- # Other OS. New line 100 times, in effect clear the screen.
- print('\n' * numlines)
- def main():
- '''Main function
- calculate the variables hours, minutes, seconds from
- command line arguments and pass to timer().
- After timer() has compleated, it runs the command 'init 0' to
- shutdown system.'''
- try:
- hours = int(sys.argv[1]) # This 3 lines calculates hours,
- minutes = int(sys.argv[2]) # minutes, seconds from command
- seconds = int(sys.argv[3]) # line arguments
- except:
- print('''
- Usage : shutdowntimer <hours> <minutes> <seconds>
- Copyright 2009 Aneesh <aneesh.nl@gmail.com>
- For more details, view licence''')
- exit()
- # provides time delay and prints out time remaining.
- timer(hours, minutes, seconds)
- print('Shuting down ...')
- os.system('init 0') # shut down the system
- return 0
- def timer(hours, minutes, seconds):
- '''Provides time delay
- Accepts hours, minutes and seconds as int.
- Prints the remaining time in each second.
- '''
- try:
- while True:
- clearscreen()
- print('Shutting down in {0} hours {1} minutes {2} seconds'\
- .format(hours, minutes, seconds))
- # provides a time delay of 1 second
- time.sleep(1)
- # if...elif...else ladder.
- if seconds > 0:
- seconds -= 1 # decrement seconds
- elif minutes > 0: # when seconds = 0, this condition become true
- minutes -= 1 # decement minutes
- seconds = 59 # reset seconds
- elif hours > 0: # when seconds = 0 & minutes =0,
- # this condition become true
- hours -=1 # decrement hours
- minutes = 59 # reset minutes
- else: # if hours, minutes & seconds = 0
- return None # exit from function
- except KeyboardInterrupt:
- print('''
- You cancelled the shutdown.. Thanks for using.
- shutdowntimer version 1.0 stable
- Copyright 2009 Aneesh <aneesh.nl@gmail.com>
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- MA 02110-1301, USA.''')
- exit()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement