Exemplo n.º 1
0
 def one_test_resolve(self, arglist, goodls, badls):
     if (goodls):
         for (src, wantls, wantdic) in goodls:
             nod = sparse.parse(src)
             (ls, dic) = arglist.resolve(nod)
             ils = [ resolve_value(val) for val in ls ]
             idic = dict([ (key, resolve_value(val)) for (key,val) in dic.items() ])
             self.assertEqual(ils, wantls)
             self.assertEqual(idic, wantdic)
     if (badls):
         for src in badls:
             nod = sparse.parse(src)
             self.assertRaises(ValueError, arglist.resolve, nod)
Exemplo n.º 2
0
 def one_test_resolve(self, arglist, goodls, badls):
     if (goodls):
         for (src, wantls, wantdic) in goodls:
             nod = sparse.parse(src)
             (ls, dic) = arglist.resolve(nod)
             ils = [resolve_value(val) for val in ls]
             idic = dict([(key, resolve_value(val))
                          for (key, val) in dic.items()])
             self.assertEqual(ils, wantls)
             self.assertEqual(idic, wantdic)
     if (badls):
         for src in badls:
             nod = sparse.parse(src)
             self.assertRaises(ValueError, arglist.resolve, nod)
Exemplo n.º 3
0
    def test_parse_simple(self):
        ls = [
            ('1', '1'),
            ('-23', '-23'),
            ('5.000', '5.000'),
            ('1.52', '1.52'),
            ('1462134251412', '1462134251412'),
            ('hello', 'hello'),
            ('h', 'h'),
            ('#', '#'),
            ('_hello_', '_hello_'),
            ("'hello'", 'hello'),
            ('"hello"', 'hello'),
            (u"'hello'", 'hello'),
            (u"'1.2.unic\u00F8de'", u'1.2.unic\u00F8de'),
            (u"1.2.unic\u00F8de", u'1.2.unic\u00F8de'),
            ("'esc \\' \\\" \\\\.'", "esc \' \" \\."),
            ('  1  ', '1'),
            (' \t#\t  ', '#'),
            ('  hello  ', 'hello'),
            ('  "hello"  ', 'hello'),
        ]

        for (orig, res) in ls:
            nod = parse(orig)
            self.assert_(isinstance(nod, ID))
            self.assertEqual(nod, res)
Exemplo n.º 4
0
    def get_argument_list(cls):
        """get_argument_list() -> ArgList

        Return the argument list specification for the class.
        """

        res = Agent.cached_argument_lists.get(cls)
        if res is not None:
            return res

        # Default value
        res = None
        nodestr = None

        loader = pload.PackageLoader.global_loader
        if loader:
            try:
                _, resource = loader.find_item_resources(cls)
                nodestr = resource.get_one('boodler.arguments')
            except:
                pass

        if nodestr:
            node = sparse.parse(nodestr)
            res = argdef.ArgList.from_node(node)

        Agent.cached_argument_lists[cls] = res
        return res
Exemplo n.º 5
0
    def test_parse_simple(self):
        ls = [
            ('1', '1'),
            ('-23', '-23'),
            ('5.000', '5.000'),
            ('1.52', '1.52'),
            ('1462134251412', '1462134251412'),
            ('hello', 'hello'),
            ('h', 'h'),
            ('#', '#'),
            ('_hello_', '_hello_'),
            ("'hello'", 'hello'),
            ('"hello"', 'hello'),
            (u"'hello'", 'hello'),
            (u"'1.2.unic\u00F8de'", u'1.2.unic\u00F8de'),
            (u"1.2.unic\u00F8de", u'1.2.unic\u00F8de'),
            ("'esc \\' \\\" \\\\.'", "esc \' \" \\."),
            ('  1  ', '1'),
            (' \t#\t  ', '#'),
            ('  hello  ', 'hello'),
            ('  "hello"  ', 'hello'),
        ]

        for (orig, res) in ls:
            nod = parse(orig)
            self.assert_(isinstance(nod, ID))
            self.assertEqual(nod, res)
