From a62f90d8cc96b9dea9289ad6e420d1c0b16f6c36 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Thu, 24 Jan 2013 19:33:03 +0100 Subject: utils: added modtool --- gr-utils/src/python/modtool/util_functions.py | 127 ++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 gr-utils/src/python/modtool/util_functions.py (limited to 'gr-utils/src/python/modtool/util_functions.py') diff --git a/gr-utils/src/python/modtool/util_functions.py b/gr-utils/src/python/modtool/util_functions.py new file mode 100644 index 000000000..029ae04bf --- /dev/null +++ b/gr-utils/src/python/modtool/util_functions.py @@ -0,0 +1,127 @@ +""" Utility functions for gr_modtool.py """ + +import re +import sys + +### Utility functions ######################################################## +def get_command_from_argv(possible_cmds): + """ Read the requested command from argv. This can't be done with optparse, + since the option parser isn't defined before the command is known, and + optparse throws an error.""" + command = None + for arg in sys.argv: + if arg[0] == "-": + continue + else: + command = arg + if command in possible_cmds: + return arg + return None + +def append_re_line_sequence(filename, linepattern, newline): + """Detects the re 'linepattern' in the file. After its last occurrence, + paste 'newline'. If the pattern does not exist, append the new line + to the file. Then, write. """ + oldfile = open(filename, 'r').read() + lines = re.findall(linepattern, oldfile, flags=re.MULTILINE) + if len(lines) == 0: + open(filename, 'a').write(newline) + return + last_line = lines[-1] + newfile = oldfile.replace(last_line, last_line + newline + '\n') + open(filename, 'w').write(newfile) + +def remove_pattern_from_file(filename, pattern): + """ Remove all occurrences of a given pattern from a file. """ + oldfile = open(filename, 'r').read() + pattern = re.compile(pattern, re.MULTILINE) + open(filename, 'w').write(pattern.sub('', oldfile)) + +def str_to_fancyc_comment(text): + """ Return a string as a C formatted comment. """ + l_lines = text.splitlines() + outstr = "/* " + l_lines[0] + "\n" + for line in l_lines[1:]: + outstr += " * " + line + "\n" + outstr += " */\n" + return outstr + +def str_to_python_comment(text): + """ Return a string as a Python formatted comment. """ + return re.compile('^', re.MULTILINE).sub('# ', text) + +def strip_default_values(string): + """ Strip default values from a C++ argument list. """ + return re.sub(' *=[^,)]*', '', string) + +def strip_arg_types(string): + """" Strip the argument types from a list of arguments + Example: "int arg1, double arg2" -> "arg1, arg2" """ + string = strip_default_values(string) + return ", ".join([part.strip().split(' ')[-1] for part in string.split(',')]) + +def get_modname(): + """ Grep the current module's name from gnuradio.project or CMakeLists.txt """ + modname_trans = {'howto-write-a-block': 'howto'} + try: + prfile = open('gnuradio.project', 'r').read() + regexp = r'projectname\s*=\s*([a-zA-Z0-9-_]+)$' + return re.search(regexp, prfile, flags=re.MULTILINE).group(1).strip() + except IOError: + pass + # OK, there's no gnuradio.project. So, we need to guess. + cmfile = open('CMakeLists.txt', 'r').read() + regexp = r'(project\s*\(\s*|GR_REGISTER_COMPONENT\(")gr-(?P[a-zA-Z1-9-_]+)(\s*(CXX)?|" ENABLE)' + try: + modname = re.search(regexp, cmfile, flags=re.MULTILINE).group('modname').strip() + if modname in modname_trans.keys(): + modname = modname_trans[modname] + return modname + except AttributeError: + return None + +def get_class_dict(): + " Return a dictionary of the available commands in the form command->class " + classdict = {} + for g in globals().values(): + try: + if issubclass(g, ModTool): + classdict[g.name] = g + for a in g.aliases: + classdict[a] = g + except (TypeError, AttributeError): + pass + return classdict + +def is_number(s): + " Return True if the string s contains a number. " + try: + float(s) + return True + except ValueError: + return False + +def xml_indent(elem, level=0): + """ Adds indents to XML for pretty printing """ + i = "\n" + level*" " + if len(elem): + if not elem.text or not elem.text.strip(): + elem.text = i + " " + if not elem.tail or not elem.tail.strip(): + elem.tail = i + for elem in elem: + xml_indent(elem, level+1) + if not elem.tail or not elem.tail.strip(): + elem.tail = i + else: + if level and (not elem.tail or not elem.tail.strip()): + elem.tail = i + +def ask_yes_no(question, default): + """ Asks a binary question. Returns True for yes, False for no. + default is given as a boolean. """ + question += {True: ' [Y/n] ', False: ' [y/N] '}[default] + if raw_input(question).lower() != {True: 'n', False: 'y'}[default]: + return default + else: + return not default -- cgit From 9ef0f125355a4541c691f18d05ad7ca7b6f7125e Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Sun, 27 Jan 2013 16:57:04 +0100 Subject: modtool: added copyleft headers --- gr-utils/src/python/modtool/util_functions.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'gr-utils/src/python/modtool/util_functions.py') diff --git a/gr-utils/src/python/modtool/util_functions.py b/gr-utils/src/python/modtool/util_functions.py index 029ae04bf..33d8ad333 100644 --- a/gr-utils/src/python/modtool/util_functions.py +++ b/gr-utils/src/python/modtool/util_functions.py @@ -1,9 +1,28 @@ +# +# Copyright 2013 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# """ Utility functions for gr_modtool.py """ import re import sys -### Utility functions ######################################################## def get_command_from_argv(possible_cmds): """ Read the requested command from argv. This can't be done with optparse, since the option parser isn't defined before the command is known, and -- cgit From 2d695b3c4c86b5c206f95dcc1d71f97d808d98b8 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Mon, 28 Jan 2013 15:26:05 +0100 Subject: modtool: cleanup, bugfixes --- gr-utils/src/python/modtool/util_functions.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) (limited to 'gr-utils/src/python/modtool/util_functions.py') diff --git a/gr-utils/src/python/modtool/util_functions.py b/gr-utils/src/python/modtool/util_functions.py index 33d8ad333..4ca294ac3 100644 --- a/gr-utils/src/python/modtool/util_functions.py +++ b/gr-utils/src/python/modtool/util_functions.py @@ -18,11 +18,13 @@ # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # -""" Utility functions for gr_modtool.py """ +""" Utility functions for gr_modtool """ import re import sys +# None of these must depend on other modtool stuff! + def get_command_from_argv(possible_cmds): """ Read the requested command from argv. This can't be done with optparse, since the option parser isn't defined before the command is known, and @@ -38,7 +40,7 @@ def get_command_from_argv(possible_cmds): return None def append_re_line_sequence(filename, linepattern, newline): - """Detects the re 'linepattern' in the file. After its last occurrence, + """ Detects the re 'linepattern' in the file. After its last occurrence, paste 'newline'. If the pattern does not exist, append the new line to the file. Then, write. """ oldfile = open(filename, 'r').read() @@ -99,19 +101,6 @@ def get_modname(): except AttributeError: return None -def get_class_dict(): - " Return a dictionary of the available commands in the form command->class " - classdict = {} - for g in globals().values(): - try: - if issubclass(g, ModTool): - classdict[g.name] = g - for a in g.aliases: - classdict[a] = g - except (TypeError, AttributeError): - pass - return classdict - def is_number(s): " Return True if the string s contains a number. " try: -- cgit