summaryrefslogtreecommitdiff
path: root/gr-utils/modtool/cli/makeyaml.py
blob: 834cc05a215891b650d188828b349f263c4286db (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
#
# Copyright 2018 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#
""" Automatically create YAML bindings for GRC from block code """

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

import os
import click

try:
    from gnuradio.blocktool import BlockHeaderParser
    from gnuradio.blocktool.core.base import BlockToolException
except ImportError:
    have_blocktool = False
else:
    have_blocktool = True

from ..core import get_block_candidates, ModToolMakeYAML, yaml_generator
from ..tools import SequenceCompleter
from .base import common_params, block_name, run, cli_input


@click.command('makeyaml', short_help=ModToolMakeYAML.description)
@click.option('-b', '--blocktool', is_flag=True,
              help='Use blocktool support to print yaml output. FILE PATH mandatory if used.')
@click.option('-o', '--output', is_flag=True,
              help='If given, a file with desired output format will be generated')
@common_params
@block_name
def cli(**kwargs):
    """
    \b
    Make an YAML file for GRC block bindings

    Note: This does not work on python blocks
    """
    kwargs['cli'] = True
    if kwargs['blocktool']:
        kwargs['modtool'] = True
        if kwargs['blockname'] is None:
            raise BlockToolException('Missing argument FILE PATH with blocktool flag')
        kwargs['file_path'] = os.path.abspath(kwargs['blockname'])
        if os.path.isfile(kwargs['file_path']):
            parse_yml = BlockHeaderParser(**kwargs)
            parse_yml.run_blocktool()
            parse_yml.cli = True
            parse_yml.yaml = True
            yaml_generator(parse_yml, **kwargs)
        else:
            raise BlockToolException('Invalid file path.')
    else:
        self = ModToolMakeYAML(**kwargs)
        click.secho("GNU Radio module name identified: " + self.info['modname'], fg='green')
        get_pattern(self)
        run(self)

def get_pattern(self):
    """ Get the regex pattern for block(s) to be parsed """
    if self.info['pattern'] is None:
        block_candidates = get_block_candidates()
        with SequenceCompleter(block_candidates):
            self.info['pattern'] = cli_input('Which blocks do you want to parse? (Regex): ')
    if not self.info['pattern'] or self.info['pattern'].isspace():
        self.info['pattern'] = '.'