Exemplo n.º 6
0
    def test_wrapping_node_to_val(self):
        nod = sparse.parse('foo')
        val = node_to_value(str, nod)
        self.assertEqual(type(val), str)
        self.assertEqual(val, 'foo')

        nod = sparse.parse('(foo bar)')
        val = node_to_value(list, nod)
        self.assert_(isinstance(val, ArgListWrapper))
        self.assertEqual(val.ls, ['foo', 'bar'])

        nod = sparse.parse('(foo bar)')
        val = node_to_value(tuple, nod)
        self.assertEqual(val, ('foo', 'bar'))

        nod = sparse.parse('(foo ())')
        val = node_to_value(tuple, nod)
        self.assert_(isinstance(val, ArgTupleWrapper))
Exemplo n.º 7
0
    def test_wrapping_node_to_val(self):
        nod = sparse.parse('foo')
        val = node_to_value(str, nod)
        self.assertEqual(type(val), str)
        self.assertEqual(val, 'foo')

        nod = sparse.parse('(foo bar)')
        val = node_to_value(list, nod)
        self.assert_(isinstance(val, ArgListWrapper))
        self.assertEqual(val.ls, ['foo', 'bar'])

        nod = sparse.parse('(foo bar)')
        val = node_to_value(tuple, nod)
        self.assertEqual(val, ('foo', 'bar'))

        nod = sparse.parse('(foo ())')
        val = node_to_value(tuple, nod)
        self.assert_(isinstance(val, ArgTupleWrapper))
Exemplo n.º 8
0
    def test_parse_list(self):
        ls = [
            ('()', []),
            ('  (  )    ', []),
            ('(1 "2" _3)', ['1', '2', '_3']),
            ('(()(()))', [[],[[]]]),
            ('(1()a()"x")', ['1', [], 'a', [], 'x']),
            ('(123()aa()"xx")', ['123', [], 'aa', [], 'xx']),
            ('( 45 ( 1 abc ( ) "$#$" ) ) ', ['45', ['1', 'abc', [], '$#$']]),
            ('(abc"123"456"xyz")', ['abc', '123', '456', 'xyz']),
        ]

        for (orig, res) in ls:
            nod = parse(orig)
            self.assert_(self.compare(nod, res))
Exemplo n.º 9
0
    def test_id_serialize(self):
        ls = [
            ('xyz', 'xyz'),
            ('"xyz"', 'xyz'),
            ('"xy z"', '"xy z"'),
            ('"xy=z"', '"xy=z"'),
            ('"xy(z"', '"xy(z"'),
            ('"xy)z"', '"xy)z"'),
            ('"xy\tz"', '"xy\tz"'),
            ('"xy\'z"', '"xy\'z"'),
            ('"xy\\"z"', "'xy\"z'"),
            ('"x\'y\\"z"', '"x\'y\\"z"'),
            ("'xy\\'z'", '"xy\'z"'),
            ("'xy\"z'", "'xy\"z'"),
            ("'x\\'y\"z'", '"x\'y\\"z"'),
            ('"xy\\\\z"', '"xy\\\\z"'),
            ('"x\'\\"y\\\\z"', '"x\'\\"y\\\\z"'),
        ]

        for (src, dest) in ls:
            nod = parse(src)
            self.assertEqual(nod.serialize(), dest)
            nod2 = parse(nod.serialize())
            self.assertEqual(nod, nod2)
Exemplo n.º 10
0
    def test_id_serialize(self):
        ls = [
            ('xyz', 'xyz'),
            ('"xyz"', 'xyz'),
            ('"xy z"', '"xy z"'),
            ('"xy=z"', '"xy=z"'),
            ('"xy(z"', '"xy(z"'),
            ('"xy)z"', '"xy)z"'),
            ('"xy\tz"', '"xy\tz"'),
            ('"xy\'z"', '"xy\'z"'),
            ('"xy\\"z"', "'xy\"z'"),
            ('"x\'y\\"z"', '"x\'y\\"z"'),
            ("'xy\\'z'", '"xy\'z"'),
            ("'xy\"z'", "'xy\"z'"),
            ("'x\\'y\"z'", '"x\'y\\"z"'),
            ('"xy\\\\z"', '"xy\\\\z"'),
            ('"x\'\\"y\\\\z"', '"x\'\\"y\\\\z"'),
        ]

        for (src, dest) in ls:
            nod = parse(src)
            self.assertEqual(nod.serialize(), dest)
            nod2 = parse(nod.serialize())
            self.assertEqual(nod, nod2)
