Skip to content

Commit 1acc639

Browse files
author
Klemen Vodopivec
committed
Turn CommandSequence into Python list, add += operator
With CommandSequence acting as Python list, it inherits all functions to manage the list. In addition, overloaded += operator makes it easy to add one or more commands.
1 parent 2110c9d commit 1acc639

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

scan/commands/commandsequence.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from scan.commands.command import Command
1212
from scan.util.xml_helper import indent
1313

14-
class CommandSequence(object):
14+
class CommandSequence(list):
1515
"""A sequence of scan commands
1616
1717
Basically a list of commands,
@@ -21,19 +21,16 @@ class CommandSequence(object):
2121
:param commands: One or more commands, or existing list of commands.
2222
"""
2323
def __init__(self, *commands):
24-
self.commands=[]
25-
for command in commands:
26-
# Append individual command
27-
if isinstance(command, Command):
28-
self.commands.append(command)
29-
else:
30-
# Assume iterable tuple, list, set, .. and append its content
31-
self.commands += list(command)
24+
super(CommandSequence, self).__init__()
25+
self.append(*commands)
3226

33-
def __len__(self):
34-
""":return: Number of commands"""
35-
return len(self.commands)
36-
27+
def __iadd__(self, other):
28+
if isinstance(other, list):
29+
self.append(*other)
30+
else:
31+
self.append(other)
32+
return self
33+
3734
def append(self, *commands):
3835
"""Append more commands to the sequence
3936
@@ -42,15 +39,15 @@ def append(self, *commands):
4239
for command in commands:
4340
# Append individual command
4441
if isinstance(command, Command):
45-
self.commands.append(command)
42+
super(CommandSequence, self).append(command)
4643
else:
4744
# Assume iterable tuple, list, set, .. and append its content
48-
self.commands += list(command)
45+
self.append(list(command))
4946

5047
def genSCN(self):
5148
""":return: Command in XML format suitable for scan server"""
5249
scn = ET.Element('commands')
53-
for c in self.commands:
50+
for c in self:
5451
scn.append(c.genXML())
5552

5653
indent(scn)
@@ -71,11 +68,11 @@ def format(self):
7168
]
7269
7370
"""
74-
if len(self.commands) == 0:
71+
if len(self) == 0:
7572
return "[]"
7673

7774
result = "["
78-
for cmd in self.commands:
75+
for cmd in self:
7976
result += "\n" + cmd.format(1)
8077
result += "\n]"
8178
return result
@@ -84,7 +81,7 @@ def __str__(self):
8481
return self.format()
8582

8683
def __repr__(self):
87-
return "CommandSequence(" + str(self.commands) + ")"
84+
return "CommandSequence(" + str(self) + ")"
8885

8986

9087
if __name__ == "__main__":

0 commit comments

Comments
 (0)