summaryrefslogtreecommitdiff
path: root/zuul/lib/filecomments.py
blob: fe8399a0d193beab94a39e0add80b2cb522e756e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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.

import voluptuous as vs


FILE_COMMENT = {
    'line': int,
    'message': str,
    'range': {
        'start_line': int,
        'start_character': int,
        'end_line': int,
        'end_character': int,
    },
    'level': str,
}

FILE_COMMENTS = {str: [FILE_COMMENT]}

FILE_COMMENTS_SCHEMA = vs.Schema(FILE_COMMENTS)


def validate(file_comments):
    FILE_COMMENTS_SCHEMA(file_comments)


def extractLines(file_comments):
    """Extract file/line tuples from file comments for mapping"""

    lines = set()
    for path, comments in file_comments.items():
        for comment in comments:
            if 'line' in comment:
                lines.add((path, int(comment['line'])))
            if 'range' in comment:
                rng = comment['range']
                for key in ['start_line', 'end_line']:
                    if key in rng:
                        lines.add((path, int(rng[key])))
    return list(lines)


def updateLines(file_comments, lines):
    """Update the line numbers in file_comments with the supplied mapping"""

    for path, comments in file_comments.items():
        for comment in comments:
            if 'line' in comment:
                comment['line'] = lines.get((path, comment['line']),
                                            comment['line'])
            if 'range' in comment:
                rng = comment['range']
                for key in ['start_line', 'end_line']:
                    if key in rng:
                        rng[key] = lines.get((path, rng[key]), rng[key])