Added support of has/set dynamic attribute of a command.

development
Shawn Davis 4 years ago
parent 1ddea7a1a5
commit 891b07b388
  1. 2
      VERSION.txt
  2. 47
      scripttease/library/commands/base.py
  3. 20
      tests/test_library_commands_base.py

@ -1 +1 @@
6.4.0-d 6.4.2-d

@ -138,6 +138,28 @@ class Command(object):
return "\n".join(b) return "\n".join(b)
def has_attribute(self, name):
"""Indicates whether the command has the named, dynamic attribute.
:param name: The name of the attribute to be checked.
:type name: str
:rtype: bool
"""
return name in self._attributes
def set_attribute(self, name, value):
"""Set the value of a dynamic attribute.
:param name: The name of the attribute.
:type name: str
:param value: The value of the attribute.
"""
self._attributes[name] = value
def _get_statement(self): def _get_statement(self):
"""By default, get the statement passed upon command initialization. """By default, get the statement passed upon command initialization.
@ -222,6 +244,31 @@ class ItemizedCommand(object):
return "\n".join(a) return "\n".join(a)
def has_attribute(self, name):
"""Indicates whether the command has the named, dynamic attribute.
:param name: The name of the attribute to be checked.
:type name: str
:rtype: bool
"""
return name in self.kwargs
def set_attribute(self, name, value):
"""Set the value of a dynamic attribute.
:param name: The name of the attribute.
:type name: str
:param value: The value of the attribute.
.. note::
This is applied to all command in the itemized list.
"""
self.kwargs[name] = value
class Sudo(object): class Sudo(object):
"""Helper class for defining sudo options.""" """Helper class for defining sudo options."""

@ -35,6 +35,10 @@ class TestCommand(object):
statement = c.get_statement() statement = c.get_statement()
assert "if [[ $?" in statement assert "if [[ $?" in statement
def test_has_attribute(self):
c = Command("ls -ls")
assert c.has_attribute("testing") is False
def test_init(self): def test_init(self):
c = Command("ls -ls", sudo=Sudo(user="deploy")) c = Command("ls -ls", sudo=Sudo(user="deploy"))
assert isinstance(c.sudo, Sudo) assert isinstance(c.sudo, Sudo)
@ -60,6 +64,12 @@ class TestCommand(object):
c = Command("ls -ls") c = Command("ls -ls")
assert repr(c) == "<Command>" assert repr(c) == "<Command>"
def test_set_attribute(self):
c = Command("ls -ls")
assert c.testing is None
c.set_attribute("testing", True)
assert c.testing is True
class TestItemizedCommand(object): class TestItemizedCommand(object):
@ -80,6 +90,16 @@ class TestItemizedCommand(object):
assert "psycopg2-binary" in statement assert "psycopg2-binary" in statement
assert "django" in statement assert "django" in statement
def test_has_attribute(self):
c = ItemizedCommand(python_pip, ["Pillow", "psycopg2-binary", "django"], "$item")
assert c.has_attribute("testing") is False
def test_repr(self): def test_repr(self):
c = ItemizedCommand(python_pip, ["Pillow", "psycopg2-binary", "django"], "$item") c = ItemizedCommand(python_pip, ["Pillow", "psycopg2-binary", "django"], "$item")
assert repr(c) == "<ItemizedCommand python_pip>" assert repr(c) == "<ItemizedCommand python_pip>"
def test_set_attribute(self):
c = ItemizedCommand(python_pip, ["Pillow", "psycopg2-binary", "django"], "$item")
assert c.testing is None
c.set_attribute("testing", True)
assert c.testing is True

Loading…
Cancel
Save