예제 #1
0
    def export_last(self,
                    tags,
                    filename=None,
                    resource_type="model",
                    project=None,
                    **kwargs):
        """Retrieves a remote resource by tag when finished and stores it
           in the user-given file

           The resource parameter should be a string containing the
           resource id or the dict returned by the corresponding create method.
           As each resource is an evolving object that is processed
           until it reaches the FINISHED or FAULTY state, the function will
           wait until the resource is in one of these states to store the
           associated info.

        """

        if tags is not None and tags != '':
            query_string = LIST_LAST % tags
            if project is not None:
                query_string += ";project=%s" % project

            kwargs.update({
                'query_string':
                "%s;%s" % (query_string, kwargs.get('query_string', ''))
            })

            response = self._list("%s%s" % (self.url, resource_type), **kwargs)
            if len(response.get("objects", [])) > 0:
                resource_info = response["objects"][0]
                if not is_status_final(resource_info):
                    self.ok(resource_info)
                if filename is None:
                    file_dir = self.storage or DFT_STORAGE
                    now = datetime.datetime.now().strftime("%a%b%d%y_%H%M%S")
                    filename = os.path.join( \
                        file_dir,
                        "%s_%s.json" % (tags.replace("/", "_"), now))
                if resource_type in COMPOSED_RESOURCES:
                    for component_id in resource_info["models"]:
                        self.export( \
                            component_id,
                            filename=os.path.join( \
                                os.path.dirname(filename),
                                component_id.replace("/", "_")))
                return save_json(resource_info, filename)
            else:
                raise ValueError("No %s found with tags %s." %
                                 (resource_type, tags))
        else:
            raise ValueError("First agument is expected to be a non-empty"
                             " tag.")
예제 #2
0
    def export_last(self, tags, filename=None,
                    resource_type="model", project=None,
                    **kwargs):
        """Retrieves a remote resource by tag when finished and stores it
           in the user-given file

           The resource parameter should be a string containing the
           resource id or the dict returned by the corresponding create method.
           As each resource is an evolving object that is processed
           until it reaches the FINISHED or FAULTY state, the function will
           wait until the resource is in one of these states to store the
           associated info.

        """

        if tags is not None and tags != '':
            query_string = LIST_LAST % tags
            if project is not None:
                query_string += ";project=%s" % project

            kwargs.update({'query_string': "%s;%s" % \
                (query_string, kwargs.get('query_string', ''))})

            response = self._list("%s%s" % (self.url, resource_type),
                                  **kwargs)
            if len(response.get("objects", [])) > 0:
                resource_info = response["objects"][0]
                if not is_status_final(resource_info):
                    self.ok(resource_info)
                if filename is None:
                    file_dir = self.storage or DFT_STORAGE
                    now = datetime.datetime.now().strftime("%a%b%d%y_%H%M%S")
                    filename = os.path.join( \
                        file_dir,
                        "%s_%s.json" % (tags.replace("/", "_"), now))
                if resource_type in COMPOSED_RESOURCES:
                    for component_id in resource_info["models"]:
                        self.export( \
                            component_id,
                            filename=os.path.join( \
                                os.path.dirname(filename),
                                component_id.replace("/", "_")))
                return save_json(resource_info, filename)
            else:
                raise ValueError("No %s found with tags %s." % (resource_type,
                                                                tags))
        else:
            raise ValueError("First agument is expected to be a non-empty"
                             " tag.")
