Example #1
1
File: ast.py Project: bazsi/codega
 def __init__(self, name, properties, base=AstNodeBase):
     self.__base = base
     self.__name = name
     self.__properties = OrderedDict(properties)
Example #2
0
    def test_ordering(self):
        od = OrderedDict()
        keys = [ 'a', 'A', 'b', 'B', 'c', 'C' ] + range(100) + range(500, 400, -1)
        for i in keys:
            od[i] = '--%r' % i

        for index, key in zip(od.keys(), keys):
            self.assertEqual(index, key)

        # overwriting should not affect the order
        od['C'] = 1000

        for index, check in zip(od.keys(), keys):
            self.assertEqual(index, check)

        pos = len(od._keyorder)
        od['x'] = 1
        self.assertEqual(od._keyorder[pos], 'x')
Example #3
0
File: ast.py Project: bazsi/codega
class Info(object):
    '''Stores information about an AST class'''

    def __init__(self, name, properties, base=AstNodeBase):
        self.__base = base
        self.__name = name
        self.__properties = OrderedDict(properties)

    @property
    def name(self):
        return self.__name

    @property
    def bases(self):
        return self.__bases

    @property
    def properties(self):
        return OrderedDict(self.__properties)

    def map_properties(self, obj, args, kwargs):
        res = OrderedDict()
        args = list(args)
        kwargs = dict(kwargs)

        for name, klass in self.__properties.iteritems():
            if kwargs and name in kwargs:
                res[name] = kwargs.pop(name)

            elif args:
                res[name] = args[0]
                del args[0]

            elif klass == OPTIONAL:
                res[name] = None

            else:
                raise ValueError("Cannot handle required argument %s" % name)

        setattr(obj, 'ast_properties', res)
        for key, value in res.iteritems():
            assert not hasattr(obj, key)
            setattr(obj, key, value)

    def get_class(self, metainfo):
        if not metainfo.has_class(self.__name):
            self.__create_class(metainfo)

        return metainfo.get_class(self.__name)

    def __create_class(self, metainfo):
        members = {}
        members['ast_class_info'] = self
        members['ast_name'] = self.__name

        return metainfo.define_class(self.__name, (self.__base,), members)
Example #4
0
File: ast.py Project: bazsi/codega
    def map_properties(self, obj, args, kwargs):
        res = OrderedDict()
        args = list(args)
        kwargs = dict(kwargs)

        for name, klass in self.__properties.iteritems():
            if kwargs and name in kwargs:
                res[name] = kwargs.pop(name)

            elif args:
                res[name] = args[0]
                del args[0]

            elif klass == OPTIONAL:
                res[name] = None

            else:
                raise ValueError("Cannot handle required argument %s" % name)

        setattr(obj, 'ast_properties', res)
        for key, value in res.iteritems():
            assert not hasattr(obj, key)
            setattr(obj, key, value)
Example #5
0
    def test_update(self):
        od1 = OrderedDict()
        for i in range(10):
            od1[i] = 100 * i

        keys1 = od1.keys()

        od2 = OrderedDict()
        for i in range(10, 20):
            od2[i] = 100 * i

        od1.update(od2)
        for index, check in zip(od1.keys(), range(20)):
            self.assertEqual(index, check)

        od1._keyorder = keys1

        od1.update(0, od2)
        for index, check in zip(od1.keys(), range(10, 20) + range(10)):
            self.assertEqual(index, check)

        self.assertRaises(TypeError, od1.update, 0, od1, 1)
Example #6
0
    def test_insert(self):
        od = OrderedDict()
        for i in range(10):
            od[i] = 100 * i

        # New item
        od.insert(0, -1, 0)
        self.assertEqual(od.keys(), [-1] + range(10))
        od._keyorder = range(10)

        # Reordering
        #  a., insert before the old position
        od.insert(0, 5, 0)
        self.assertEqual(od.keys(), [5] + range(5) + range(6, 10))
        od._keyorder = range(10)

        #  b., insert after the old position
        od.insert(7, 5, 0)
        self.assertEqual(od.keys(), range(5) + [6, 5, 7, 8, 9])
        od._keyorder = range(10)