From b17dd0de9af623d6f14ceff5693fbc9606066800 Mon Sep 17 00:00:00 2001 From: Shawn Davis Date: Sun, 16 Aug 2020 19:11:53 -0400 Subject: [PATCH] Added support for capturing the command name. --- scripttease/factory.py | 6 ++++-- scripttease/library/commands/base.py | 18 +++++++++++++++--- scripttease/library/commands/templates.py | 1 + tests/test_library_overlays_ubuntu.py | 5 +++++ tests/test_parsers_base.py | 12 ++++++++++-- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/scripttease/factory.py b/scripttease/factory.py index c14b4b1..c2ccda4 100644 --- a/scripttease/factory.py +++ b/scripttease/factory.py @@ -53,9 +53,11 @@ class Factory(object): try: items = kwargs.pop("items", 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: log.critical("Failed to load %s command: %s" % (name, e)) return None diff --git a/scripttease/library/commands/base.py b/scripttease/library/commands/base.py index cab46aa..fc78220 100644 --- a/scripttease/library/commands/base.py +++ b/scripttease/library/commands/base.py @@ -4,10 +4,12 @@ class Command(object): """A command line statement.""" - def __init__(self, statement, comment=None, condition=None, cd=None, environments=None, function=None, prefix=None, - register=None, shell=None, stop=False, sudo=None, tags=None, **kwargs): + def __init__(self, statement, comment=None, condition=None, cd=None, environments=None, function=None, name=None, + prefix=None, register=None, shell=None, stop=False, sudo=None, tags=None, **kwargs): """Initialize a command. + :param name: The name of the command. + :param statement: The statement to be executed. :type statement: str @@ -26,6 +28,10 @@ class Command(object): :param function: The name of the function in which the statement is executed. :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. :type prefix: str @@ -53,6 +59,7 @@ class Command(object): self.cd = cd self.environments = environments or list() self.function = function + self.name = name self.prefix = prefix self.register = register self.shell = shell @@ -143,7 +150,7 @@ class Command(object): class ItemizedCommand(object): """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. :param callback: The function to be used to generate the command. @@ -151,6 +158,10 @@ class ItemizedCommand(object): :param items: The command arguments. :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 kwargs: Keyword arguments are passed to the command class upon instantiation. @@ -160,6 +171,7 @@ class ItemizedCommand(object): self.callback = callback self.items = items self.kwargs = kwargs + self.name = name def __getattr__(self, item): return self.kwargs.get(item) diff --git a/scripttease/library/commands/templates.py b/scripttease/library/commands/templates.py index b4b6bb6..22ddc95 100644 --- a/scripttease/library/commands/templates.py +++ b/scripttease/library/commands/templates.py @@ -52,6 +52,7 @@ class Template(Command): 'environments': kwargs.pop("environments", None), 'function': kwargs.pop("function", None), # 'local': kwargs.pop("local", False), + 'name': "template", 'prefix': kwargs.pop("prefix", None), 'shell': kwargs.pop("shell", "/bin/bash"), 'stop': kwargs.pop("stop", False), diff --git a/tests/test_library_overlays_ubuntu.py b/tests/test_library_overlays_ubuntu.py index 1afe749..6a69621 100644 --- a/tests/test_library_overlays_ubuntu.py +++ b/tests/test_library_overlays_ubuntu.py @@ -43,6 +43,11 @@ def test_apache_enable_site(): 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(): c = service_reload("postfix") assert "service postfix reload" in c.get_statement() diff --git a/tests/test_parsers_base.py b/tests/test_parsers_base.py index d728ac4..e41c391 100644 --- a/tests/test_parsers_base.py +++ b/tests/test_parsers_base.py @@ -1,4 +1,5 @@ import pytest +from scripttease.library.overlays.posix import touch, Function from scripttease.library.scripts import Script # from scripttease.parsers import filter_commands, load_commands from scripttease.parsers.base import Parser @@ -13,8 +14,15 @@ class TestParser(object): # def test_get_commands(self): # pass # - # def test_get_functions(self): - # pass + def test_get_functions(self): + 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): p = Parser("/path/to/nonexistent.txt")