Exemplo n.º 11
0
    def test_parse_list(self):
        ls = [
            ('()', []),
            ('  (  )    ', []),
            ('(1 "2" _3)', ['1', '2', '_3']),
            ('(()(()))', [[], [[]]]),
            ('(1()a()"x")', ['1', [], 'a', [], 'x']),
            ('(123()aa()"xx")', ['123', [], 'aa', [], 'xx']),
            ('( 45 ( 1 abc ( ) "$#$" ) ) ', ['45', ['1', 'abc', [], '$#$']]),
            ('(abc"123"456"xyz")', ['abc', '123', '456', 'xyz']),
        ]

        for (orig, res) in ls:
            nod = parse(orig)
            self.assert_(self.compare(nod, res))
Exemplo n.º 12
0
    def test_id_completeness(self):
        ls = [
            '', 'a', 'Abd', '    ', ' $ # ^ ',
            'a(b', 'a)b', 'a()b', 'a=b',
            '"', ' " ', "'", " ' ", ' \' " ', ' "\' \'" ',
            '\\', ' \\ \\ ', '\\\'', '\\"', '\\" \\\'',
            'a(\')b', 'a(")b', 'a("\')b', 'a("\')b\\c',
            'tab\tnew\nspace ',
            u'unicode', u'unic\u00F8de', u'unic\u0153de',
        ]

        for val in ls:
            nod = ID(val)
            st = nod.serialize()
            nod2 = parse(st)
            self.assertEqual(nod, nod2)
            self.assertEqual(val, nod2.id)
Exemplo n.º 13
0
    def test_id_completeness(self):
        ls = [
            '',
            'a',
            'Abd',
            '    ',
            ' $ # ^ ',
            'a(b',
            'a)b',
            'a()b',
            'a=b',
            '"',
            ' " ',
            "'",
            " ' ",
            ' \' " ',
            ' "\' \'" ',
            '\\',
            ' \\ \\ ',
            '\\\'',
            '\\"',
            '\\" \\\'',
            'a(\')b',
            'a(")b',
            'a("\')b',
            'a("\')b\\c',
            'tab\tnew\nspace ',
            u'unicode',
            u'unic\u00F8de',
            u'unic\u0153de',
        ]

        for val in ls:
            nod = ID(val)
            st = nod.serialize()
            nod2 = parse(st)
            self.assertEqual(nod, nod2)
            self.assertEqual(val, nod2.id)
Exemplo n.º 14
0
    def test_parse_attr(self):
        nod = parse('(a=1 b=2 c="three" d=())')
        self.assert_(isinstance(nod, List))
        self.assertEqual(len(nod), 0)
        
        self.assertEqual(len(nod.attrs), 4)
        self.assertEqual(nod.get_attr('a'), '1')
        self.assert_(isinstance(nod.get_attr('a'), ID))
        self.assertEqual(nod.get_attr('b'), '2')
        self.assertEqual(nod.get_attr('c'), 'three')
        val = nod.get_attr('d')
        self.assert_(isinstance(val, List))
        self.assertEqual(len(val), 0)
        self.assertEqual(len(val.attrs), 0)

        nod = parse('(1 x=a 2 y=(y1=y2)"three" z=(z)xyzzy(z1=z2))')
        res = ['1', '2', 'three', 'xyzzy', []]
        self.assert_(self.compare(nod, res))

        self.assertEqual(len(nod.attrs), 3)
        self.assertEqual(nod.get_attr('x'), 'a')
        self.assert_(self.compare(nod.get_attr('y'), []))
        val = nod[-1]
        self.assert_(self.compare(val, []))
        self.assertEqual(val.attrs['z1'], 'z2')

        nod = parse('("#=$" $=#)')
        self.assertEqual(len(nod), 1)
        self.assertEqual(nod[0], "#=$")
        self.assertEqual(nod.get_attr('$'), '#')
        
        nod = parse('(a = x)')
        self.assertEqual(len(nod), 0)
        self.assertEqual(nod.get_attr('a'), 'x')
        
        nod = parse('("a" = x)')
        self.assertEqual(len(nod), 0)
        self.assertEqual(nod.get_attr('a'), 'x')
        
        nod = parse('("a"=x)')
        self.assertEqual(len(nod), 0)
        self.assertEqual(nod.get_attr('a'), 'x')
