Skip to content

Commit

Permalink
ModTool: Proper check on blockname before renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
swap-nil7 committed Jun 25, 2018
1 parent 03ae793 commit 304feab
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion gr-utils/python/modtool/modtool_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import os
import re
import glob
import itertools

from .util_functions import append_re_line_sequence, ask_yes_no
from .cmakefile_editor import CMakeFileEditor
Expand All @@ -44,6 +46,21 @@ def __init__(self):
self._skip_cmakefiles = False
self._license_file = None

def get_block_candidates(self):
block_candidates = []
cpp_filters = ["*.cc", "*.cpp"]
cpp_blocks = []
for ftr in cpp_filters:
cpp_blocks += [x for x in glob.glob1("lib", ftr) if not (x.startswith('qa_') or
x.startswith('test_'))]
python_blocks = [x for x in glob.glob1("python", "*.py") if not (x.startswith('qa_') or
x.startswith('build') or x.startswith('__init__'))]
for block in itertools.chain(cpp_blocks, python_blocks):
block = os.path.splitext(block)[0]
block = block.split('_impl')[0]
block_candidates.append(block)
return block_candidates

@staticmethod
def setup_parser(parser):
#parser = parser.add_argument_group(title="Rename module options")
Expand All @@ -66,13 +83,19 @@ def setup(self, options):
if not re.match('[a-zA-Z0-9_]+', self._info['oldname']):
raise ModToolException('Invalid block name.')
print("Block/code to rename identifier: " + self._info['oldname'])
block_candidates = self.get_block_candidates()
if self._info['oldname'] not in block_candidates:
choices = [x for x in block_candidates if self._info['oldname'] in x]
if len(choices)>0:
print("Suggested alternatives: "+str(choices))
raise ModToolException("Blockname for renaming does not exists!")
self._info['fulloldname'] = self._info['modname'] + '_' + self._info['oldname']

# now get the new block name
if options.new_name is None:
self._info['newname'] = input("Enter name of block/code (without module name prefix): ")
else:
self._info['newname'] = options.new_name[0]
self._info['newname'] = options.new_name
if not re.match('[a-zA-Z0-9_]+', self._info['newname']):
raise ModToolException('Invalid block name.')
print("Block/code identifier: " + self._info['newname'])
Expand Down

0 comments on commit 304feab

Please sign in to comment.