Skip to content

Commit

Permalink
chore: add better excpetion for prompts run in automation where no in…
Browse files Browse the repository at this point in the history
…put can be given
  • Loading branch information
robcxyz committed Dec 12, 2022
1 parent b310d52 commit 93f5020
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 10 deletions.
21 changes: 19 additions & 2 deletions tackle/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,27 @@ def __init__(self, extra_message: str, context: 'Union[Context, BaseContext]'):

class HookCallException(TackleParserException):
"""
Exception for an unknown field in a hook.
Exception when calling a hook.
Raised when field has been provided not declared in the hook type.
Raised within a python hook.
"""


class PromptHookCallException(TackleParserException):
"""
Exception when calling a prompt hook typically in automation.
Raised within a python hook.
"""

def __init__(self, context: 'Union[Context, BaseContext]'):
super().__init__(
extra_message="Error calling hook most likely due to hook being called in "
"automation where no input was given for key. Try setting "
"the key with an `override` if that is the case. Check: "
"https://sudoblockio.github.io/tackle/testing-providers/#testing-tackle-scripts", # noqa
context=context,
)


class HookParseException(TackleParserException):
Expand Down
5 changes: 4 additions & 1 deletion tackle/providers/prompts/hooks/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

from tackle.models import BaseHook, Field
from tackle.utils.dicts import get_readable_key_path
from tackle import exceptions


class InquirerCheckboxHook(BaseHook):
"""
Hook for PyInquirer `checkbox` type prompts. Allows the user to multi-select from a
list of choices and outputs a list. Takes in three forms of `choices` inputs. A
list of string, a list of maps with all keys having a `name` field per the original
[spec](https://github.com/CITGuru/PyInquirer/blob/master/examples/checkbox.py), or
[spec](https://github.com/kazhala/InquirerPy/blob/master/examples/checkbox.py), or
list of maps with the key as the question, the value as the output.
"""

Expand Down Expand Up @@ -145,4 +146,6 @@ def _run_prompt(self):
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
except EOFError:
raise exceptions.PromptHookCallException(context=self)
return response['tmp']
5 changes: 4 additions & 1 deletion tackle/providers/prompts/hooks/confirm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

from tackle.models import BaseHook, Field
from tackle.utils.dicts import get_readable_key_path
from tackle import exceptions


class InquirerConfirmHook(BaseHook):
"""
Hook to confirm with a message and return a boolean.
[Source example](https://github.com/CITGuru/PyInquirer/blob/master/examples/confirm.py)
[Source example](https://github.com/kazhala/InquirerPy/blob/master/examples/confirm.py)
"""

hook_type: str = 'confirm'
Expand Down Expand Up @@ -37,6 +38,8 @@ def exec(self) -> bool:
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
except EOFError:
raise exceptions.PromptHookCallException(context=self)
return response['tmp']

elif self.default:
Expand Down
5 changes: 4 additions & 1 deletion tackle/providers/prompts/hooks/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

from tackle.models import BaseHook, Field
from tackle.utils.dicts import get_readable_key_path
from tackle import exceptions


class InquirerEditorHook(BaseHook):
"""
Hook for PyInquirer `editor` type prompts. Opens an editor like nano to fill in a
field. [Source example](https://github.com/CITGuru/PyInquirer/blob/master/examples/editor.py)
field. [Source example](https://github.com/kazhala/InquirerPy/blob/master/examples/editor.py)
"""

hook_type: str = 'editor'
Expand All @@ -37,6 +38,8 @@ def exec(self) -> bool:
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
except EOFError:
raise exceptions.PromptHookCallException(context=self)
return response['tmp']

elif self.default:
Expand Down
5 changes: 4 additions & 1 deletion tackle/providers/prompts/hooks/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from typing import Any
from tackle.models import BaseHook, Field
from tackle.utils.dicts import get_readable_key_path
from tackle import exceptions


