diff options
Diffstat (limited to 'grc/core')
-rw-r--r-- | grc/core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | grc/core/Config.py | 55 | ||||
-rw-r--r-- | grc/core/FlowGraph.py | 2 | ||||
-rw-r--r-- | grc/core/Param.py | 34 | ||||
-rw-r--r-- | grc/core/Platform.py | 58 | ||||
-rw-r--r-- | grc/core/generator/Generator.py | 9 | ||||
-rw-r--r-- | grc/core/utils/epy_block_io.py | 15 |
7 files changed, 95 insertions, 79 deletions
diff --git a/grc/core/CMakeLists.txt b/grc/core/CMakeLists.txt index 623d912d47..8aee8116b2 100644 --- a/grc/core/CMakeLists.txt +++ b/grc/core/CMakeLists.txt @@ -20,6 +20,7 @@ ######################################################################## GR_PYTHON_INSTALL(FILES Block.py + Config.py Connection.py Constants.py Element.py diff --git a/grc/core/Config.py b/grc/core/Config.py new file mode 100644 index 0000000000..ac38d9978c --- /dev/null +++ b/grc/core/Config.py @@ -0,0 +1,55 @@ +""" +Copyright 2016 Free Software Foundation, Inc. +This file is part of GNU Radio + +GNU Radio Companion 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 2 +of the License, or (at your option) any later version. + +GNU Radio Companion 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 this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +""" + +import os +from os.path import expanduser, normpath, expandvars, exists + + +class Config(object): + + key = 'grc' + name = 'GNU Radio Companion (no gui)' + license = __doc__.strip() + website = 'http://gnuradio.org' + + hier_block_lib_dir = os.environ.get('GRC_HIER_PATH', expanduser('~/.grc_gnuradio')) + + def __init__(self, prefs_file, version, version_parts=None): + self.prefs = prefs_file + self.version = version + self.version_parts = version_parts or version[1:].split('-', 1)[0].split('.')[:3] + + @property + def block_paths(self): + path_list_sep = {'/': ':', '\\': ';'}[os.path.sep] + + paths_sources = ( + self.hier_block_lib_dir, + os.environ.get('GRC_BLOCKS_PATH', ''), + self.prefs.get_string('grc', 'local_blocks_path', ''), + self.prefs.get_string('grc', 'global_blocks_path', ''), + ) + + collected_paths = sum((paths.split(path_list_sep) + for paths in paths_sources), []) + + valid_paths = [normpath(expanduser(expandvars(path))) + for path in collected_paths if exists(path)] + + return valid_paths diff --git a/grc/core/FlowGraph.py b/grc/core/FlowGraph.py index 3d744f74c4..af6d35f7fb 100644 --- a/grc/core/FlowGraph.py +++ b/grc/core/FlowGraph.py @@ -370,7 +370,7 @@ class FlowGraph(Element): n['block'] = [b.export_data() for b in blocks] n['connection'] = [c.export_data() for c in connections] instructions = odict({ - 'created': '.'.join(self.get_parent().config.version2), + 'created': '.'.join(self.get_parent().config.version_parts), 'format': FLOW_GRAPH_FILE_FORMAT_VERSION, }) return odict({'flow_graph': n, '_instructions': instructions}) diff --git a/grc/core/Param.py b/grc/core/Param.py index 0be30975e0..99106defe4 100644 --- a/grc/core/Param.py +++ b/grc/core/Param.py @@ -20,7 +20,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import ast import re -from gnuradio import eng_notation, gr from . import Constants from .Constants import VECTOR_TYPES, COMPLEX_TYPES, REAL_TYPES, INT_TYPES @@ -31,8 +30,12 @@ from .utils import odict import __builtin__ -ID_BLACKLIST = ['self', 'options', 'gr', 'blks2', 'wxgui', 'wx', 'math', 'forms', 'firdes'] + \ - filter(lambda x: not x.startswith('_'), dir(gr.top_block())) + dir(__builtin__) +ID_BLACKLIST = ['self', 'options', 'gr', 'blks2', 'wxgui', 'wx', 'math', 'forms', 'firdes'] + dir(__builtin__) +try: + from gnuradio import gr + ID_BLACKLIST.extend(attr for attr in dir(gr.top_block()) if not attr.startswith('_')) +except ImportError: + pass _check_id_matcher = re.compile('^[a-z|A-Z]\w*$') _show_id_matcher = re.compile('^(variable\w*|parameter|options|notebook)$') @@ -51,22 +54,23 @@ def _get_elem(lst, key): def num_to_str(num): """ Display logic for numbers """ + def eng_notation(value, fmt='g'): + """Convert a number to a string in engineering notation. E.g., 5e-9 -> 5n""" + template = '{:' + fmt + '}{}' + magnitude = abs(value) + for exp, symbol in zip(range(9, -15-1, -3), 'GMk munpf'): + factor = 10 ** exp + if magnitude >= factor: + return template.format(value / factor, symbol.strip()) + return template.format(value, '') + if isinstance(num, COMPLEX_TYPES): num = complex(num) # Cast to python complex if num == 0: return '0' - elif num.imag == 0: - # Value is real - return '{}'.format(eng_notation.num_to_str(num.real)) - elif num.real == 0: - # Value is imaginary - return '{}j'.format(eng_notation.num_to_str(num.imag)) - elif num.imag < 0: - return '{}-{}j'.format(eng_notation.num_to_str(num.real), - eng_notation.num_to_str(abs(num.imag))) - else: - return '{}+{}j'.format(eng_notation.num_to_str(num.real), - eng_notation.num_to_str(num.imag)) + output = eng_notation(num.real) if num.real else '' + output += eng_notation(num.imag, '+g' if output else 'g') + 'j' if num.imag else '' + return output else: return str(num) diff --git a/grc/core/Platform.py b/grc/core/Platform.py index 97e2b38309..4c1e6f471a 100644 --- a/grc/core/Platform.py +++ b/grc/core/Platform.py @@ -19,10 +19,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import os import sys -from gnuradio import gr from . import ParseXML, Messages, Constants +from .Config import Config from .Element import Element from .generator import Generator from .FlowGraph import FlowGraph @@ -34,59 +34,9 @@ from .Param import Param from .utils import odict, extract_docs -from os.path import expanduser, normpath, expandvars, dirname, exists - - -class Config(object): - - key = 'grc' - name = 'GNU Radio Companion' - license = __doc__.strip() - website = 'http://gnuradio.org' - version = gr.version() - version2 = (gr.major_version(), gr.api_version(), gr.minor_version()) - - hier_block_lib_dir = os.environ.get('GRC_HIER_PATH', expanduser('~/.grc_gnuradio')) - - def __init__(self): - self.prefs = self._get_prefs() - - if not exists(self.hier_block_lib_dir): - os.mkdir(self.hier_block_lib_dir) - - @staticmethod - def _get_prefs(): - try: - from gnuradio import gr - prefs = gr.prefs() - except ImportError: - import ConfigParser - prefs = ConfigParser.ConfigParser() - - return prefs - - @property - def block_paths(self): - path_list_sep = {'/': ':', '\\': ';'}[os.path.sep] - - paths_sources = ( - self.hier_block_lib_dir, - os.environ.get('GRC_BLOCKS_PATH', ''), - self.prefs.get_string('grc', 'local_blocks_path', ''), - self.prefs.get_string('grc', 'global_blocks_path', ''), - ) - - collected_paths = sum((paths.split(path_list_sep) - for paths in paths_sources), []) - - valid_paths = [normpath(expanduser(expandvars(path))) - for path in collected_paths if exists(path)] - - return valid_paths - - class Platform(Element): + Config = Config Generator = Generator FlowGraph = FlowGraph Connection = Connection @@ -96,11 +46,11 @@ class Platform(Element): is_platform = True - def __init__(self): + def __init__(self, *args, **kwargs): """ Make a platform for GNU Radio """ Element.__init__(self) - self.config = Config() + self.config = self.Config(*args, **kwargs) self.block_docstrings = {} self.block_docstrings_loaded_callback = lambda: None # dummy to be replaced by BlockTreeWindow diff --git a/grc/core/generator/Generator.py b/grc/core/generator/Generator.py index 5c2fa6e0d5..91671072d6 100644 --- a/grc/core/generator/Generator.py +++ b/grc/core/generator/Generator.py @@ -259,9 +259,12 @@ class HierBlockGenerator(TopBlockGenerator): TopBlockGenerator.__init__(self, flow_graph, file_path) platform = flow_graph.get_parent() + hier_block_lib_dir = platform.config.hier_block_lib_dir + if not os.path.exists(hier_block_lib_dir): + os.mkdir(hier_block_lib_dir) + self._mode = HIER_BLOCK_FILE_MODE - self.file_path = os.path.join(platform.config.hier_block_lib_dir, - self._flow_graph.get_option('id') + '.py') + self.file_path = os.path.join(hier_block_lib_dir, self._flow_graph.get_option('id') + '.py') self._file_path_xml = self.file_path + '.xml' def get_file_path_xml(self): @@ -388,4 +391,4 @@ class QtHierBlockGenerator(HierBlockGenerator): "\n#set $win = 'self.%s' % $id" "\n${gui_hint()($win)}" ) - return n
\ No newline at end of file + return n diff --git a/grc/core/utils/epy_block_io.py b/grc/core/utils/epy_block_io.py index c070e1c3f3..df3a4bbc3e 100644 --- a/grc/core/utils/epy_block_io.py +++ b/grc/core/utils/epy_block_io.py @@ -2,9 +2,6 @@ import inspect import collections -from gnuradio import gr -import pmt - TYPE_MAP = { 'complex64': 'complex', 'complex': 'complex', @@ -31,21 +28,27 @@ def _ports(sigs, msgs): return ports -def _blk_class(source_code): +def _find_block_class(source_code, cls): ns = {} try: exec source_code in ns except Exception as e: raise ValueError("Can't interpret source code: " + str(e)) for var in ns.itervalues(): - if inspect.isclass(var) and issubclass(var, gr.gateway.gateway_block): + if inspect.isclass(var) and issubclass(var, cls): return var raise ValueError('No python block class found in code') def extract(cls): + try: + from gnuradio import gr + import pmt + except ImportError: + raise EnvironmentError("Can't import GNU Radio") + if not inspect.isclass(cls): - cls = _blk_class(cls) + cls = _find_block_class(cls, gr.gateway.gateway_block) spec = inspect.getargspec(cls.__init__) defaults = map(repr, spec.defaults or ()) |