summaryrefslogtreecommitdiff
path: root/gr-utils/python/modtool/modtool_base.py
diff options
context:
space:
mode:
authorMarcus Müller <marcus@hostalia.de>2018-08-31 23:02:22 +0200
committerMarcus Müller <marcus@hostalia.de>2018-08-31 23:02:22 +0200
commit254fe5e89403d4de1fa6663d09efdf946996aff3 (patch)
tree62877d7ac7fdedf6c397c51e22ac6f97eba97ddf /gr-utils/python/modtool/modtool_base.py
parent896d1c9da31963ecf5b0d90942c2af51ca998a69 (diff)
parent5ad935c3a3dd46ce2860b13e2b774e4841784616 (diff)
Merge remote-tracking branch 'origin/next' into merge_next
Diffstat (limited to 'gr-utils/python/modtool/modtool_base.py')
-rw-r--r--gr-utils/python/modtool/modtool_base.py111
1 files changed, 60 insertions, 51 deletions
diff --git a/gr-utils/python/modtool/modtool_base.py b/gr-utils/python/modtool/modtool_base.py
index 0bbd95946d..4263e498f7 100644
--- a/gr-utils/python/modtool/modtool_base.py
+++ b/gr-utils/python/modtool/modtool_base.py
@@ -20,13 +20,17 @@
#
""" Base class for the modules """
+from __future__ import print_function
+from __future__ import absolute_import
+from __future__ import unicode_literals
+
import os
import re
-from optparse import OptionParser, OptionGroup
+from argparse import ArgumentParser, RawDescriptionHelpFormatter
from gnuradio import gr
-from util_functions import get_modname
-from scm import SCMRepoFactory
+from .util_functions import get_modname
+from .scm import SCMRepoFactory
class ModToolException(BaseException):
""" Standard exception for modtool classes. """
@@ -35,6 +39,8 @@ class ModToolException(BaseException):
class ModTool(object):
""" Base class for all modtool command classes. """
name = 'base'
+ description = None
+
def __init__(self):
self._subdirs = ['lib', 'include', 'python', 'swig', 'grc'] # List subdirs where stuff happens
self._has_subdirs = {}
@@ -44,52 +50,56 @@ class ModTool(object):
for subdir in self._subdirs:
self._has_subdirs[subdir] = False
self._skip_subdirs[subdir] = False
- self.parser = self.setup_parser()
self._dir = None
self.scm = None
- def setup_parser(self):
- """ Init the option parser. If derived classes need to add options,
- override this and call the parent function. """
- parser = OptionParser(add_help_option=False)
- parser.usage = '%prog ' + self.name + ' [options] <PATTERN> \n' + \
- ' Call "%prog ' + self.name + '" without any options to run it interactively.'
- ogroup = OptionGroup(parser, "General options")
- ogroup.add_option("-h", "--help", action="help", help="Displays this help message.")
- ogroup.add_option("-d", "--directory", type="string", default=".",
- help="Base directory of the module. Defaults to the cwd.")
- ogroup.add_option("-n", "--module-name", type="string", default=None,
- help="Use this to override the current module's name (is normally autodetected).")
- ogroup.add_option("-N", "--block-name", type="string", default=None,
- help="Name of the block, where applicable.")
- ogroup.add_option("--skip-lib", action="store_true", default=False,
- help="Don't do anything in the lib/ subdirectory.")
- ogroup.add_option("--skip-swig", action="store_true", default=False,
- help="Don't do anything in the swig/ subdirectory.")
- ogroup.add_option("--skip-python", action="store_true", default=False,
- help="Don't do anything in the python/ subdirectory.")
- ogroup.add_option("--skip-grc", action="store_true", default=False,
- help="Don't do anything in the grc/ subdirectory.")
- ogroup.add_option("--scm-mode", type="choice", choices=('yes', 'no', 'auto'),
- default=gr.prefs().get_string('modtool', 'scm_mode', 'no'),
- help="Use source control management (yes, no or auto).")
- ogroup.add_option("-y", "--yes", action="store_true", default=False,
- help="Answer all questions with 'yes'. This can overwrite and delete your files, so be careful.")
- parser.add_option_group(ogroup)
+ @staticmethod
+ def setup_parser(parser):
+ """Override in child class."""
+ pass
+
+ @staticmethod
+ def setup_parser_block(parser):
+ """Setup options specific for block manipulating modules."""
+ parser.add_argument("blockname", nargs="?", metavar="BLOCK_NAME",
+ help="Name of the block/module")
+
+ @staticmethod
+ def get_parser():
+ """Init the option parser."""
+ parser = ArgumentParser(
+ description='Manipulate with GNU Radio modules source code tree. ' + \
+ 'Call it withou options to run specified command interactively',
+ formatter_class=RawDescriptionHelpFormatter)
+ parser.add_argument("-d", "--directory", default=".",
+ help="Base directory of the module. Defaults to the cwd.")
+ parser.add_argument("--skip-lib", action="store_true",
+ help="Don't do anything in the lib/ subdirectory.")
+ parser.add_argument("--skip-swig", action="store_true",
+ help="Don't do anything in the swig/ subdirectory.")
+ parser.add_argument("--skip-python", action="store_true",
+ help="Don't do anything in the python/ subdirectory.")
+ parser.add_argument("--skip-grc", action="store_true",
+ help="Don't do anything in the grc/ subdirectory.")
+ parser.add_argument("--scm-mode", choices=('yes', 'no', 'auto'),
+ default=gr.prefs().get_string('modtool', 'scm_mode', 'no'),
+ help="Use source control management [ yes | no | auto ]).")
+ parser.add_argument("-y", "--yes", action="store_true",
+ help="Answer all questions with 'yes'. This can overwrite and delete your files, so be careful.")
return parser
- def setup(self, options, args):
+ def setup(self, options):
""" Initialise all internal variables, such as the module name etc. """
self._dir = options.directory
if not self._check_directory(self._dir):
raise ModToolException('No GNU Radio module found in the given directory.')
- if options.module_name is not None:
+ if hasattr(options, 'module_name') and options.module_name is not None:
self._info['modname'] = options.module_name
else:
self._info['modname'] = get_modname()
if self._info['modname'] is None:
raise ModToolException('No GNU Radio module found in the given directory.')
- print "GNU Radio module name identified: " + self._info['modname']
+ print("GNU Radio module name identified: " + self._info['modname'])
if self._info['version'] == '36' and (
os.path.isdir(os.path.join('include', self._info['modname'])) or
os.path.isdir(os.path.join('include', 'gnuradio', self._info['modname']))
@@ -103,7 +113,7 @@ class ModTool(object):
self._skip_subdirs['swig'] = True
if options.skip_grc or not self._has_subdirs['grc']:
self._skip_subdirs['grc'] = True
- self._info['blockname'] = options.block_name
+ self._info['blockname'] = options.blockname
self._setup_files()
self._info['yes'] = options.yes
self.options = options
@@ -139,7 +149,7 @@ class ModTool(object):
else:
self.scm = SCMRepoFactory(self.options, '.').make_empty_scm_manager()
if self.scm is None:
- print "Error: Can't set up SCM."
+ print("Error: Can't set up SCM.")
exit(1)
def _check_directory(self, directory):
@@ -151,7 +161,7 @@ class ModTool(object):
files = os.listdir(directory)
os.chdir(directory)
except OSError:
- print "Can't read or chdir to directory %s." % directory
+ print("Can't read or chdir to directory %s." % directory)
return False
self._info['is_component'] = False
for f in files:
@@ -165,11 +175,11 @@ class ModTool(object):
has_makefile = True
# TODO search for autofoo
elif os.path.isdir(f):
- if f in self._has_subdirs.keys():
+ if (f in list(self._has_subdirs.keys())):
self._has_subdirs[f] = True
else:
self._skip_subdirs[f] = True
- return bool(has_makefile and (self._has_subdirs.values()))
+ return bool(has_makefile and (list(self._has_subdirs.values())))
def _get_mainswigfile(self):
""" Find out which name the main SWIG file has. In particular, is it
@@ -182,20 +192,19 @@ class ModTool(object):
return fname
return None
- def run(self):
+ def run(self, options):
""" Override this. """
- pass
+ raise NotImplementedError('Module implementation missing')
-def get_class_dict(the_globals):
- " Return a dictionary of the available commands in the form command->class "
- classdict = {}
- for g in the_globals:
+def get_modtool_modules(all_objects):
+ """Return list with all modtool modules."""
+ modules = []
+ for o in all_objects:
try:
- if issubclass(g, ModTool):
- classdict[g.name] = g
- for alias in g.aliases:
- classdict[alias] = g
+ if issubclass(o, ModTool) and o != ModTool:
+ modules.append(o)
except (TypeError, AttributeError):
pass
- return classdict
+ return modules
+