class InquirerExpandHook(BaseHook):
"""
Hook for PyInquirer `expand` type prompt.
[Source example](https://github.com/CITGuru/PyInquirer/blob/master/examples/expand.py)
[Source example](https://github.com/kazhala/InquirerPy/blob/master/examples/expand.py)
"""

hook_type: str = 'expand'
Expand Down Expand Up @@ -38,6 +39,8 @@ def exec(self) -> list:
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
except EOFError:
raise exceptions.PromptHookCallException(context=self)
return response['tmp']

elif self.default:
Expand Down
5 changes: 4 additions & 1 deletion tackle/providers/prompts/hooks/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from typing import Any
from tackle.models import BaseHook, Field
from tackle.utils.dicts import get_readable_key_path
from tackle import exceptions


class InquirerInputHook(BaseHook):
"""
Hook for PyInquirer 'input' type prompts. Allows the user to input a string input.
[Source example](https://github.com/CITGuru/PyInquirer/blob/master/examples/input.py)
[Source example](https://github.com/kazhala/InquirerPy/blob/master/examples/input.py)
"""

hook_type: str = 'input'
Expand Down Expand Up @@ -42,6 +43,8 @@ def exec(self) -> str:
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
except EOFError:
raise exceptions.PromptHookCallException(context=self)
return response['tmp']

elif self.default:
Expand Down
5 changes: 4 additions & 1 deletion tackle/providers/prompts/hooks/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

from tackle.models import BaseHook, Field
from tackle.utils.dicts import get_readable_key_path
from tackle import exceptions


class InquirerPasswordHook(BaseHook):
"""
Hook for PyInquirer `password` type prompts. Masks the input as the user types it
in. [Source example](https://github.com/CITGuru/PyInquirer/blob/master/examples/password.py)
in. [Source example](https://github.com/kazhala/InquirerPy/blob/master/examples/password.py)
"""

hook_type: str = 'password'
Expand Down Expand Up @@ -39,6 +40,8 @@ def exec(self) -> str:
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
except EOFError:
raise exceptions.PromptHookCallException(context=self)
return response['tmp']

elif self.default:
Expand Down
5 changes: 4 additions & 1 deletion tackle/providers/prompts/hooks/rawlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from typing import Any
from tackle.models import BaseHook, Field
from tackle.utils.dicts import get_readable_key_path
from tackle import exceptions


class InquirerRawListHook(BaseHook):
"""
Hook for PyInquirer 'rawlist' type prompts. Similar to `select` hook with less
flexibility. [Source example](https://github.com/CITGuru/PyInquirer/blob/master/examples/rawlist.py)
flexibility. [Source example](https://github.com/kazhala/InquirerPy/blob/master/examples/rawlist.py)
"""

hook_type: str = 'rawlist'
Expand Down Expand Up @@ -40,6 +41,8 @@ def exec(self) -> list:
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
except EOFError:
raise exceptions.PromptHookCallException(context=self)
return response['tmp']

elif self.default:
Expand Down
5 changes: 4 additions & 1 deletion tackle/providers/prompts/hooks/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

from tackle.models import BaseHook, Field
from tackle.utils.dicts import get_readable_key_path
from tackle import exceptions


class InquirerListHook(BaseHook, smart_union=True):
"""
Hook for PyInquirer 'list' type prompts, a single selector that returns a string.
Takes in two forms of `choices` inputs, list of string or list of maps with the
key as the question and the value as the output.
[Source example](https://github.com/CITGuru/PyInquirer/blob/master/examples/list.py)
[Source example](https://github.com/kazhala/InquirerPy/blob/master/examples/list.py)
"""

hook_type: str = 'select'
Expand Down Expand Up @@ -81,6 +82,8 @@ def _run_prompt(self):
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
except EOFError:
raise exceptions.PromptHookCallException(context=self)
return response['tmp']

elif isinstance(self.choices[0], str):
Expand Down

0 comments on commit 93f5020

Please sign in to comment.