summaryrefslogtreecommitdiff
path: root/gr-utils/blocktool/core/comments.py
blob: d7919609bb6510ee43d78ef830fb924cca84efd7 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#
# Copyright 2019 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#
""" Module to read and add special blocktool comments in the public header """

from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals

import warnings

from ..core import Constants


def strip_symbols(line):
    """
    helper function to strip symbols
    from blocktool comment syntax
    """
    return line.split(':')[-1].lstrip().rstrip()


def exist_comments(self):
    """
    function to check if blocktool special comments
    already exist in the public header
    """
    _comments = True
    _index = None
    lines = []
    with open(self.target_file, 'r') as header:
        lines = header.readlines()
        for line in lines:
            if Constants.BLOCKTOOL in line:
                _index = lines.index(line)
    return bool(_index)


def validate_message_port(self, message_ports, suppress_input, suppress_output):
    """
    function to solve conflicts if any in the
    *message_port* comments and the implementation information
    """
    if message_ports['input'] != self.parsed_data['message_port']['input']:
        if not suppress_input:
            warnings.warn(
                'Conflict in values input message port Id. Add ! at the start of the key-value line to mandatory use the comment value.')
            self.parsed_data['message_port']['input'] = message_ports['input']
    if message_ports['output'] != self.parsed_data['message_port']['output']:
        if not suppress_output:
            warnings.warn(
                'Conflict in values output message port Id. Add ! at the start of the key-value line to mandatory use the comment value.')
            self.parsed_data['message_port']['output'] = message_ports['output']


def read_comments(self):
    """
    function to read special blocktool comments
    in the public header
    """
    temp_parsed_data = {}
    if self.parsed_data['io_signature'] or self.parsed_data['message_port']:
        temp_parsed_data['io_signature'] = self.parsed_data['io_signature']
        temp_parsed_data['message_port'] = self.parsed_data['message_port']
    self.parsed_data['io_signature'] = {
        "input": {
            "signature": None
        },
        "output": {
            "signature": None
        }
    }
    self.parsed_data['message_port'] = {
        "input": [],
        "output": []
    }
    _suppress_input = False
    _suppress_output = False
    parsed_io = self.parsed_data['io_signature']
    message_port = self.parsed_data['message_port']
    special_comments = []
    _index = None
    lines = []
    with open(self.target_file, 'r') as header:
        lines = header.readlines()
        for line in lines:
            if Constants.BLOCKTOOL in line:
                _index = lines.index(line)

    if _index is not None:
        _index = _index+1
        for num in range(_index, len(lines)):
            if Constants.END_BLOCKTOOL in lines[num]:
                break
            special_comments.append(lines[num])
        for comment in special_comments:
            if Constants.INPUT_SIG in comment:
                parsed_io['input']['signature'] = strip_symbols(comment)
            if Constants.INPUT_MAX in comment:
                parsed_io['input']['max_streams'] = strip_symbols(comment)
            if Constants.INPUT_MIN in comment:
                parsed_io['input']['min_streams'] = strip_symbols(comment)
            if parsed_io['input']['signature'] is Constants.MAKE and not None:
                if Constants.INPUT_MAKE_SIZE in comment:
                    parsed_io['input']['sizeof_stream_item'] = strip_symbols(
                        comment)
            elif parsed_io['input']['signature'] is Constants.MAKE2 and not None:
                if Constants.INPUT_MAKE_SIZE1 in comment:
                    parsed_io['input']['sizeof_stream_item1'] = strip_symbols(
                        comment)
                if Constants.INPUT_MAKE_SIZE2 in comment:
                    parsed_io['input']['sizeof_stream_item2'] = strip_symbols(
                        comment)
            elif parsed_io['input']['signature'] is Constants.MAKE3 and not None:
                if Constants.INPUT_MAKE_SIZE1 in comment:
                    parsed_io['input']['sizeof_stream_item1'] = strip_symbols(
                        comment)
                if Constants.INPUT_MAKE_SIZE2 in comment:
                    parsed_io['input']['sizeof_stream_item2'] = strip_symbols(
                        comment)
                if Constants.INPUT_MAKE_SIZE3 in comment:
                    parsed_io['input']['sizeof_stream_item3'] = strip_symbols(
                        comment)
            elif parsed_io['input']['signature'] is Constants.MAKEV and not None:
                if Constants.INPUT_MAKEV_SIZE in comment:
                    parsed_io['input']['sizeof_stream_items'] = strip_symbols(
                        comment)

            if Constants.OUTPUT_SIG in comment:
                parsed_io['output']['signature'] = strip_symbols(comment)
            if Constants.OUTPUT_MAX in comment:
                parsed_io['output']['max_streams'] = strip_symbols(comment)
            if Constants.OUTPUT_MIN in comment:
                parsed_io['output']['min_streams'] = strip_symbols(comment)
            if parsed_io['output']['signature'] is Constants.MAKE and not None:
                if Constants.OUTPUT_MAKE_SIZE in comment:
                    parsed_io['output']['sizeof_stream_item'] = strip_symbols(
                        comment)
            elif parsed_io['output']['signature'] is Constants.MAKE2:
                if Constants.OUTPUT_MAKE_SIZE1 in comment:
                    parsed_io['output']['sizeof_stream_item1'] = strip_symbols(
                        comment)
                if Constants.OUTPUT_MAKE_SIZE2 in comment:
                    parsed_io['output']['sizeof_stream_item2'] = strip_symbols(
                        comment)
            elif parsed_io['output']['signature'] is Constants.MAKE3 and not None:
                if Constants.OUTPUT_MAKE_SIZE1 in comment:
                    parsed_io['output']['sizeof_stream_item1'] = strip_symbols(
                        comment)
                if Constants.OUTPUT_MAKE_SIZE2 in comment:
                    parsed_io['output']['sizeof_stream_item2'] = strip_symbols(
                        comment)
                if Constants.OUTPUT_MAKE_SIZE3 in comment:
                    parsed_io['output']['sizeof_stream_item3'] = strip_symbols(
                        comment)
            elif parsed_io['output']['signature'] is Constants.MAKEV and not None:
                if Constants.OUTPUT_MAKEV_SIZE in comment:
                    parsed_io['output']['sizeof_stream_items'] = strip_symbols(
                        comment)

            if Constants.INPUT_PORT in comment:
                if Constants.EXCLAMATION in comment:
                    _suppress_input = True
                if strip_symbols(comment):
                    message_port['input'] = strip_symbols(comment).split(', ')
            if Constants.OUTPUT_PORT in comment:
                if Constants.EXCLAMATION in comment:
                    _suppress_output = True
                if strip_symbols(comment):
                    message_port['output'] = strip_symbols(comment).split(', ')
    validate_message_port(
        self, temp_parsed_data['message_port'], _suppress_input, _suppress_output)
    self.parsed_data['io_signature'] = temp_parsed_data['io_signature']


