From 894abd05c0edbd39ad54674d48fed27c5580459f Mon Sep 17 00:00:00 2001 From: Shawn Davis Date: Tue, 21 Jun 2022 20:39:24 -0500 Subject: [PATCH] Added base support for commands that generate multiple statements. --- scripttease/lib/commands/base.py | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/scripttease/lib/commands/base.py b/scripttease/lib/commands/base.py index 3e83f17..49c4626 100644 --- a/scripttease/lib/commands/base.py +++ b/scripttease/lib/commands/base.py @@ -14,6 +14,7 @@ __all__ = ( "Command", "Content", "ItemizedCommand", + "MultipleCommands", "Prompt", "Sudo", "Template", @@ -273,6 +274,8 @@ class ItemizedCommand(object): self.args = args self.callback = callback self.items = items + self.name = None + self.kwargs = kwargs # Set defaults for when ItemizedCommand is referenced directly before individual commands are instantiated. For @@ -292,6 +295,7 @@ class ItemizedCommand(object): """ kwargs = self.kwargs.copy() + kwargs['name'] = self.name a = list() for item in self.items: @@ -326,6 +330,42 @@ class ItemizedCommand(object): return True +class MultipleCommands(object): + """An interface for handling command composition where multiple statements must be generated.""" + + def __init__(self, commands, comment=None, tags=None, **kwargs): + self.commands = commands + self.comment = comment or "run multiple commands" + self.tags = tags or list() + + self.options = kwargs + + def __getattr__(self, item): + return self.options.get(item) + + def __iter__(self): + return iter(self.commands) + + def __repr__(self): + return "<%s %s>" % (self.__class__.__name__, self.comment) + + def get_statement(self, cd=True, include_comment=True, include_register=True, include_stop=True): + a = list() + if include_comment: + a.append("# %s" % self.comment) + + for command in self.commands: + statement = command.get_statement( + cd=cd, + include_comment=False, + include_register=include_register, + include_stop=include_stop + ) + a.append(statement) + + return "\n".join(a) + + class Prompt(Command): """Prompt the user for input."""