commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r10775 - in gnuradio/branches/developers/jblum/grc/grc


From: jblum
Subject: [Commit-gnuradio] r10775 - in gnuradio/branches/developers/jblum/grc/grc/src: platforms/base platforms/gui platforms/python utils
Date: Sun, 5 Apr 2009 00:40:08 -0600 (MDT)

Author: jblum
Date: 2009-04-05 00:40:07 -0600 (Sun, 05 Apr 2009)
New Revision: 10775

Added:
   gnuradio/branches/developers/jblum/grc/grc/src/utils/odict.py
Modified:
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Block.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/FlowGraph.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Param.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Platform.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Port.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Block.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/FlowGraph.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/python/Block.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/python/Port.py
   gnuradio/branches/developers/jblum/grc/grc/src/utils/Makefile.am
   gnuradio/branches/developers/jblum/grc/grc/src/utils/__init__.py
Log:
Added utility methods to odict find and finall.
This replaces listify and exists_or_else and is much more useful.



Modified: gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Block.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Block.py      
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Block.py      
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -17,7 +17,6 @@
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 """
 
-from ... import utils
 from ... utils import odict
 from Element import Element
 from Param import Param
@@ -59,33 +58,33 @@
                #build the block
                Element.__init__(self, flow_graph)
                #grab the data
-               params = utils.listify(n, 'param')
-               sources = utils.listify(n, 'source')
-               sinks = utils.listify(n, 'sink')
-               self._name = n['name']
-               self._key = n['key']
-               self._category = utils.exists_or_else(n, 'category', '')
-               self._block_wrapper_path = n['block_wrapper_path']
+               params = n.findall('param')
+               sources = n.findall('source')
+               sinks = n.findall('sink')
+               self._name = n.find('name')
+               self._key = n.find('key')
+               self._category = n.find('category') or ''
+               self._block_wrapper_path = n.find('block_wrapper_path')
                #create the param objects
                self._params = odict()
                #add the id param
                self._params['id'] = self.get_parent().get_parent().Param(
                        self,
-                       {
+                       odict({
                                'name': 'ID',
                                'key': 'id',
                                'type': 'id',
-                       }
+                       })
                )
                self._params['_enabled'] = self.get_parent().get_parent().Param(
                        self,
-                       {
+                       odict({
                                'name': 'Enabled',
                                'key': '_enabled',
                                'type': 'raw',
                                'value': 'True',
                                'hide': 'all',
-                       }
+                       })
                )
                for param in map(lambda n: 
self.get_parent().get_parent().Param(self, n), params):
                        key = param.get_key()
@@ -250,13 +249,11 @@
                Any param keys that do not exist will be ignored.
                @param n the nested data odict
                """
-               params_n = utils.listify(n, 'param')
+               params_n = n.findall('param')
                for param_n in params_n:
-                       #key and value must exist in the n data
-                       if 'key' in param_n.keys() and 'value' in 
param_n.keys():
-                               key = param_n['key']
-                               value = param_n['value']
-                               #the key must exist in this block's params
-                               if key in self.get_param_keys():
-                                       self.get_param(key).set_value(value)
+                       key = param_n.find('key')
+                       value = param_n.find('value')
+                       #the key must exist in this block's params
+                       if key in self.get_param_keys():
+                               self.get_param(key).set_value(value)
                self.validate()

Modified: 
gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/FlowGraph.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/FlowGraph.py  
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/FlowGraph.py  
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -17,7 +17,6 @@
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 """
 
-from ... import utils
 from ... utils import odict
 from Element import Element
 from Block import Block
@@ -37,7 +36,7 @@
                #initialize
                Element.__init__(self, platform)
                #inital blank import
-               self.import_data({'flow_graph': {}})
+               self.import_data()
 
        def _get_unique_id(self, base_id=''):
                """
@@ -179,9 +178,9 @@
                n['timestamp'] = time.ctime()
                n['block'] = [block.export_data() for block in 
self.get_blocks()]
                n['connection'] = [connection.export_data() for connection in 
self.get_connections()]
-               return {'flow_graph': n}
+               return odict({'flow_graph': n})
 
-       def import_data(self, n):
+       def import_data(self, n=None):
                """
                Import blocks and connections into this flow graph.
                Clear this flowgraph of all previous blocks and connections.
@@ -190,19 +189,16 @@
                """
                #remove previous elements
                self._elements = list()