Exemplo n.º 15
0
    def test_parse_attr(self):
        nod = parse('(a=1 b=2 c="three" d=())')
        self.assert_(isinstance(nod, List))
        self.assertEqual(len(nod), 0)

        self.assertEqual(len(nod.attrs), 4)
        self.assertEqual(nod.get_attr('a'), '1')
        self.assert_(isinstance(nod.get_attr('a'), ID))
        self.assertEqual(nod.get_attr('b'), '2')
        self.assertEqual(nod.get_attr('c'), 'three')
        val = nod.get_attr('d')
        self.assert_(isinstance(val, List))
        self.assertEqual(len(val), 0)
        self.assertEqual(len(val.attrs), 0)

        nod = parse('(1 x=a 2 y=(y1=y2)"three" z=(z)xyzzy(z1=z2))')
        res = ['1', '2', 'three', 'xyzzy', []]
        self.assert_(self.compare(nod, res))

        self.assertEqual(len(nod.attrs), 3)
        self.assertEqual(nod.get_attr('x'), 'a')
        self.assert_(self.compare(nod.get_attr('y'), []))
        val = nod[-1]
        self.assert_(self.compare(val, []))
        self.assertEqual(val.attrs['z1'], 'z2')

        nod = parse('("#=$" $=#)')
        self.assertEqual(len(nod), 1)
        self.assertEqual(nod[0], "#=$")
        self.assertEqual(nod.get_attr('$'), '#')

        nod = parse('(a = x)')
        self.assertEqual(len(nod), 0)
        self.assertEqual(nod.get_attr('a'), 'x')

        nod = parse('("a" = x)')
        self.assertEqual(len(nod), 0)
        self.assertEqual(nod.get_attr('a'), 'x')

        nod = parse('("a"=x)')
        self.assertEqual(len(nod), 0)
        self.assertEqual(nod.get_attr('a'), 'x')
Exemplo n.º 16
0
    def test_simple_node_to_val(self):

        goodls = [
            (int, '5', 5),
            (int, '005', 5),
            (int, '"5"', 5),
            (long, '5', 5),
            (float, '5', 5.0),
            (str, '5', '5'),
            (str, 'foo', 'foo'),
            (unicode, 'foo', 'foo'),
            (str, u'foo', u'foo'),
            (bool, '""', False),
            (bool, '0', False),
            (bool, 'no', False),
            (bool, 'FALSE', False),
            (bool, '1', True),
            (bool, 'YES', True),
            (bool, 'true', True),
            (list, '(() foo)', [[], 'foo']),
            (ListOf(), '(())', [[]]),
            (ListOf(), '(() foo)', [[], 'foo']),
            (ListOf(str), '(foo bar)', ['foo', 'bar']),
            (ListOf(int), '(1 3 2)', [1, 3, 2]),
            (ListOf(bool), '(0 1 false true NO YES)', [False, True, False, True, False, True]),
            (ListOf(int, str), '(1 2 3 4)', [1, '2', 3, '4']),
            (ListOf(str, None), '(foo (bar) baz (()))', ['foo', ['bar'], 'baz', [[]]]),
            (ListOf(TupleOf(int, str)), '((1 2) (3 4))', [(1, '2'), (3, '4')]),
            (tuple, '(() foo)', ([], 'foo')),
            (TupleOf(), '(() 1 2)', ([], '1', '2')),
            (TupleOf(int, str), '(1 2)', (1, '2')),
            (None, 'foo', 'foo'),
            (None, '()', []),
            (None, '(foo (1) ())', ['foo', ['1'], []]),
            (int, '(no=value)', None),
            (str, '(no=value)', None),
            (list, '(no=value)', None),
            (ListOf(), '(no=value)', None),
            (None, '(no=value)', None),
        ]
        
        badls = [
            (int, '()'),
            (int, '(1)'),
            (int, 'foo'),
            (int, '5.0'),
            (int, '5x'),
            (str, '()'),
            (float, '()'),
            (float, 'foo'),
            (bool, '()'),
            (list, 'foo'),
            (list, '(foo x=1)'),
            (ListOf(), 'foo'),
            (ListOf(str), 'foo'),
            (ListOf(str), '(())'),
            (ListOf(int), '(foo)'),
            (ListOf(str, int), '(foo bar)'),
            (ListOf(int, str), '(1 foo bar)'),
            (TupleOf(str, str), '(baz)'),
            (TupleOf(str, str), '(baz foo bar)'),
            (int, '(none=one)'),
            (int, '(foo none=none)'),
        ]

        for (typ, st, res) in goodls:
            nod = sparse.parse(st)
            val = node_to_value(typ, nod)
            val = resolve_value(val)
            self.assertEqual(val, res)
            self.assertEqual(type(val), type(res))
            if (type(val) == list):
                for (sub1, sub2) in zip(val, res):
                    self.assertEqual(type(sub1), type(sub2))

        for (typ, st) in badls:
            nod = sparse.parse(st)
            self.assertRaises(ValueError, node_to_value, typ, nod)
