Skip to content

Commit

Permalink
Build: Added PEP8 linting (fixes #37)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvaughan committed Nov 4, 2017
1 parent 53229a6 commit 04856cf
Show file tree
Hide file tree
Showing 6 changed files with 592 additions and 463 deletions.
8 changes: 7 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ end_of_line = lf
insert_final_newline = true

# Special cases for script and config files
[*.{js,py,yml,cfg,ini}]
[*.{py,js}]
charset = utf-8
indent_style = space
indent_size = 4

# Special cases for config files
[*.{yml,cfg,ini}]
charset = utf-8
indent_style = space
indent_size = 2
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dev-dependencies: dependencies
pip install -U -r requirements-dev.txt

lint: dev-dependencies lint-docstring
@echo "Code Linting Not Implemented"
pycodestyle --show-pep8 --show-source ./

lint-docstring: dev-dependencies
@echo "DocString Linting Not Implemented"
Expand Down
183 changes: 100 additions & 83 deletions action_plugins/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,122 +30,139 @@


class ActionModule(ActionBase):
"""
Prompts user with one-or-more messages and optionally waits for input for each.
"""
"""
Prompts user with one-or-more messages and optionally waits for input for each.
TRANSFERS_FILES = False
VALID_PARAMS = ["say"]
.. class:: ActionModule
.. versionadded:: 0.1.0
"""

TRANSFERS_FILES = False
VALID_PARAMS = ["say"]

def __init__(self, task, connection, play_context, loader, templar, shared_loader_obj):
"""
Initializes the prompt Ansible plugin.
"""
super(ActionModule, self).__init__(task, connection, play_context, loader, templar, shared_loader_obj)
self.setOutput(sys.stdout)

def __init__(self, task, connection, play_context, loader, templar, shared_loader_obj):
"""
Initializes the prompt Ansible plugin.
def run(self, tmp=None, task_vars=None):
"""
Performs the plugin task, prompting the user one or more times.
.. versionadded:: 0.1.0
.. function:: __init__(task, connection, play_context, loader, templar, shared_loader_obj)
"""
super(ActionModule, self).__init__(task, connection, play_context, loader, templar, shared_loader_obj)
self.setOutput(sys.stdout)

:kwarg tmp: the temporary directory to use if creating files
:kwarg task_vars: any variables associated with the task

:returns: a dictionary of results from the module
"""
if task_vars is None:
task_vars = dict()
def run(self, tmp=None, task_vars=None):
"""
Performs the plugin task, prompting the user one or more times.
result = super(ActionModule, self).run(tmp, task_vars)
args = self._task.args
:kwarg tmp: the temporary directory to use if creating files
:kwarg task_vars: any variables associated with the task
:returns: a dictionary of results from the module
# Expect only the messages parameter
if 'msg' not in args:
return self._fail(result, "Required 'msg' parameter missing.")
.. versionadded:: 0.1.0
.. function:: run([tmp=None, task_vars=None])
"""
if task_vars is None:
task_vars = dict()

if len(args) != 1:
return self._fail(result, "Expected single 'msg' parameter. Multiple parameters given.")
result = super(ActionModule, self).run(tmp, task_vars)
args = self._task.args

return self._prompt(result, args['msg'])
# Expect only the messages parameter
if 'msg' not in args:
return self._fail(result, "Required 'msg' parameter missing.")

if len(args) != 1:
return self._fail(result, "Expected single 'msg' parameter. Multiple parameters given.")

def setOutput(self, output=None):
"""
Sets the output stream to write to.
return self._prompt(result, args['msg'])

:kwarg output: an output stream to write to
"""
if output == None:
self.output = sys.stdout

else:
self.output = output
def setOutput(self, output=None):
"""
Sets the output stream to write to.
:kwarg output: an output stream to write to
def _prompt(self, result, msg):
"""
Prompts the user with a message and optionally asks for a response.
.. versionadded:: 0.1.0
.. function:: setOutput([output=None])
"""
if output is None:
self.output = sys.stdout

:kwarg result: the base result dict to build on
:kwarg msg: the message provided to parse (string, object, or list)
else:
self.output = output

:returns: an updated dict response with success or failure
"""
if not isinstance(msg, list):
msg = [msg]

if len(msg) == 0:
return self._fail(result, "No message provided")
def _prompt(self, result, msg):
"""
Prompts the user with a message and optionally asks for a response.
# Parse each item on the list
for m in msg:
:kwarg result: the base result dict to build on
:kwarg msg: the message provided to parse (string, object, or list)
if m != None and not isinstance(m, (str, dict)):
m = str(m)
:returns: an updated dict response with success or failure
# If no message is provided, fail
if m == None or len(m) == 0:
return self._fail(result, "No message provided")
.. versionadded:: 0.1.0
.. function:: _prompt(result, msg)
"""
if not isinstance(msg, list):
msg = [msg]

# If a simple scalar value is provided, simply display it
if not isinstance(m, dict):
self.output.write("%s\n" % m)
continue
if len(msg) == 0:
return self._fail(result, "No message provided")

# If this is a set of key/value pairs, parse it
for arg in m:
if arg not in self.VALID_PARAMS:
return self._fail(result, "Unexpected parameter '%s'" % arg)
# Parse each item on the list
for m in msg:

if 'say' in m:
self.output.write("%s\n" % m['say'])
if m is not None and not isinstance(m, (str, dict)):
m = str(m)

return result
# If no message is provided, fail
if m is None or len(m) == 0:
return self._fail(result, "No message provided")

# If a simple scalar value is provided, simply display it
if not isinstance(m, dict):
self.output.write("%s\n" % m)
continue

def _fail(self, result, message, *args):
"""
Raises an Ansible exception with a given message.
# If this is a set of key/value pairs, parse it
for arg in m:
if arg not in self.VALID_PARAMS:
return self._fail(result, "Unexpected parameter '%s'" % arg)

:kwarg result: the base result object to build on
:kwarg message: the message to pass to the Ansible exception
:kwarg args: an arbitrary number of arguments to replace in the message's formatting
if 'say' in m:
self.output.write("%s\n" % m['say'])

:returns: an updated dict response with the provided failure condition
"""
if not isinstance(result, dict):
raise TypeError("Invalid result provided. Expected dict, received %s." % type(result))
return result


def _fail(self, result, message, *args):
"""
Raises an Ansible exception with a given message.
:kwarg result: the base result object to build on
:kwarg message: the message to pass to the Ansible exception
:kwarg args: an arbitrary number of arguments to replace in the message's formatting
:returns: an updated dict response with the provided failure condition
.. versionadded:: 0.1.0
.. function:: _fail(result, message, *args)
"""
if not isinstance(result, dict):
raise TypeError("Invalid result provided. Expected dict, received %s." % type(result))

if not isinstance(message, str):
raise TypeError("Invalid message provided. Expected string, received '%s'." % message)
if not isinstance(message, str):
raise TypeError("Invalid message provided. Expected string, received '%s'." % message)

if message == "":
raise ValueError("Empty message provided. Requires failure message.")
if message == "":
raise ValueError("Empty message provided. Requires failure message.")

result['failed'] = True
result['msg'] = message % (args)
result['failed'] = True
result['msg'] = message % (args)

return result
return result
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage
pycodestyle
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pycodestyle]
max-line-length = 118
ignore=E303
Loading

0 comments on commit 04856cf

Please sign in to comment.