diff options
Diffstat (limited to 'gr-utils/src')
-rw-r--r-- | gr-utils/src/python/CMakeLists.txt | 2 | ||||
-rw-r--r-- | gr-utils/src/python/gr_read_file_metadata | 87 | ||||
-rwxr-xr-x | gr-utils/src/python/grcc | 66 |
3 files changed, 155 insertions, 0 deletions
diff --git a/gr-utils/src/python/CMakeLists.txt b/gr-utils/src/python/CMakeLists.txt index 90caeb234..d2af6b952 100644 --- a/gr-utils/src/python/CMakeLists.txt +++ b/gr-utils/src/python/CMakeLists.txt @@ -50,6 +50,8 @@ GR_PYTHON_INSTALL( gr_plot_short gr_plot_qt gr_filter_design + gr_read_file_metadata + grcc DESTINATION ${GR_RUNTIME_DIR} COMPONENT "utils" ) diff --git a/gr-utils/src/python/gr_read_file_metadata b/gr-utils/src/python/gr_read_file_metadata new file mode 100644 index 000000000..cf0cd5b11 --- /dev/null +++ b/gr-utils/src/python/gr_read_file_metadata @@ -0,0 +1,87 @@ +#!/usr/bin/env python +# +# Copyright 2012 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. +# + +import sys +from optparse import OptionParser + +from gruel import pmt +from gnuradio.blocks import parse_file_metadata + +def main(filename, detached=False): + handle = open(filename, "rb") + + nheaders = 0 + nread = 0 + while(True): + # read out next header bytes + hdr_start = handle.tell() + header_str = handle.read(parse_file_metadata.HEADER_LENGTH) + if(len(header_str) == 0): + break + + # Convert from string to PMT (should be a dictionary) + try: + header = pmt.pmt_deserialize_str(header_str) + except RuntimeError: + sys.stderr.write("Could not deserialize header: invalid or corrupt data file.\n") + sys.exit(1) + + print "HEADER {0}".format(nheaders) + info = parse_file_metadata.parse_header(header, True) + + if(info["extra_len"] > 0): + extra_str = handle.read(info["extra_len"]) + if(len(extra_str) == 0): + break + + try: + extra = pmt.pmt_deserialize_str(extra_str) + except RuntimeError: + sys.stderr.write("Could not deserialize extras: invalid or corrupt data file.\n") + sys.exit(1) + + print "\nExtra Header:" + extra_info = parse_file_metadata.parse_extra_dict(extra, info, True) + + nheaders += 1 + nread += parse_file_metadata.HEADER_LENGTH + info["extra_len"] + if(not detached): + nread += info['nbytes'] + handle.seek(nread, 0) + print "\n\n" + +if __name__ == "__main__": + usage="%prog: [options] filename" + description = "Read in a GNU Radio file with meta data, extracts the header and prints it." + + parser = OptionParser(conflict_handler="resolve", + usage=usage, description=description) + parser.add_option("-D", "--detached", action="store_true", default=False, + help="Used if header is detached.") + (options, args) = parser.parse_args () + + if(len(args) < 1): + sys.stderr.write("No filename given\n") + sys.exit(1) + + filename = args[0] + main(filename, options.detached) diff --git a/gr-utils/src/python/grcc b/gr-utils/src/python/grcc new file mode 100755 index 000000000..7e5665dc0 --- /dev/null +++ b/gr-utils/src/python/grcc @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# +# Copyright 2012 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. +# + +try: + from grc.python.Platform import Platform +except ImportError: + from gnuradio.grc.python.Platform import Platform + +from optparse import OptionParser +import os, sys + +class grcc: + def __init__(self, grcfile, out_dir): + self.out_dir = out_dir + self.platform = Platform() + data = self.platform.parse_flow_graph(grcfile) + + self.fg = self.platform.get_new_flow_graph() + self.fg.import_data(data) + self.fg.validate() + + self.gen = self.platform.get_generator()(self.fg, out_dir) + self.gen.write() + + def exec_program(self): + progname = self.fg.get_option('id') + os.system("{0}/{1}.py".format(self.out_dir, progname)) + +if __name__ == "__main__": + usage="%prog: [options] filename" + description = "Compiles a GRC file (.grc) into a GNU Radio Python program. The program is stored in ~/.grc_gnuradio by default, but this location can be changed with the -d option." + + parser = OptionParser(conflict_handler="resolve", usage=usage, description=description) + parser.add_option("-d", "--directory", type="string", default='{0}/.grc_gnuradio/'.format(os.environ["HOME"]), + help="Specify the directory to output the compile program [default=%default]") + parser.add_option("-e", "--execute", action="store_true", default=False, + help="Run the program after compiling [default=%default]") + (options, args) = parser.parse_args () + + if(len(args) != 1): + sys.stderr.write("Please specify a GRC file name to compile.\n") + sys.exit(1) + + g = grcc(args[0], options.directory+"/") + + if(options.execute): + g.exec_program() |