Пример #1
0
    def create_drop_objects(self):
        # create local vars for better closures:
        drop_objects_name = "__drop_%ss" % self._term
        rest_target = '%ss/' % self._term
        module_logger = self._module_logger
        obj_class = self._class
        obj_term = self._term
        http = self._http

        def drop_objects(items):
            if not isinstance(items, list) and not isinstance(items, tuple):
                items = [items]
            victim_ids = {}
            for item in items:
                if isinstance(item, basestring):
                    victim_ids[item] = self.get_entity_id_from_name(item)
                elif isinstance(item, obj_class):
                    victim_ids[item.name] = item._id
                else:
                    raise TypeError("Excepted argument of type {term} or else the {term}'s name".format(term=obj_term))
            for name, id in victim_ids.items():
                module_logger.info("Drop %s %s", obj_term, name)
                http.delete(rest_target + str(id))  # TODO: update w/ URI jazz
        set_entity_collection(drop_objects, entity_type_to_collection_name(self._term))  # so meta knows where it goes
        drop_objects.__name__ = drop_objects_name
        drop_objects.__doc__ = """Deletes the {obj_term} on the server.""".format(obj_term=obj_term)
        # decorate the method with api and arg, which will also give a nice command def for setting the doc_stub
        api_decorator = get_api_decorator(module_logger, parent_class_name="_BaseGlobals")
        arg_decorator = arg(name="items",
                            data_type="[ str | {obj_term} object | list [ str | {obj_term} objects ]]".format(obj_term=obj_term),
                            description="Either the name of the {obj_term} object to delete or the {obj_term} object itself".format(obj_term=obj_term))
        decorated_method = api_decorator(arg_decorator(drop_objects))
        return decorated_method
Пример #2
0
    def create_get_object(self):
        get_object_name = "__get_%s" % self._term
        rest_target = '%ss' % self._term
        rest_target_with_name = '%s?name=' % rest_target
        module_logger = self._module_logger
        http = self._http
        term = self._term
        obj_class = self._class
        get_class = get_entity_class_from_store

        def get_object(identifier):
            module_logger.info("%s(%s)", get_object_name, identifier)
            if isinstance(identifier, basestring):
                if uri_pattern.match(identifier):
                    uri = identifier
                else:
                    uri = rest_target_with_name + identifier
            else:
                uri = '%s/%s' % (rest_target, identifier)
            r = http.get(uri)
            try:
                entity_type = r.json()['entity_type']
            except KeyError:
                return obj_class(_info=r.json())
            else:
                if not entity_type.startswith(term):
                    raise ValueError("Object '%s' is not a %s type" %
                                     (identifier, term))
                cls = get_class(entity_type)
                return cls(_info=r.json())

        set_entity_collection(get_object,
                              entity_type_to_collection_name(
                                  self._term))  # so meta knows where it goes
        get_object.__name__ = get_object_name
        get_object.__doc__ = """Get handle to a {obj_term} object.""".format(
            obj_term=self._term)
        # decorate the method with api and arg, which will also give a nice command def for setting the doc_stub
        api_decorator = get_api_decorator(module_logger,
                                          parent_class_name="_BaseGlobals")
        arg_decorator = arg(name="identifier",
                            data_type="str | int",
                            description="Name of the %s to get" % self._term)
        # Determining the return type is difficult, because we don't know in advance what specific type of entity
        # we will get (like Frame or VertexFrame or ?).  We choose to go with the general entity type, like "Frame",
        # because it is likely the most common case and also has backing for SPA (i.e. _BaseFrame does not get defined
        # in the DocStubs)  Btw, we know "Model" will fail SPA --there is no "general" Model class.
        returns_type = upper_first(self._term)  # so, "frame" -> "Frame"
        returns_decorator = returns(returns_type, "%s object" % self._term)
        decorated_method = api_decorator(
            returns_decorator(arg_decorator(get_object)))
        return decorated_method