Exemplo n.º 17
0
    def test_sequence_node_to_val(self):
        typ = ListOf()
        self.assertEqual(typ.min, 0)
        self.assertEqual(typ.max, None)
        self.assertEqual(typ.repeat, 1)

        typ = TupleOf()
        self.assertEqual(typ.min, 0)
        self.assertEqual(typ.max, None)
        self.assertEqual(typ.repeat, 1)

        typ = ListOf(int, str, bool)
        self.assertEqual(typ.min, 0)
        self.assertEqual(typ.max, None)
        self.assertEqual(typ.repeat, 3)

        typ = TupleOf(int, str, bool)
        self.assertEqual(typ.min, 3)
        self.assertEqual(typ.max, 3)
        self.assertEqual(typ.repeat, 3)

        typ = ListOf(int, str, bool, min=1, max=4, repeat=2)
        self.assertEqual(typ.min, 1)
        self.assertEqual(typ.max, 4)
        self.assertEqual(typ.repeat, 2)

        typ = TupleOf(int, str, bool, min=1, max=4, repeat=2)
        self.assertEqual(typ.min, 1)
        self.assertEqual(typ.max, 4)
        self.assertEqual(typ.repeat, 2)

        typ = TupleOf(int, str, bool, max=4)
        self.assertEqual(typ.min, 0)
        self.assertEqual(typ.max, 4)
        self.assertEqual(typ.repeat, 3)

        typ = ListOf(int, str, bool, min=1)
        self.assertEqual(typ.min, 1)
        self.assertEqual(typ.max, None)
        self.assertEqual(typ.repeat, 3)

        goodls = [
            (TupleOf(str, str), '(x y)', ('x', 'y')),
            (TupleOf(str, int, max=4), '(1 2 3 4)', ('1', 2, '3', 4)),
            (TupleOf(str, min=1, max=3), '(x)', ('x', )),
            (TupleOf(str, min=1, max=3), '(x y)', ('x', 'y')),
            (TupleOf(str, min=1, max=3), '(x y z)', ('x', 'y', 'z')),
            (TupleOf(str, int, repeat=1), '(1)', ('1', )),
            (TupleOf(str, int, repeat=1), '(1 2)', ('1', 2)),
            (TupleOf(str, int, repeat=1), '(1 2 3)', ('1', 2, 3)),
            (TupleOf(str, int, bool,
                     repeat=2), '(1 2 3 4)', ('1', 2, True, 4)),
        ]
        badls = [
            (TupleOf(str, str), '(x)'),
            (TupleOf(str, str), '(x y z)'),
            (TupleOf(str, min=1, max=3), '()'),
            (TupleOf(str, min=1, max=3), '(x y z w)'),
        ]

        for (typ, st, res) in goodls:
            nod = sparse.parse(st)
            val = node_to_value(typ, nod)
            val = resolve_value(val)
            self.assertEqual(val, res)
            self.assertEqual(type(val), type(res))
            if (type(val) == list):
                for (sub1, sub2) in zip(val, res):
                    self.assertEqual(type(sub1), type(sub2))

        for (typ, st) in badls:
            nod = sparse.parse(st)
            self.assertRaises(ValueError, node_to_value, typ, nod)
