Esempio n. 1
0
    def test_complex_prop(self):
        class Foo(jw.JsonWrappedObj):
            writeable = True
            pass

        class Wrapper(object):
            @classmethod
            def map(c, x):
                return "in_" + x

            @classmethod
            def unmap(c, x):
                return x[len("in_"):]

        jw.props(Foo, a={"ro": True}, b={"map": Wrapper}, d={"mapto": "c"})

        f = Foo({"a": "aaa", "c": "hello"})

        def seta():
            f.a = "changed"

        self.assertRaises(AttributeError, seta)
        self.assertEquals(f.d, "hello")
        f.d = "there"
        self.assertEquals(f.d, "there")
        self.assertEquals(f.props["c"], "there")

        f = Foo.map({"b": "hello"})
        self.assertEquals(f.b, "in_hello")
        f.b = "in_goodbye"
        p = f.to_json_obj()

        self.assertEquals(p["b"], "goodbye")
Esempio n. 2
0
    def test_complex_prop(self):
        class Foo(jw.JsonWrappedObj):
            writeable = True
            pass

        class Wrapper(object):
            @classmethod
            def map(c,x):
                return "in_" + x

            @classmethod
            def unmap(c,x):
                return x[len("in_"):]

        jw.props(Foo, 
                 a={
                 "ro": True
                },
                 b={ "map" : Wrapper },
                 d={
                "mapto" : "c"
                })
        
        f = Foo({"a":"aaa","c":"hello"})
        def seta():
            f.a = "changed"
        self.assertRaises(AttributeError, seta)
        self.assertEquals(f.d, "hello")
        f.d = "there"
        self.assertEquals(f.d, "there")
        self.assertEquals(f.props["c"], "there")

        f = Foo.map({"b":"hello"})
        self.assertEquals(f.b, "in_hello")
        f.b="in_goodbye"
        p = f.to_json_obj()

        self.assertEquals(p["b"], "goodbye")
Esempio n. 3
0
    def test_simple_prop(self):
        class Foo(jw.JsonWrappedObj):
            pass

        jw.props(Foo, "hey")
        f = Foo({"hey":"you"})

        self.assertEquals(f.hey, "you")

        def failer():
            f.hey = "here"
        
        self.assertRaises(AttributeError, failer)

        class Bar(jw.JsonWrappedObj):
            writeable = True
            pass

        jw.props(Bar, "hey")

        b = Bar({"hey":"you"})
        self.assertEquals(b.hey, "you")
        b.hey = "hay!"
        self.assertEquals(b.hey, "hay!")
Esempio n. 4
0
    def test_simple_prop(self):
        class Foo(jw.JsonWrappedObj):
            pass

        jw.props(Foo, "hey")
        f = Foo({"hey": "you"})

        self.assertEquals(f.hey, "you")

        def failer():
            f.hey = "here"

        self.assertRaises(AttributeError, failer)

        class Bar(jw.JsonWrappedObj):
            writeable = True
            pass

        jw.props(Bar, "hey")

        b = Bar({"hey": "you"})
        self.assertEquals(b.hey, "you")
        b.hey = "hay!"
        self.assertEquals(b.hey, "hay!")
Esempio n. 5
0
            4: ("multipoint", point, True),
            5: ("multilinestring", linestring, True),
            6: ("multipolygon", polygon, True),
            7: ("geometrycollection", geometry_collection, True)
        }[rawkind]

        if not multi:
            return parse_f(kind)

        multi_count = chomp("I")[0]
        parse_f = geometry_collection
        return factory.multi(kind, [parse_f(kind) for r in range(multi_count)])

    return geometry_collection()


class Feature(jsonwrap.JsonWrappedObj):
    def raw_parse(self, fact):
        return parse_geometry(self.geometry, fact)

    def _attributes(self):
        if not hasattr(self, "_attrs"):
            self._attrs = dict(
                (p, v) for (p, v) in self.props.iteritems() if p != 'geometry')
        return self._attrs

    attributes = property(_attributes)


jsonwrap.props(Feature, "geometry")
Esempio n. 6
0
            1 : ("point",point,False),
            2 : ("linestring",linestring,False),
            3 : ("polygon",polygon,False),
            4 : ("multipoint",point,True),
            5 : ("multilinestring",linestring,True),
            6 : ("multipolygon",polygon,True),
            7 : ("geometrycollection", geometry_collection, True)
            }[rawkind]

        if not multi:
            return parse_f(kind)

        multi_count = chomp("I")[0]
        parse_f = geometry_collection
        return factory.multi(kind, [parse_f(kind) for r in range(multi_count)])

    return geometry_collection()

class Feature(jsonwrap.JsonWrappedObj):
    def raw_parse(self, fact):
        return parse_geometry(self.geometry, fact)

    def _attributes(self):
        if not hasattr(self, "_attrs"):
            self._attrs = dict( (p,v) for (p,v) in self.props.iteritems() if p != 'geometry')
        return self._attrs
    attributes = property(_attributes)
    
jsonwrap.props(Feature,
               "geometry")