Exemple #1
0
    def factory(cls, docstring, name, schema, requests_kwargs, dl, client):
        class_name = utils.to_camel_case(name)

        resource = type(str(class_name), (cls, ), {})
        #resource.__doc__ = docstring
        resource._schema = schema
        resource.client = client
        resource._instance_links = {}
        resource._attributes = {}
        routes = {}

        for link_desc in schema[LINKS]:
            if link_desc[HREF] in routes:
                route = routes[link_desc[HREF]]
            else:
                route = Route(link_desc[HREF])
                routes[link_desc[HREF]] = route

            link = Link(route, method=link_desc[METHOD], schema=link_desc.get(SCHEMA, {}),
                        target_schema=link_desc.get(TARGET_SCHEMA, {}), requests_kwargs=requests_kwargs,
                        docstring=link_desc.get(DOC, None))

            rel = utils.to_snake_case(link_desc[REL])
            if route.is_instance:
                resource._instance_links[rel] = link
            else:
                setattr(resource, rel, LinkProxy(link))

        for name, prop in schema[PROPERTIES].items():
            attr = Attribute(prop)
            resource._attributes[name] = attr
            property_name = name
            if name.startswith("$"):
                property_name = name.replace("$", "")
            if attr.read_only:
                setattr(resource, property_name, property(fget=partial(resource._get_property, name), doc=attr.__doc__))
            else:
                setattr(resource, property_name, property(fget=partial(resource._get_property, name),
                                                          fset=partial(resource._set_property, name),
                                                          fdel=partial(resource._del_property, name),
                                                          doc=attr.__doc__))
        if class_name == "Sample":
            resource.download_file = dl

        return resource
 def test_snake_case(self):
     self.assertEqual("snake_case", utils.to_snake_case("SnakeCase"))
     self.assertEqual("d_n_a", utils.to_snake_case("DNA"))
     self.assertEqual("dna", utils.to_snake_case("Dna"))
Exemple #3
0
 def _parse_schema(self):
     if PROPERTIES in self._link.schema:
         properties = self._link.schema[PROPERTIES]
         for prop in properties.keys():
             self._attributes[prop] = Attribute(properties[prop])
             setattr(self, utils.to_snake_case(prop), self._proxy(prop))