Exemplo n.º 18
0
    def test_simple_node_to_val(self):

        goodls = [
            (int, '5', 5),
            (int, '005', 5),
            (int, '"5"', 5),
            (long, '5', 5),
            (float, '5', 5.0),
            (str, '5', '5'),
            (str, 'foo', 'foo'),
            (unicode, 'foo', 'foo'),
            (str, u'foo', u'foo'),
            (bool, '""', False),
            (bool, '0', False),
            (bool, 'no', False),
            (bool, 'FALSE', False),
            (bool, '1', True),
            (bool, 'YES', True),
            (bool, 'true', True),
            (list, '(() foo)', [[], 'foo']),
            (ListOf(), '(())', [[]]),
            (ListOf(), '(() foo)', [[], 'foo']),
            (ListOf(str), '(foo bar)', ['foo', 'bar']),
            (ListOf(int), '(1 3 2)', [1, 3, 2]),
            (ListOf(bool), '(0 1 false true NO YES)',
             [False, True, False, True, False, True]),
            (ListOf(int, str), '(1 2 3 4)', [1, '2', 3, '4']),
            (ListOf(str, None), '(foo (bar) baz (()))',
             ['foo', ['bar'], 'baz', [[]]]),
            (ListOf(TupleOf(int, str)), '((1 2) (3 4))', [(1, '2'), (3, '4')]),
            (tuple, '(() foo)', ([], 'foo')),
            (TupleOf(), '(() 1 2)', ([], '1', '2')),
            (TupleOf(int, str), '(1 2)', (1, '2')),
            (None, 'foo', 'foo'),
            (None, '()', []),
            (None, '(foo (1) ())', ['foo', ['1'], []]),
            (int, '(no=value)', None),
            (str, '(no=value)', None),
            (list, '(no=value)', None),
            (ListOf(), '(no=value)', None),
            (None, '(no=value)', None),
        ]

        badls = [
            (int, '()'),
            (int, '(1)'),
            (int, 'foo'),
            (int, '5.0'),
            (int, '5x'),
            (str, '()'),
            (float, '()'),
            (float, 'foo'),
            (bool, '()'),
            (list, 'foo'),
            (list, '(foo x=1)'),
            (ListOf(), 'foo'),
            (ListOf(str), 'foo'),
            (ListOf(str), '(())'),
            (ListOf(int), '(foo)'),
            (ListOf(str, int), '(foo bar)'),
            (ListOf(int, str), '(1 foo bar)'),
            (TupleOf(str, str), '(baz)'),
            (TupleOf(str, str), '(baz foo bar)'),
            (int, '(none=one)'),
            (int, '(foo none=none)'),
        ]

        for (typ, st, res) in goodls:
            nod = sparse.parse(st)
            val = node_to_value(typ, nod)
            val = resolve_value(val)
            self.assertEqual(val, res)
            self.assertEqual(type(val), type(res))
            if (type(val) == list):
                for (sub1, sub2) in zip(val, res):
                    self.assertEqual(type(sub1), type(sub2))

        for (typ, st) in badls:
            nod = sparse.parse(st)
            self.assertRaises(ValueError, node_to_value, typ, nod)
Exemplo n.º 19
0
def load_described(loader, args, wantmodule=False):
    """load_described(loader, val, wantmodule=False) -> module or Agent

    Load a named module or agent. The argument should be a string (or
    list of strings, or sparse Tree) giving a fully qualified name:

        package.name/AgentName
        package.name:version.needed/AgentName
        package.name::exact.version.number/AgentName

    To load an agent with arguments, just append the arguments.

        package.name/AgentName 0.5 2

    Just as on the command line, arguments referring to more agents
    go in parentheses:

        package.name/AgentName (package.name/AnotherAgent 2)

    To get an entire module, pass wantmodule=True. (And leave off the
    "/AgentName" part, and don't use any arguments.)

    This method is not intended for module creation time (loading one
    module which another depends on). It bypasses Boodler's dependency
    tracking.
    """

    if isinstance(args, str):
        argstr = args
        args = ['(', args, ')']
    elif isinstance(args, list):
        argstr = ' '.join(args)
        args = ['('] + args + [')']
    elif isinstance(args, tuple):
        argstr = ' '.join(args)
        args = ['('] + list(args) + [')']
    elif isinstance(args, sparse.Tree):
        argstr = args.serialize()
        # args is fine
    else:
        raise TypeError('args must be a str, list of str, or Tree')

    if not isinstance(args, sparse.Tree):
        args = sparse.parse(' '.join(args))

    if isinstance(args, sparse.ID):
        args = sparse.List(args)

    if not isinstance(args, sparse.List):
        raise ValueError('arguments must be a list')

    if not args:
        # default to the null agent, if none was given
        args = sparse.List(sparse.ID('/boodle.builtin.NullAgent'))

    classarg = args[0]
    if not isinstance(classarg, sparse.ID):
        raise ValueError('arguments must begin with a class name')

    if wantmodule:
        # clumsy!
        mod = loader.load_item_by_name(classarg.as_string() + '/')

        if not isinstance(mod, types.ModuleType):
            raise TypeError(argstr + ' is not a module')
        if len(args) > 1:
            raise ValueError('modules cannot have arguments')
        return mod

    clas = loader.load_item_by_name(classarg.as_string())

    if not isinstance(clas, type(Agent)):
        raise TypeError(argstr + ' is not a class')
    if not issubclass(clas, Agent):
        raise TypeError(argstr + ' is not an Agent class')

    arglist = clas.get_argument_list()
    if arglist is None:
        arglist = argdef.ArgList()
    (valls, valdic) = arglist.resolve(args)
    wrapper = argdef.ArgClassWrapper(clas, valls, valdic)
    return wrapper