Пример #3
0
    def create_drop_objects(self):
        # create local vars for better closures:
        drop_objects_name = "__drop_%ss" % self._term
        module_logger = self._module_logger
        obj_class = self._class
        obj_term = self._term
        http = self._http

        def drop_objects(items):
            num_items_deleted = 0
            if not isinstance(items, list) and not isinstance(items, tuple):
                items = [items]
            victim_uris = set()
            for item in items:
                if isinstance(item, basestring):
                    try:
                        victim_uris.add(self.get_entity_uri_from_name(item))
                    except:
                        pass    #  Don't fail if item with the specified name was not found
                elif isinstance(item, obj_class):
                    try:
                        if item.status == "ACTIVE":
                            victim_uris.add(item.uri)
                    except:
                        pass
                else:
                    raise TypeError("Excepted argument of type {term} or else the {term}'s name".format(term=obj_term))
            for uri in victim_uris:
                module_logger.info("Drop %s %s", obj_term, uri)
                try:
                    http.delete(uri)
                    num_items_deleted += 1
                except RuntimeError as e:
                    module_logger.warn("RuntimeError when attempting to drop item with uri: {uri}. ({message})".format(uri=uri, message=e.message))
                except:
                    module_logger.warn("Error when attempting to drop item with uri: {uri}".format(uri=uri))
            return num_items_deleted
        set_entity_collection(drop_objects, entity_type_to_collection_name(self._term))  # so meta knows where it goes
        drop_objects.__name__ = drop_objects_name
        drop_objects.__doc__ = """Deletes the {obj_term} on the server.""".format(obj_term=obj_term)
        # decorate the method with api and arg, which will also give a nice command def for setting the doc_stub
        api_decorator = get_api_decorator(module_logger, parent_class_name="_BaseGlobals")
        arg_decorator = arg(name="items",
                            data_type="[ str | {obj_term} object | list [ str | {obj_term} objects ]]".format(obj_term=obj_term),
                            description="Either the name of the {obj_term} object to delete or the {obj_term} object itself".format(obj_term=obj_term))
        returns_decorator = returns(list, "Number of {obj_term}s deleted.".format(obj_term=obj_term))
        decorated_method = api_decorator(returns_decorator(arg_decorator(drop_objects)))
        return decorated_method
Пример #4
0
    def create_get_object(self):
        get_object_name = "__get_%s" % self._term
        rest_target = '%ss' % self._term
        rest_target_with_name = '%s?name=' % rest_target
        module_logger = self._module_logger
        http = self._http
        term = self._term
        obj_class = self._class
        get_class = get_entity_class_from_store

        def get_object(identifier):
            module_logger.info("%s(%s)", get_object_name, identifier)
            if isinstance(identifier, basestring):
                if uri_pattern.match(identifier):
                    uri = identifier
                else:
                    uri = rest_target_with_name + identifier
            else:
                uri = '%s/%s' % (rest_target, identifier)
            r = http.get(uri)
            try:
                entity_type = r.json()['entity_type']
            except KeyError:
                return obj_class(_info=r.json())
            else:
                if not entity_type.startswith(term):
                    raise ValueError("Object '%s' is not a %s type" % (identifier, term))
                cls = get_class(entity_type)
                return cls(_info=r.json())
        set_entity_collection(get_object, entity_type_to_collection_name(self._term))  # so meta knows where it goes
        get_object.__name__ = get_object_name
        get_object.__doc__ = """Get handle to a {obj_term} object.""".format(obj_term=self._term)
        # decorate the method with api and arg, which will also give a nice command def for setting the doc_stub
        api_decorator = get_api_decorator(module_logger, parent_class_name="_BaseGlobals")
        arg_decorator = arg(name="identifier",
                            data_type="str | int",
                            description="Name of the %s to get" % self._term)
        # Determining the return type is difficult, because we don't know in advance what specific type of entity
        # we will get (like Frame or VertexFrame or ?).  We choose to go with the general entity type, like "Frame",
        # because it is likely the most common case and also has backing for SPA (i.e. _BaseFrame does not get defined
        # in the DocStubs)  Btw, we know "Model" will fail SPA --there is no "general" Model class.
        returns_type = upper_first(self._term)  # so, "frame" -> "Frame"
        returns_decorator = returns(returns_type, "%s object" % self._term)
        decorated_method = api_decorator(returns_decorator(arg_decorator(get_object)))
        return decorated_method
