Skip to content

Commit

Permalink
Catch keyboard interrupts (#60)
Browse files Browse the repository at this point in the history
* catch keyboard interrupts

* catch interrupt on new input prompts
  • Loading branch information
Badgie authored Mar 10, 2020
1 parent 7068a61 commit 18acab4
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions spotirec.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,11 @@ def print_choices(data=None, prompt=True, sort=False) -> str:
continue
print(line.strip('\n'))
if prompt:
input_string = input('Enter integer identifiers for 1-5 whitespace separated selections that you wish to '
'include [default: top 5]:\n') or '0 1 2 3 4'
try:
input_string = input('Enter integer identifiers for 1-5 whitespace separated selections that you wish to '
'include [default: top 5]:\n') or '0 1 2 3 4'
except KeyboardInterrupt:
exit(0)
# If seed type is genres, simply parse the seed, else return the input for further processing
if 'genres' in rec.seed_type:
parse_seed_info([data[int(x)] for x in input_string.strip(' ').split(' ')])
Expand Down Expand Up @@ -470,7 +473,10 @@ def save_device():
"""

def prompt_device_index() -> int:
ind = input('Select a device by index[0]: ') or 0
try:
ind = input('Select a device by index[0]: ') or 0
except KeyboardInterrupt:
exit(0)
try:
assert devices[int(ind)] is not None
return int(ind)
Expand All @@ -480,8 +486,11 @@ def prompt_device_index() -> int:
return prompt_device_index()

def prompt_name() -> str:
inp = input('Enter an identifier for your device: ')
try:
try:
inp = input('Enter an identifier for your device: ')
except KeyboardInterrupt:
exit(0)
assert inp
assert ' ' not in inp
return inp
Expand Down Expand Up @@ -539,7 +548,10 @@ def save_playlist():
"""

def input_id() -> str:
iden = input('Please input an identifier for your playlist: ')
try:
iden = input('Please input an identifier for your playlist: ')
except KeyboardInterrupt:
exit(0)
try:
assert iden
assert ' ' not in iden
Expand All @@ -550,7 +562,10 @@ def input_id() -> str:
return input_id()

def input_uri() -> str:
uri = input('Please input the URI for your playlist: ')
try:
uri = input('Please input the URI for your playlist: ')
except KeyboardInterrupt:
exit(0)
try:
assert uri
assert re.match(PLAYLIST_URI_RE, uri)
Expand Down Expand Up @@ -854,8 +869,12 @@ def parse():
rec.based_on = 'custom mix'
rec.seed_type = 'custom'
print_choices(data=get_user_top_genres(), prompt=False, sort=True)
user_input = input('Enter a combination of 1-5 whitespace separated genre names, track uris, and artist uris. '
'\nGenres with several words should be connected with dashes, e.g.; vapor-death-pop.\n')
try:
user_input = input(
'Enter a combination of 1-5 whitespace separated genre names, track uris, and artist uris. '
'\nGenres with several words should be connected with dashes, e.g.; vapor-death-pop.\n')
except KeyboardInterrupt:
exit(0)
if not user_input:
print('Please enter 1-5 seeds')
exit(1)
Expand Down

0 comments on commit 18acab4

Please sign in to comment.