Exemplo n.º 20
0
    def test_sequence_node_to_val(self):
        typ = ListOf()
        self.assertEqual(typ.min, 0)
        self.assertEqual(typ.max, None)
        self.assertEqual(typ.repeat, 1)

        typ = TupleOf()
        self.assertEqual(typ.min, 0)
        self.assertEqual(typ.max, None)
        self.assertEqual(typ.repeat, 1)

        typ = ListOf(int, str, bool)
        self.assertEqual(typ.min, 0)
        self.assertEqual(typ.max, None)
        self.assertEqual(typ.repeat, 3)

        typ = TupleOf(int, str, bool)
        self.assertEqual(typ.min, 3)
        self.assertEqual(typ.max, 3)
        self.assertEqual(typ.repeat, 3)

        typ = ListOf(int, str, bool, min=1, max=4, repeat=2)
        self.assertEqual(typ.min, 1)
        self.assertEqual(typ.max, 4)
        self.assertEqual(typ.repeat, 2)

        typ = TupleOf(int, str, bool, min=1, max=4, repeat=2)
        self.assertEqual(typ.min, 1)
        self.assertEqual(typ.max, 4)
        self.assertEqual(typ.repeat, 2)

        typ = TupleOf(int, str, bool, max=4)
        self.assertEqual(typ.min, 0)
        self.assertEqual(typ.max, 4)
        self.assertEqual(typ.repeat, 3)

        typ = ListOf(int, str, bool, min=1)
        self.assertEqual(typ.min, 1)
        self.assertEqual(typ.max, None)
        self.assertEqual(typ.repeat, 3)

        goodls = [
            (TupleOf(str, str), '(x y)', ('x', 'y')),
            (TupleOf(str, int, max=4), '(1 2 3 4)', ('1', 2, '3', 4)),
            (TupleOf(str, min=1, max=3), '(x)', ('x',)),
            (TupleOf(str, min=1, max=3), '(x y)', ('x', 'y')),
            (TupleOf(str, min=1, max=3), '(x y z)', ('x', 'y', 'z')),
            (TupleOf(str, int, repeat=1), '(1)', ('1',)),
            (TupleOf(str, int, repeat=1), '(1 2)', ('1', 2)),
            (TupleOf(str, int, repeat=1), '(1 2 3)', ('1', 2, 3)),
            (TupleOf(str, int, bool, repeat=2), '(1 2 3 4)', ('1', 2, True, 4)),
        ]
        badls = [
            (TupleOf(str, str), '(x)'),
            (TupleOf(str, str), '(x y z)'),
            (TupleOf(str, min=1, max=3), '()'),
            (TupleOf(str, min=1, max=3), '(x y z w)'),
        ]

        for (typ, st, res) in goodls:
            nod = sparse.parse(st)
            val = node_to_value(typ, nod)
            val = resolve_value(val)
            self.assertEqual(val, res)
            self.assertEqual(type(val), type(res))
            if (type(val) == list):
                for (sub1, sub2) in zip(val, res):
                    self.assertEqual(type(sub1), type(sub2))

        for (typ, st) in badls:
            nod = sparse.parse(st)
            self.assertRaises(ValueError, node_to_value, typ, nod)