#  Copyright 2008-2013 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
"""
Visitors can be used to easily travel the test suites, test cases and keywords.

For usage example, see :mod:`~robot.result` package.

The pattern is internally widely used in Robot Framework, for example see the
source code of :class:`robot.model.tagsetter.TagSetter`.
"""

from robot.model import SuiteVisitor


class ResultVisitor(SuiteVisitor):
    """Abstract class to conveniently travel
    :class:`robot.result.executionresult.Result` objects.

    See :mod:`example of usage <robot.result>`
    """
    def visit_result(self, result):
        if self.start_result(result) is not False:
            result.suite.visit(self)
            result.statistics.visit(self)
            result.errors.visit(self)
            self.end_result(result)

    def start_result(self, result):
        pass

    def end_result(self, result):
        pass

    def visit_statistics(self, stats):
        if self.start_statistics(stats) is not False:
            stats.total.visit(self)
            stats.tags.visit(self)
            stats.suite.visit(self)
            self.end_statistics(stats)

    def start_statistics(self, stats):
        pass

    def end_statistics(self, stats):
        pass

    def visit_total_statistics(self, stats):
        if self.start_total_statistics(stats) is not False:
            for stat in stats:
                stat.visit(self)
            self.end_total_statistics(stats)

    def start_total_statistics(self, stats):
        pass

    def end_total_statistics(self, stats):
        pass

    def visit_tag_statistics(self, stats):
        if self.start_tag_statistics(stats) is not False:
            for stat in stats:
                stat.visit(self)
            self.end_tag_statistics(stats)

    def start_tag_statistics(self, stats):
        pass

    def end_tag_statistics(self, stats):
        pass

    def visit_suite_statistics(self, stats):
        if self.start_suite_statistics(stats) is not False:
            for stat in stats:
                stat.visit(self)
            self.end_suite_statistics(stats)

    def start_suite_statistics(self, stats):
        pass

    def end_suite_statistics(self, suite_stats):
        pass

    def visit_stat(self, stat):
        if self.start_stat(stat) is not False:
            self.end_stat(stat)

    def start_stat(self, stat):
        pass

    def end_stat(self, stat):
        pass

    def visit_errors(self, errors):
        self.start_errors(errors)
        for msg in errors:
            msg.visit(self)
        self.end_errors(errors)

    def start_errors(self, errors):
        pass

    def end_errors(self, errors):
        pass

@@ -1063,33 +1063,942 @@
     
-See :mod:%60example of usag
+An implementation of visitor can be given to the visit method of result%0A    object. This will cause the result object to traversed and the visitor%0A    object's %60%60visit_x%60%60, %60%60start_x%60%60, and %60%60end_x%60%60 methods to be called for%0A    each suite, test, keyword, the results, and the statistics.%0A%0A    The start and end method are called for each element and their child%0A    elements. The visitor implementation can override only those that it is%0A    interested in. If any of the %60%60start_x%60%60 methods returns False, the visiting%0A    is stopped.%0A%0A    If the visitor implements a %60%60visit_x%60%60 method for element x, then the%0A    children of that element will not be visited, unless the visitor calls them%0A    explicitly. For example if the visitor implements method :meth:%60visit_test%60,%0A    the :meth:%60visit_keyword%60, :meth:%60start_keyword%60, and :meth:%60end_keyword%60%0A    methods are not called.%0A%0A    See package documentation for :mod:%60a usage exampl
 e %3Cr
@@ -2010,16 +2010,17 @@
 result%3E%60
+.
 %0A    %22%22%22

document the Visitor Api a bit better