Пример #5
0
    def create_get_object_names(self):
        get_object_names_name = "__get_%s_names" % self._term
        rest_collection = self._term + 's'
        module_logger = self._module_logger
        http = self._http

        def get_object_names():
            module_logger.info(get_object_names_name)
            r = http.get(rest_collection)
            payload = r.json()
            return [item.get('name', None) for item in payload]
        set_entity_collection(get_object_names, entity_type_to_collection_name(self._term))  # so meta knows where it goes
        get_object_names.__name__ = get_object_names_name
        get_object_names.__doc__ = """Retrieve names for all the {obj_term} objects on the server.""".format(obj_term=self._term)
        # decorate the method with api and arg, which will also give a nice command def for setting the doc_stub
        api_decorator = get_api_decorator(module_logger, parent_class_name="_BaseGlobals")
        returns_decorator = returns(list, "List of names")
        decorated_method = api_decorator(returns_decorator(get_object_names))
        return decorated_method
Пример #6
0
    def create_get_object_names(self):
        get_object_names_name = "__get_%s_names" % self._term
        rest_collection = self._term + 's'
        module_logger = self._module_logger
        http = self._http

        def get_object_names():
            module_logger.info(get_object_names_name)
            r = http.get(rest_collection)
            payload = r.json()
            return [item.get('name', None) for item in payload]
        set_entity_collection(get_object_names, entity_type_to_collection_name(self._term))  # so meta knows where it goes
        get_object_names.__name__ = get_object_names_name
        get_object_names.__doc__ = """Retrieve names for all the {obj_term} objects on the server.""".format(obj_term=self._term)
        # decorate the method with api and arg, which will also give a nice command def for setting the doc_stub
        api_decorator = get_api_decorator(module_logger, parent_class_name="_BaseGlobals")
        returns_decorator = returns(list, "List of names")
        decorated_method = api_decorator(returns_decorator(get_object_names))
        return decorated_method
Пример #7
0
    def create_drop_objects(self):
        # create local vars for better closures:
        drop_objects_name = "__drop_%ss" % self._term
        module_logger = self._module_logger
        obj_class = self._class
        obj_term = self._term
        http = self._http

        def drop_objects(items):
            num_items_deleted = 0
            if not isinstance(items, list) and not isinstance(items, tuple):
                items = [items]
            victim_uris = {}
            for item in items:
                if isinstance(item, basestring):
                    try:
                        victim_uris[item] = self.get_entity_uri_from_name(item)
                    except:
                        pass    #  Don't fail if item with the specified name was not found
                elif isinstance(item, obj_class):
                    victim_uris[item.name] = item.uri
                else:
                    raise TypeError("Excepted argument of type {term} or else the {term}'s name".format(term=obj_term))
            for name, uri in victim_uris.items():
                module_logger.info("Drop %s %s", obj_term, name)
                http.delete(uri)
                num_items_deleted += 1
            return num_items_deleted
        set_entity_collection(drop_objects, entity_type_to_collection_name(self._term))  # so meta knows where it goes
        drop_objects.__name__ = drop_objects_name
        drop_objects.__doc__ = """Deletes the {obj_term} on the server.""".format(obj_term=obj_term)
        # decorate the method with api and arg, which will also give a nice command def for setting the doc_stub
        api_decorator = get_api_decorator(module_logger, parent_class_name="_BaseGlobals")
        arg_decorator = arg(name="items",
                            data_type="[ str | {obj_term} object | list [ str | {obj_term} objects ]]".format(obj_term=obj_term),
                            description="Either the name of the {obj_term} object to delete or the {obj_term} object itself".format(obj_term=obj_term))
        returns_decorator = returns(list, "Number of {obj_term}s deleted.".format(obj_term=obj_term))
        decorated_method = api_decorator(returns_decorator(arg_decorator(drop_objects)))
        return decorated_method