1#  Copyright 2008-2015 Nokia Networks
2#  Copyright 2016-     Robot Framework Foundation
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16from robot.utils import is_string
17
18
19class CommentCache(object):
20
21    def __init__(self):
22        self._comments = []
23
24    def add(self, comment):
25        self._comments.append(comment)
26
27    def consume_with(self, function):
28        for comment in self._comments:
29            function(comment)
30        self.__init__()
31
32
33class Comments(object):
34
35    def __init__(self):
36        self._comments = []
37
38    def add(self, row):
39        if row.comments:
40            self._comments.extend(c.strip() for c in row.comments if c.strip())
41
42    @property
43    def value(self):
44        return self._comments
45
46
47class Comment(object):
48
49    def __init__(self, comment_data):
50        if is_string(comment_data):
51            comment_data = [comment_data] if comment_data else []
52        self._comment = comment_data or []
53
54    def __len__(self):
55        return len(self._comment)
56
57    def as_list(self):
58        if self._not_commented():
59            self._comment[0] = '# ' + self._comment[0]
60        return self._comment
61
62    def _not_commented(self):
63        return self._comment and self._comment[0] and self._comment[0][0] != '#'
64