-               #the flow graph tag must exists, or use blank data
-               if 'flow_graph' in n.keys(): fg_n = n['flow_graph']
-               else:
-                       Messages.send_error_load('Flow graph data not found, 
loading blank flow graph.')
-                       fg_n = {}
-               blocks_n = utils.listify(fg_n, 'block')
-               connections_n = utils.listify(fg_n, 'connection')
+               #use blank data if none provided
+               fg_n = n and n.find('flow_graph') or odict()
+               blocks_n = fg_n.findall('block')
+               connections_n = fg_n.findall('connection')
                #create option block
                self._options_block = self.get_parent().get_new_block(self, 
'options')
                self._options_block.get_param('id').set_value('options')
                #build the blocks
                for block_n in blocks_n:
-                       key = block_n['key']
+                       key = block_n.find('key')
                        if key == 'options': block = self._options_block
                        else: block = self.get_new_block(key)
                        #only load the block when the block key was valid
@@ -210,21 +206,14 @@
                        else: Messages.send_error_load('Block key "%s" not 
found in %s'%(key, self.get_parent()))
                #build the connections
                for connection_n in connections_n:
-                       #test that the data tags exist
-                       try:
-                               assert('source_block_id' in connection_n.keys())
-                               assert('sink_block_id' in connection_n.keys())
-                               assert('source_key' in connection_n.keys())
-                               assert('sink_key' in connection_n.keys())
-                       except AssertionError: continue
                        #try to make the connection
                        try:
                                #get the block ids
-                               source_block_id = 
connection_n['source_block_id']
-                               sink_block_id = connection_n['sink_block_id']
+                               source_block_id = 
connection_n.find('source_block_id')
+                               sink_block_id = 
connection_n.find('sink_block_id')
                                #get the port keys
-                               source_key = connection_n['source_key']
-                               sink_key = connection_n['sink_key']
+                               source_key = connection_n.find('source_key')
+                               sink_key = connection_n.find('sink_key')
                                #verify the blocks
                                block_ids = map(lambda b: b.get_id(), 
self.get_blocks())
                                assert(source_block_id in block_ids)

Modified: gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Param.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Param.py      
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Param.py      
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -17,7 +17,6 @@
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 """
 
-from ... import utils
 from ... utils import odict
 from Element import Element
 import pygtk
@@ -72,12 +71,12 @@
 
 class Option(Element):
 
-       def __init__(self, param, name, key, opts):
+       def __init__(self, param, n):
                Element.__init__(self, param)
-               self._name = name
-               self._key = key
+               self._name = n.find('name')
+               self._key = n.find('key')
                self._opts = dict()
-               for opt in opts:
+               for opt in n.findall('opt'):
                        #separate the key:value
                        try: key, value = opt.split(':')
                        except: self._exit_with_error('Error separating "%s" 
into key:value'%opt)
@@ -88,9 +87,7 @@
                        self._opts[key] = value
 
        def __str__(self): return 'Option %s(%s)'%(self.get_name(), 
self.get_key())
-
        def get_name(self): return self._name
-
        def get_key(self): return self._key
 
        ##############################################
@@ -100,29 +97,6 @@
        def get_opt(self, key): return self._opts[key]
        def get_opts(self): return self._opts.values()
 
-       ##############################################
-       ## Static Make Methods
-       ##############################################
-       def make_option_from_n(param, n):
-               """
-               Make a new option from nested data.
-               @param param the parent element
-               @param n the nested odict
-               @return a new option
-               """
-               #grab the data
-               name = n['name']
-               key = n['key']
-               opts = utils.listify(n, 'opt')
-               #build the option
-               return Option(
-                       param=param,
-                       name=name,
-                       key=key,
-                       opts=opts,
-               )
-       make_option_from_n = staticmethod(make_option_from_n)
-
 class Param(Element):
 
        ##possible param types
@@ -136,21 +110,16 @@
                @return a new param
                """
                #grab the data
-               name = n['name']
-               key = n['key']
-               value = utils.exists_or_else(n, 'value', '')
-               type = n['type']
-               hide = utils.exists_or_else(n, 'hide', '')
-               options = utils.listify(n, 'option')
+               self._name = n.find('name')
+               self._key = n.find('key')
+               value = n.find('value') or ''
+               self._type = n.find('type')
+               self._hide = n.find('hide') or ''
                #build the param
                Element.__init__(self, block)
-               self._name = name
-               self._key = key
-               self._type = type
-               self._hide = hide
                #create the Option objects from the n data
                self._options = odict()
-               for option in map(lambda o: Option.make_option_from_n(self, o), 
options):
+               for option in map(lambda o: Option(self, o), 
n.findall('option')):
                        key = option.get_key()
                        #test against repeated keys
                        try: assert(key not in self.get_option_keys())

Modified: 
gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Platform.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Platform.py   
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Platform.py   
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -19,7 +19,6 @@
 
 import os
 from ... utils import ParseXML
-from ... import utils
 from Element import Element as _Element
 from FlowGraph import FlowGraph as _FlowGraph
 from Connection import Connection as _Connection
@@ -73,17 +72,17 @@
                """
                try: ParseXML.validate_dtd(f, self._block_dtd)
                except ParseXML.XMLSyntaxError, e: self._exit_with_error('Block 
definition "%s" failed: \n\t%s'%(f, e))
-               for n in utils.listify(ParseXML.from_file(f), 'block'):
-                       #inject block wrapper path
-                       n['block_wrapper_path'] = f
-                       block = self.Block(self._flow_graph, n)
-                       key = block.get_key()
-                       #test against repeated keys
-                       try: assert(key not in self.get_block_keys())
-                       except AssertionError: self._exit_with_error('Key "%s" 
already exists in blocks'%key)
-                       #store the block
-                       self._blocks[key] = block
-                       self._blocks_n[key] = n
+               n = ParseXML.from_file(f).find('block')
+               #inject block wrapper path
+               n['block_wrapper_path'] = f
+               block = self.Block(self._flow_graph, n)
+               key = block.get_key()
+               #test against repeated keys
+               try: assert(key not in self.get_block_keys())
+               except AssertionError: self._exit_with_error('Key "%s" already 
exists in blocks'%key)
+               #store the block
+               self._blocks[key] = block
+               self._blocks_n[key] = n
 
        def load_block_tree(self, block_tree):
                """
@@ -95,19 +94,19 @@
                #recursive function to load categories and blocks
                def load_category(cat_n, parent=''):
                        #add this category
-                       parent = '%s/%s'%(parent, cat_n['name'])
+                       parent = '%s/%s'%(parent, cat_n.find('name'))
                        block_tree.add_block(parent)
                        #recursive call to load sub categories
-                       map(lambda c: load_category(c, parent), 
utils.listify(cat_n, 'cat'))
+                       map(lambda c: load_category(c, parent), 
cat_n.findall('cat'))
                        #add blocks in this category
-                       for block_key in utils.listify(cat_n, 'block'):
+                       for block_key in cat_n.findall('block'):
                                block_tree.add_block(parent, 
self.get_block(block_key))
                #load the block tree
                f = self._block_tree
                try: ParseXML.validate_dtd(f, BLOCK_TREE_DTD)
                except ParseXML.XMLSyntaxError, e: self._exit_with_error('Block 
tree "%s" failed: \n\t%s'%(f, e))
                #add all blocks in the tree
-               load_category(ParseXML.from_file(f)['cat'])
+               load_category(ParseXML.from_file(f).find('cat'))
                #add all other blocks, use the catgory
                for block in self.get_blocks():
                        #blocks with empty categories are in the xml block tree 
or hidden

Modified: gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Port.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Port.py       
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/base/Port.py       
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -17,7 +17,6 @@
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 """
 
-from ... import utils
 from Element import Element
 
 class Port(Element):

Modified: gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Block.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Block.py       
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Block.py       
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 """
-Copyright 2007 Free Software Foundation, Inc.
+Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -20,6 +20,7 @@
 from Element import Element
 import Utils
 import Colors
+from ... utils import odict
 from ... gui.Constants import BORDER_PROXIMITY_SENSITIVITY
 from Constants import \
        BLOCK_FONT, BLOCK_LABEL_PADDING, \
@@ -41,23 +42,23 @@
                #add the position param
                self._params['_coordinate'] = 
self.get_parent().get_parent().Param(
                        self,
-                       {
+                       odict({
                                'name': 'GUI Coordinate',
                                'key': '_coordinate',
                                'type': 'raw',
                                'value': '(0, 0)',
                                'hide': 'all',
-                       }
+                       })
                )
                self._params['_rotation'] = 
self.get_parent().get_parent().Param(
                        self,
-                       {
+                       odict({
                                'name': 'GUI Rotation',
                                'key': '_rotation',
                                'type': 'raw',
                                'value': '0',
                                'hide': 'all',
-                       }
+                       })
                )
                Element.__init__(self)
 

Modified: 
gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/FlowGraph.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/FlowGraph.py   
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/FlowGraph.py   
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 """
-Copyright 2007 Free Software Foundation, Inc.
+Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -27,7 +27,6 @@
        ELEMENT_DELETE
 import Colors
 import Utils
-from ... import utils
 from ... gui.ParamsDialog import ParamsDialog
 from Element import Element
 from .. base import FlowGraph as _FlowGraph
@@ -137,15 +136,15 @@
                y_off = v_adj.get_value() - y_min + v_adj.page_size/4
                #create blocks
                for block_n in blocks_n:
-                       block_key = block_n['key']
+                       block_key = block_n.find('key')
                        if block_key == 'options': continue
                        block = self.get_new_block(block_key)
                        selected.add(block)
                        #set params
-                       params_n = utils.listify(block_n, 'param')
+                       params_n = block_n.findall('param')
                        for param_n in params_n:
-                               param_key = param_n['key']
-                               param_value = param_n['value']
+                               param_key = param_n.find('key')
+                               param_value = param_n.find('value')
                                #setup id parameter
                                if param_key == 'id':
                                        old_id2block[param_value] = block
@@ -160,8 +159,8 @@
                self.update()
                #create connections
                for connection_n in connections_n:
-                       source = 
old_id2block[connection_n['source_block_id']].get_source(connection_n['source_key'])
-                       sink = 
old_id2block[connection_n['sink_block_id']].get_sink(connection_n['sink_key'])
+                       source = 
old_id2block[connection_n.find('source_block_id')].get_source(connection_n.find('source_key'))
+                       sink = 
old_id2block[connection_n.find('sink_block_id')].get_sink(connection_n.find('sink_key'))
                        self.connect(source, sink)
                #set all pasted elements selected
                for block in selected: selected = 
selected.union(set(block.get_connections()))

Modified: 
gnuradio/branches/developers/jblum/grc/grc/src/platforms/python/Block.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/python/Block.py    
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/python/Block.py    
2009-04-05 06:40:07 UTC (rev 10775)
@@ -19,7 +19,6 @@
 
 from .. base.Block import Block as _Block
 from utils import extract_docs
-from ... import utils
 
 class Block(_Block):
 
@@ -36,11 +35,11 @@
                @return block a new block
                """
                #grab the data
-               doc = utils.exists_or_else(n, 'doc', '')
-               imports = map(lambda i: i.strip(), utils.listify(n, 'import'))
-               make = n['make']
-               checks = utils.listify(n, 'check')
-               callbacks = utils.listify(n, 'callback')
+               doc = n.find('doc') or ''
+               imports = map(lambda i: i.strip(), n.findall('import'))
+               make = n.find('make')
+               checks = n.findall('check')
+               callbacks = n.findall('callback')
                #build the block
                _Block.__init__(
                        self,

Modified: 
gnuradio/branches/developers/jblum/grc/grc/src/platforms/python/Port.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/python/Port.py     
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/python/Port.py     
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -18,7 +18,6 @@
 """
 
 from .. base.Port import Port as _Port
-from ... import utils
 import Constants
 
 class Port(_Port):
@@ -33,9 +32,9 @@
                @param n the nested odict
                @return a new port
                """
-               vlen = utils.exists_or_else(n, 'vlen', '1')
-               nports = utils.exists_or_else(n, 'nports', '')
-               optional = utils.exists_or_else(n, 'optional', '')
+               vlen = n.find('vlen') or '1'
+               nports = n.find('nports') or ''
+               optional = n.find('optional') or ''
                #build the port
                _Port.__init__(
                        self,

Modified: gnuradio/branches/developers/jblum/grc/grc/src/utils/Makefile.am
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/utils/Makefile.am    
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/utils/Makefile.am    
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 #
-# Copyright 2008 Free Software Foundation, Inc.
+# Copyright 2008, 2009 Free Software Foundation, Inc.
 # 
 # This file is part of GNU Radio
 # 
@@ -25,5 +25,6 @@
 
 ourpython_PYTHON = \
        converter.py \
+       odict.py \
        ParseXML.py \
        __init__.py

Modified: gnuradio/branches/developers/jblum/grc/grc/src/utils/__init__.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/utils/__init__.py    
2009-04-05 00:06:49 UTC (rev 10774)
+++ gnuradio/branches/developers/jblum/grc/grc/src/utils/__init__.py    
2009-04-05 06:40:07 UTC (rev 10775)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -17,66 +17,4 @@
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 """
 
-from UserDict import DictMixin
-
-class odict(DictMixin):
-
-       def __init__(self, d={}):
-               self._keys = list(d.keys())
-               self._data = dict(d.copy())
-
-       def __setitem__(self, key, value):
-               if key not in self._data:
-                       self._keys.append(key)
-               self._data[key] = value
-
-       def __getitem__(self, key):
-               return self._data[key]
-
-       def __delitem__(self, key):
-               del self._data[key]
-               self._keys.remove(key)
-
-       def keys(self):
-               return list(self._keys)
-
-       def copy(self):
-               copy_dict = odict()
-               copy_dict._data = self._data.copy()
-               copy_dict._keys = list(self._keys)
-               return copy_dict
-
-       def insert_after(self, pos_key, key, val):
-               """
-               Insert the new key, value entry after the entry given by the 
position key.
-               If the positional key is None, insert at the end.
-               @param pos_key the positional key
-               @param key the key for the new entry
-               @param val the value for the new entry
-               """
-               index = (pos_key is None) and len(self._keys) or 
self._keys.index(pos_key)
-               assert key not in self._keys
-               self._keys.insert(index+1, key)
-               self._data[key] = val
-
-       def insert_before(self, pos_key, key, val):
-               """
-               Insert the new key, value entry before the entry given by the 
position key.
-               If the positional key is None, insert at the begining.
-               @param pos_key the positional key
-               @param key the key for the new entry
-               @param val the value for the new entry
-               """
-               index = (pos_key is not None) and self._keys.index(pos_key) or 
0 
-               assert key not in self._keys
-               self._keys.insert(index, key)
-               self._data[key] = val
-
-def exists_or_else(d, key, alt):
-       if d.has_key(key): return d[key]
-       else: return alt
-
-def listify(d, key):
-       obj = exists_or_else(d, key, [])
-       if isinstance(obj, list): return obj
-       return [obj]
+from odict import odict

Copied: gnuradio/branches/developers/jblum/grc/grc/src/utils/odict.py (from rev 
10772, gnuradio/branches/developers/jblum/grc/grc/src/utils/__init__.py)
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/utils/odict.py               
                (rev 0)
+++ gnuradio/branches/developers/jblum/grc/grc/src/utils/odict.py       
2009-04-05 06:40:07 UTC (rev 10775)
@@ -0,0 +1,93 @@
+"""
+Copyright 2008, 2009 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
+"""
+
+from UserDict import DictMixin
+
+class odict(DictMixin):
+
+       def __init__(self, d={}):
+               self._keys = list(d.keys())
+               self._data = dict(d.copy())
+
+       def __setitem__(self, key, value):
+               if key not in self._data:
+                       self._keys.append(key)
+               self._data[key] = value
+
+       def __getitem__(self, key):
+               return self._data[key]
+
+       def __delitem__(self, key):
+               del self._data[key]
+               self._keys.remove(key)
+
+       def keys(self):
+               return list(self._keys)
+
+       def copy(self):
+               copy_dict = odict()
+               copy_dict._data = self._data.copy()
+               copy_dict._keys = list(self._keys)
+               return copy_dict
+
+       def insert_after(self, pos_key, key, val):
+               """
+               Insert the new key, value entry after the entry given by the 
position key.
+               If the positional key is None, insert at the end.
+               @param pos_key the positional key
+               @param key the key for the new entry
+               @param val the value for the new entry
+               """
+               index = (pos_key is None) and len(self._keys) or 
self._keys.index(pos_key)
+               assert key not in self._keys
+               self._keys.insert(index+1, key)
+               self._data[key] = val
+
+       def insert_before(self, pos_key, key, val):
+               """
+               Insert the new key, value entry before the entry given by the 
position key.
+               If the positional key is None, insert at the begining.
+               @param pos_key the positional key
+               @param key the key for the new entry
+               @param val the value for the new entry
+               """
+               index = (pos_key is not None) and self._keys.index(pos_key) or 
0 
+               assert key not in self._keys
+               self._keys.insert(index, key)
+               self._data[key] = val
+
+       def find(self, key):
+               """
+               Get the value for this key if exists.
+               @param key the key to search for
+               @return the value or None
+               """
+               if self.has_key(key): return self[key]
+               return None
+
+       def findall(self, key):
+               """
+               Get a list of values for this key.
+               @param key the key to search for
+               @return a list of values or empty list
+               """
+               obj = self.find(key)
+               if obj is None: obj = list()
+               if isinstance(obj, list): return obj
+               return [obj]


Property changes on: 
gnuradio/branches/developers/jblum/grc/grc/src/utils/odict.py
___________________________________________________________________
Added: svn:mergeinfo
   + 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]