-
-
Save mikkpr/4508999afd400d561480bfc650487b47 to your computer and use it in GitHub Desktop.
A slightly modified version of https://github.com/liviu-/ding (OS X only)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Simple CLI beep tool""" | |
from __future__ import unicode_literals | |
from __future__ import print_function | |
import os | |
import sys | |
import time | |
import datetime | |
import subprocess | |
EXIT_MSG = """Invalid arguments: {}\n--------- | |
$ ding at hh[:mm[:ss]] | |
$ ding in \d+[smh]( +\d+[smh])* | |
Examples: | |
$ ding at 15:30 | |
$ ding in 5m 30s | |
""" | |
VERSION = '1.2.0' | |
N_BEEPS = 1 | |
WAIT_BEEPS = 0.05 | |
class InvalidArguments(Exception): | |
pass | |
def check_input(args): | |
"""Validate user input""" | |
if len(args) < 2 or args[0] not in ['in', 'at']: | |
raise InvalidArguments('insufficient number of arguments') | |
if args[0] == 'in': | |
if not all(arg.endswith(('s', 'm', 'h')) for arg in args[1:]): | |
raise InvalidArguments('please use s/m/h suffixes') | |
if not all(arg[:-1].isdigit() for arg in args[1:]): | |
raise InvalidArguments('please use numbers for specifying the duration of time units') | |
if args[0] == 'at': | |
if len(args) > 2: | |
raise InvalidArguments('too many arguments') | |
if not all([arg.isdigit() for arg in args[1].split(':')]): | |
raise InvalidArguments('there should only be numbers optionally separated by ":"') | |
# Valid time | |
try: | |
datetime.time(*map(int, args[1].split(':'))) | |
except ValueError as e: | |
raise InvalidArguments(e) | |
return args | |
class TimeParser(): | |
"""Class helping with parsing user provided time into seconds""" | |
time_map = { | |
's': 1, | |
'm': 60, | |
'h': 60 * 60, | |
} | |
def __init__(self, time, relative): | |
self.time = time | |
self.relative = relative | |
def get_seconds(self): | |
return self._get_seconds_relative() if self.relative else self._get_seconds_absolute() | |
def _get_seconds_relative(self): | |
return sum([self.time_map[t[-1]] * int(t[:-1]) for t in self.time]) | |
def _get_seconds_absolute(self): | |
now = datetime.datetime.now() | |
user_time = (datetime.datetime.combine(datetime.date.today(), | |
datetime.time(*map(int, self.time[0].split(':'))))) | |
return ((user_time - now).seconds if user_time > now | |
else (user_time + datetime.timedelta(days=1) - now).seconds) | |
def print_time(seconds): | |
"""Print countdown for `seconds`""" | |
while seconds > 0: | |
start = time.time() | |
os.system('cls' if os.name == 'nt' else 'clear') # accommodate Windows | |
print(datetime.timedelta(seconds=seconds)) | |
seconds -= 1 | |
time.sleep(1 - time.time() + start) | |
def beep(seconds): | |
"""Make the beep noise""" | |
for _ in range(N_BEEPS): | |
subprocess.call(["afplay", "/System/Library/Sounds/Glass.aiff", "-v", "15"]) | |
time.sleep(WAIT_BEEPS) | |
def parse_time(args): | |
"""Figure out the number of seconds to wait""" | |
relative = True if args[0] == 'in' else False | |
user_time = args[1:] | |
parser = TimeParser(user_time, relative) | |
return parser.get_seconds() | |
def main(args=sys.argv[1:]): | |
if args and args[0] == '--version': | |
print(VERSION) | |
sys.exit() | |
try: | |
seconds = parse_time(check_input(args)) | |
except InvalidArguments as e: | |
sys.exit(EXIT_MSG.format(e)) | |
print_time(seconds) | |
beep(seconds) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIT License | |
Copyright (c) 2016 | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment