Added support for capturing the command name.

development
Shawn Davis 4 years ago
parent a4b6985aac
commit b17dd0de9a
  1. 6
      scripttease/factory.py
  2. 18
      scripttease/library/commands/base.py
  3. 1
      scripttease/library/commands/templates.py
  4. 5
      tests/test_library_overlays_ubuntu.py
  5. 12
      tests/test_parsers_base.py

@ -53,9 +53,11 @@ class Factory(object):
try: try:
items = kwargs.pop("items", None) items = kwargs.pop("items", None)
if items is not None: if items is not None:
return ItemizedCommand(callback, items, *args, **kwargs) return ItemizedCommand(callback, items, *args, name=name, **kwargs)
return callback(*args, **kwargs) command = callback(*args, **kwargs)
command.name = name
return command
except (KeyError, NameError, TypeError, ValueError) as e: except (KeyError, NameError, TypeError, ValueError) as e:
log.critical("Failed to load %s command: %s" % (name, e)) log.critical("Failed to load %s command: %s" % (name, e))
return None return None

@ -4,10 +4,12 @@
class Command(object): class Command(object):
"""A command line statement.""" """A command line statement."""
def __init__(self, statement, comment=None, condition=None, cd=None, environments=None, function=None, prefix=None, def __init__(self, statement, comment=None, condition=None, cd=None, environments=None, function=None, name=None,
register=None, shell=None, stop=False, sudo=None, tags=None, **kwargs): prefix=None, register=None, shell=None, stop=False, sudo=None, tags=None, **kwargs):
"""Initialize a command. """Initialize a command.
:param name: The name of the command.
:param statement: The statement to be executed. :param statement: The statement to be executed.
:type statement: str :type statement: str
@ -26,6 +28,10 @@ class Command(object):
:param function: The name of the function in which the statement is executed. :param function: The name of the function in which the statement is executed.
:type function: str :type function: str
:param name: The name of the command from the mapping. Not used and not required for programmatic use, but
automatically assigned during factory instantiation.
:type name: str
:param prefix: A statement to execute before the main statement is executed. :param prefix: A statement to execute before the main statement is executed.
:type prefix: str :type prefix: str
@ -53,6 +59,7 @@ class Command(object):
self.cd = cd self.cd = cd
self.environments = environments or list() self.environments = environments or list()
self.function = function self.function = function
self.name = name
self.prefix = prefix self.prefix = prefix
self.register = register self.register = register
self.shell = shell self.shell = shell
@ -143,7 +150,7 @@ class Command(object):
class ItemizedCommand(object): class ItemizedCommand(object):
"""An itemized command represents multiple commands of with the same statement but different parameters.""" """An itemized command represents multiple commands of with the same statement but different parameters."""
def __init__(self, callback, items, *args, **kwargs): def __init__(self, callback, items, *args, name=None, **kwargs):
"""Initialize the command. """Initialize the command.
:param callback: The function to be used to generate the command. :param callback: The function to be used to generate the command.
@ -151,6 +158,10 @@ class ItemizedCommand(object):
:param items: The command arguments. :param items: The command arguments.
:type items: list[str] :type items: list[str]
:param name: The name of the command from the mapping. Not used and not required for programmatic use, but
automatically assigned during factory instantiation.
:type name: str
:param args: The itemized arguments. ``$item`` should be included. :param args: The itemized arguments. ``$item`` should be included.
:param kwargs: Keyword arguments are passed to the command class upon instantiation. :param kwargs: Keyword arguments are passed to the command class upon instantiation.
@ -160,6 +171,7 @@ class ItemizedCommand(object):
self.callback = callback self.callback = callback
self.items = items self.items = items
self.kwargs = kwargs self.kwargs = kwargs
self.name = name
def __getattr__(self, item): def __getattr__(self, item):
return self.kwargs.get(item) return self.kwargs.get(item)

@ -52,6 +52,7 @@ class Template(Command):
'environments': kwargs.pop("environments", None), 'environments': kwargs.pop("environments", None),
'function': kwargs.pop("function", None), 'function': kwargs.pop("function", None),
# 'local': kwargs.pop("local", False), # 'local': kwargs.pop("local", False),
'name': "template",
'prefix': kwargs.pop("prefix", None), 'prefix': kwargs.pop("prefix", None),
'shell': kwargs.pop("shell", "/bin/bash"), 'shell': kwargs.pop("shell", "/bin/bash"),
'stop': kwargs.pop("stop", False), 'stop': kwargs.pop("stop", False),

@ -43,6 +43,11 @@ def test_apache_enable_site():
assert "a2ensite example.com" in c.get_statement() assert "a2ensite example.com" in c.get_statement()
def test_command_exists():
assert command_exists("apache") is True
assert command_exists("nonexistent") is False
def test_service_reload(): def test_service_reload():
c = service_reload("postfix") c = service_reload("postfix")
assert "service postfix reload" in c.get_statement() assert "service postfix reload" in c.get_statement()

@ -1,4 +1,5 @@
import pytest import pytest
from scripttease.library.overlays.posix import touch, Function
from scripttease.library.scripts import Script from scripttease.library.scripts import Script
# from scripttease.parsers import filter_commands, load_commands # from scripttease.parsers import filter_commands, load_commands
from scripttease.parsers.base import Parser from scripttease.parsers.base import Parser
@ -13,8 +14,15 @@ class TestParser(object):
# def test_get_commands(self): # def test_get_commands(self):
# pass # pass
# #
# def test_get_functions(self): def test_get_functions(self):
# pass parser = Parser("/it/does/not/matter.ini")
command = touch("/path/to/file.txt", function="testing")
function = Function("testing")
parser._commands.append(command)
parser._functions.append(function)
assert len(parser.get_functions()) == 1
def test_load(self): def test_load(self):
p = Parser("/path/to/nonexistent.txt") p = Parser("/path/to/nonexistent.txt")

Loading…
Cancel
Save