예제 #3
0
    def export(self, resource, filename=None, pmml=False, **kwargs):
        """Retrieves a remote resource when finished and stores it
           in the user-given file

           The resource parameter should be a string containing the
           resource id or the dict returned by the corresponding create method.
           As each resource is an evolving object that is processed
           until it reaches the FINISHED or FAULTY state, the function will
           wait until the resource is in one of these states to store the
           associated info.

        """
        resource_type = get_resource_type(resource)
        if resource_type is None:
            raise ValueError("A resource ID or structure is needed.")

        if pmml:
            if resource_type not in c.PMML_MODELS:
                raise ValueError("Failed to export to PMML. Only some models"
                                 " can be exported to PMML.")
        resource_id = get_resource_id(resource)

        if resource_id:
            if pmml:
                # only models with no text fields can be exported
                resource_info = self._get("%s%s" % (self.url, resource_id),
                                          query_string=c.TINY_RESOURCE)
                field_types = resource_info["object"].get( \
                    "dataset_field_types", {})
                if field_types.get("items", 0) > 0 or \
                        field_types.get("text", 0) > 0:
                    raise ValueError("Failed to export to PMML. Models with "
                                     "text and items fields cannot be "
                                     "exported to PMML.")
                if kwargs.get("query_string"):
                    kwargs["query_string"] += ";pmml=yes"
                else:
                    kwargs["query_string"] = "pmml=yes"
            resource_info = self._get("%s%s" % (self.url, resource_id),
                                      **kwargs)
            if not is_status_final(resource_info):
                self.ok(resource_info)
            if filename is None:
                file_dir = self.storage or DFT_STORAGE
                filename = os.path.join( \
                    file_dir, resource_id.replace("/", "_"))
            if resource_type in COMPOSED_RESOURCES:
                for component_id in resource_info["object"]["models"]:
                    self.export( \
                        component_id,
                        filename=os.path.join(os.path.dirname(filename),
                                              component_id.replace("/", "_")),
                        pmml=pmml,
                        **kwargs)
            if pmml and resource_info.get("object", {}).get("pmml"):
                resource_info = resource_info.get("object", {}).get("pmml")
                resource_info = minidom.parseString( \
                    resource_info).toprettyxml()
                return save(resource_info, filename)
            return save_json(resource_info, filename)
        else:
            raise ValueError("First agument is expected to be a valid"
                             " resource ID or structure.")
예제 #4
0
    def export(self, resource, filename=None, pmml=False, **kwargs):
        """Retrieves a remote resource when finished and stores it
           in the user-given file

           The resource parameter should be a string containing the
           resource id or the dict returned by the corresponding create method.
           As each resource is an evolving object that is processed
           until it reaches the FINISHED or FAULTY state, the function will
           wait until the resource is in one of these states to store the
           associated info.

        """
        resource_type = get_resource_type(resource)
        if resource_type is None:
            raise ValueError("A resource ID or structure is needed.")

        if pmml:
            if resource_type not in c.PMML_MODELS:
                raise ValueError("Failed to export to PMML. Only some models"
                                 " can be exported to PMML.")
        resource_id = get_resource_id(resource)

        if resource_id:
            if pmml:
                # only models with no text fields can be exported
                resource_info = self._get("%s%s" % (self.url, resource_id),
                                          query_string=c.TINY_RESOURCE)
                field_types = resource_info["object"].get( \
                    "dataset_field_types", {})
                if field_types.get("items", 0) > 0 or \
                        field_types.get("text", 0) > 0:
                    raise ValueError("Failed to export to PMML. Models with "
                                     "text and items fields cannot be "
                                     "exported to PMML.")
                if kwargs.get("query_string"):
                    kwargs["query_string"] += ";pmml=yes"
                else:
                    kwargs["query_string"] = "pmml=yes"
            resource_info = self._get("%s%s" % (self.url, resource_id),
                                      **kwargs)
            if not is_status_final(resource_info):
                self.ok(resource_info)
            if filename is None:
                file_dir = self.storage or DFT_STORAGE
                filename = os.path.join( \
                    file_dir, resource_id.replace("/", "_"))
            if resource_type in COMPOSED_RESOURCES:
                for component_id in resource_info["object"]["models"]:
                    # for weighted fusions we need to retrieve the component
                    # ID
                    if isinstance(component_id, dict):
                        component_id = component_id['id']
                    self.export( \
                        component_id,
                        filename=os.path.join(os.path.dirname(filename),
                                              component_id.replace("/", "_")),
                        pmml=pmml,
                        **kwargs)
            if pmml and resource_info.get("object", {}).get("pmml"):
                resource_info = resource_info.get("object", {}).get("pmml")
                resource_info = minidom.parseString( \
                    resource_info).toprettyxml()
                return save(resource_info, filename)
            return save_json(resource_info, filename)
        else:
            raise ValueError("First agument is expected to be a valid"
                             " resource ID or structure.")