def add_comments(self):
    """
    function to add special blocktool comments
    in the public header
    """
    _index = None
    lines = []
    parsed_io = self.parsed_data['io_signature']
    message_port = self.parsed_data['message_port']
    with open(self.target_file, 'r') as header:
        lines = header.readlines()
        for line in lines:
            if Constants.BLOCKTOOL in line:
                _index = lines.index(line)
    if _index is None:
        with open(self.target_file, 'a') as header:
            header.write('\n')
            header.write('/* '+Constants.BLOCKTOOL + '\n')
            header.write('input_signature: ' +
                         parsed_io['input']['signature'] + '\n')
            header.write('input_min_streams: ' +
                         parsed_io['input']['min_streams'] + '\n')
            header.write('input_max_streams: ' +
                         parsed_io['input']['max_streams'] + '\n')
            if parsed_io['input']['signature'] is Constants.MAKE:
                header.write('input_sizeof_stream_item: ' +
                             parsed_io['input']['sizeof_stream_item'] + '\n')
            elif parsed_io['input']['signature'] is Constants.MAKE2:
                header.write('input_sizeof_stream_item1: ' +
                             parsed_io['input']['sizeof_stream_item1'] + '\n')
                header.write('input_sizeof_stream_item2: ' +
                             parsed_io['input']['sizeof_stream_item2'] + '\n')
            elif parsed_io['input']['signature'] is Constants.MAKE3:
                header.write('input_sizeof_stream_item1: ' +
                             parsed_io['input']['sizeof_stream_item1'] + '\n')
                header.write('input_sizeof_stream_item2: ' +
                             parsed_io['input']['sizeof_stream_item2'] + '\n')
                header.write('input_sizeof_stream_item3: ' +
                             parsed_io['input']['sizeof_stream_item3'] + '\n')
            elif parsed_io['input']['signature'] is Constants.MAKEV:
                header.write('input_sizeof_stream_item: ' +
                             parsed_io['input']['sizeof_stream_items'] + '\n')
            header.write('output_signature: ' +
                         parsed_io['output']['signature'] + '\n')
            header.write('output_min_streams: ' +
                         parsed_io['output']['min_streams'] + '\n')
            header.write('output_max_streams: ' +
                         parsed_io['output']['max_streams'] + '\n')
            if parsed_io['output']['signature'] is Constants.MAKE:
                header.write('output_sizeof_stream_item: ' +
                             parsed_io['output']['sizeof_stream_item'] + '\n')
            elif parsed_io['output']['signature'] is Constants.MAKE2:
                header.write('output_sizeof_stream_item1: ' +
                             parsed_io['output']['sizeof_stream_item1'] + '\n')
                header.write('output_sizeof_stream_item2: ' +
                             parsed_io['output']['sizeof_stream_item2'] + '\n')
            elif parsed_io['output']['signature'] is Constants.MAKE3:
                header.write('output_sizeof_stream_item1: ' +
                             parsed_io['output']['sizeof_stream_item1'] + '\n')
                header.write('output_sizeof_stream_item2: ' +
                             parsed_io['output']['sizeof_stream_item2'] + '\n')
                header.write('output_sizeof_stream_item3: ' +
                             parsed_io['output']['sizeof_stream_item3'] + '\n')
            elif parsed_io['output']['signature'] is Constants.MAKEV:
                header.write('output_sizeof_stream_item: ' +
                             parsed_io['output']['sizeof_stream_items'] + '\n')

            if message_port['input']:
                header.write('message_input: ' +
                             ', '.join(message_port['input']) + '\n')
            else:
                header.write('message_input: ' + '\n')
            if message_port['output']:
                header.write('message_output: ' +
                             ', '.join(message_port['output']) + '\n')
            else:
                header.write('message_output: ' + '\n')
            header.write(Constants.END_BLOCKTOOL + '*/' + '\n')