Define initialization for different ParserNode classes

This commit is contained in:
Joona Hoikkala 2019-08-18 12:12:23 +03:00
parent 4975d0a273
commit 864f06100a
No known key found for this signature in database
GPG key ID: D5AA86BBF9B29A5C
2 changed files with 114 additions and 6 deletions

View file

@ -123,10 +123,19 @@ class ParserNode(object):
dirty: bool
# Filepath of the file where the configuration element for this ParserNode
# object resides.
# object resides. This can be None if a configuration directive is defined in
# for example the httpd command line.
filepath: Optional[str]
"""
@abc.abstractmethod
def __init__(self, ancestor=None, filepath="", dirty=False): # pragma: no cover
"""
Initializes the ParserNode instance, and sets the ParserNode specific
instance variables. This is not meant to be used directly, but through
specific classes implementing ParserNode interface.
"""
@abc.abstractmethod
def save(self, msg):
"""
@ -163,6 +172,20 @@ class CommentNode(ParserNode):
"""
# pylint: disable=super-init-not-called
@abc.abstractmethod
def __init__(self, comment, ancestor, filepath, dirty=False): # pragma: no cover
"""
Initializes the CommentNode instance and sets its instance variables.
:param str comment: Contents of the comment.
:param BlockNode ancestor: BlockNode ancestor for this CommentNode.
:param str filepath: Filesystem path for the file where this CommentNode
does or should exist in the filesystem.
:param bool dirty: Boolean flag for denoting if this CommentNode has been
created or changed after the last save.
"""
@six.add_metaclass(abc.ABCMeta)
class DirectiveNode(ParserNode):
@ -172,6 +195,10 @@ class DirectiveNode(ParserNode):
single directives, it is not able to have child nodes and hence it is always
treated as a leaf node.
If a this directive was defined on the httpd command line, the ancestor instance
variable for this DirectiveNode should be None, and it should be inserted to the
beginning of root BlockNode children sequence.
DirectiveNode objects should have the following attributes:
# True if this DirectiveNode is enabled and False if it is inside of an
@ -185,6 +212,26 @@ class DirectiveNode(ParserNode):
parameters: Tuple[str, ...]
"""
# pylint: disable=too-many-arguments, super-init-not-called
def __init__(self, name, parameters=(), ancestor=None, filepath="",
dirty=False, enabled=True): # pragma: no cover
"""
Initializes the DirectiveNode instance and sets its instance variables.
:param name: Name or key of the DirectiveNode object.
:param tuple parameters: Tuple of str parameters for this DirectiveNode.
:param ancestor: BlockNode ancestor for this DirectiveNode, or None for
directives introduced at the httpd command line.
:param str filepath: Filesystem path for the file where this DirectiveNode
does or should exist in the filesystem, or None for directives introduced
in the httpd command line.
:param bool dirty: Boolean flag for denoting if this DirectiveNode has been
created or changed after the last save.
:param bool enabled: True if this DirectiveNode object is parsed in the active
configuration of the httpd. False if the DirectiveNode exists within a
unmatched conditional configuration block.
"""
@abc.abstractmethod
def set_parameters(self, parameters):
@ -238,8 +285,9 @@ class BlockNode(ParserNode):
# parsed, but its children should be flagged as disabled.
enabled: bool
# Name, or key of the configuration directive
name: str
# Name, or key of the configuration directive. If the BlockNode is the root
# configuration node, the name should be None.
name: Optional[str]
# Tuple of parameters of this ParserNode object, excluding whitespaces.
parameters: Tuple[str, ...]
@ -249,6 +297,28 @@ class BlockNode(ParserNode):
children: Tuple[ParserNode, ...]
"""
# pylint: disable=too-many-arguments, super-init-not-called
@abc.abstractmethod
def __init__(self, name="", parameters=(), children=(), ancestor=None,
filepath="", dirty=False, enabled=True): # pragma: no cover
"""
Initializes the BlockNode instance and sets its instance variables.
:param name: Name or key of the BlockNode object, or None for root
configuration node.
:param tuple parameters: Tuple of str parameters for this BlockNode.
:param tuple children: Tuple of ParserNode children for this BlockNode.
:param ancestor: BlockNode ancestor for this BlockNode, or None for root
configuration node.
:param str filepath: Filesystem path for the file where this BlockNode does
or should exist in the filesystem.
:param bool dirty: Boolean flag for denoting if this BlockNode has been
created or changed after the last save.
:param bool enabled: True if this BlockNode object is parsed in the active
configuration object by the httpd. False if the BlockNode exists within
a unmatched conditional configuration block.
"""
@abc.abstractmethod
def add_child_block(self, name, parameters=None, position=None):
"""

View file

@ -13,6 +13,15 @@ class DummyParserNode(interfaces.ParserNode):
dirty = False
filepath = None
def __init__(self, ancestor=None, filepath=str(), dirty=False):
"""
Initializes the ParserNode instance.
"""
super(DummyParserNode, self).__init__(ancestor, filepath, dirty)
self.ancestor = ancestor
self.dirty = dirty
self.filepath = filepath
def save(self, msg): # pragma: no cover
"""Save"""
pass
@ -20,7 +29,13 @@ class DummyParserNode(interfaces.ParserNode):
class DummyCommentNode(DummyParserNode):
""" A dummy class implementing CommentNode interface """
comment = ""
def __init__(self, comment, ancestor, filepath, dirty=False):
"""
Initializes the CommentNode instance and sets its instance variables.
"""
super(DummyCommentNode, self).__init__(ancestor, filepath, dirty)
self.comment = comment
class DummyDirectiveNode(DummyParserNode):
@ -29,6 +44,17 @@ class DummyDirectiveNode(DummyParserNode):
enabled = True
name = ""
# pylint: disable=too-many-arguments
def __init__(self, name, parameters=(), ancestor=None, filepath="",
dirty=False, enabled=True):
"""
Initializes the DirectiveNode instance and sets its instance variables.
"""
super(DummyDirectiveNode, self).__init__(ancestor, filepath, dirty)
self.name = name
self.parameters = parameters
self.enabled = enabled
def set_parameters(self, parameters): # pragma: no cover
"""Set parameters"""
pass
@ -41,6 +67,18 @@ class DummyBlockNode(DummyParserNode):
enabled = True
name = ""
# pylint: disable=too-many-arguments
def __init__(self, name="", parameters=(), children=(), ancestor=None,
filepath="", dirty=False, enabled=True):
"""
Initializes the BlockNode instance and sets its instance variables.
"""
super(DummyBlockNode, self).__init__(ancestor, filepath, dirty)
self.name = name
self.parameters = parameters
self.children = children
self.enabled = enabled
def add_child_block(self, name, parameters=None, position=None): # pragma: no cover
"""Add child block"""
pass
@ -87,8 +125,8 @@ class ParserNodeTest(unittest.TestCase):
def test_dummy(self):
dummyblock = DummyBlockNode()
dummydirective = DummyDirectiveNode()
dummycomment = DummyCommentNode()
dummydirective = DummyDirectiveNode("Name")
dummycomment = DummyCommentNode("Comment", dummyblock, "/some/file")
if __name__ == "__main__":