Exemplo n.º 1
0
    def resume_cube(self, display=False):
        """resume_cube(display=False) -> self : Resume the last cube produced by the user.
        :param display: option for displaying the response in a "pretty way" using the pretty_print function (default is False)
        :type display: bool
        :returns: self or None
        :rtype: Client or None
        :raises: RuntimeError
        """

        if self.username is None or self.password is None or self.server is None or self.port is None:
            raise RuntimeError('one or more login parameters are None')
        query = 'operator=oph_get_config;key=OPH_DATACUBE;'
        self.last_request = query
        try:
            self.last_response, self.last_jobid, newsession, return_value, error = _ophsubmit.submit(
                self.username, self.password, self.server, self.port, query)
            if return_value:
                raise RuntimeError(error)
            response = self.deserialize_response()
            if response is not None:
                for response_i in response['response']:
                    if response_i['objkey'] == 'get_config':
                        self.cube = response_i['objcontent'][0]['rowvalues'][
                            0][1]

                    if display is True:
                        self.pretty_print(response_i, response)

                    break
        except Exception as e:
            print(get_linenumber(),
                  "Something went wrong in resuming last cube:", e)
            return None
        return self
Exemplo n.º 2
0
    def wsubmit(self, workflow, *params):
        """wsubmit(workflow,*params) -> self : Submit an entire workflow passing a JSON string or the path of a JSON file and an optional series of
           parameters that will replace $1, $2 etc. in the workflow. The workflow will be validated against the Ophidia Workflow JSON Schema.
        :param workflow: JSON string or path of a JSON file containing an Ophidia workflow
        :type workflow: str
        :param params: list of positional parameters that will replace $1, $2 etc. in the workflow
        :type params: str
        :returns: self or None
        :rtype: Client or None
        :raises: RuntimeError
        """

        if workflow is None:
            raise RuntimeError('workflow is not present')
        if self.username is None or self.password is None or self.server is None or self.port is None:
            raise RuntimeError('one or more login parameters are None')
        request = None

        if os.path.isfile(workflow):
            try:
                file = open(workflow, 'r')
                buffer = file.read()
                file.close()
                for index, param in enumerate(params, start=1):
                    buffer = buffer.replace('${' + str(index) + '}',
                                            str(param))
                    buffer = re.sub('(\$' + str(index) + ')([^0-9]|$)',
                                    str(param) + '\g<2>', buffer)
                buffer = re.sub('(\$\{?(\d*)\}?)', '', buffer)
                request = json.loads(buffer)

            except Exception as e:
                print(
                    get_linenumber(),
                    "Something went wrong in reading and/or parsing the file:",
                    e)
                return None
        else:
            try:
                buffer = workflow
                for index, param in enumerate(params, start=1):
                    buffer = buffer.replace('${' + str(index) + '}',
                                            str(param))
                    buffer = re.sub('(\$' + str(index) + ')([^0-9]|$)',
                                    str(param) + '\g<2>', buffer)
                buffer = re.sub('(\$\{?(\d*)\}?)', '', buffer)
                request = json.loads(buffer)

            except Exception as e:
                print(get_linenumber(),
                      "Something went wrong in parsing the string:", e)
                return None

        if self.session and 'sessionid' not in request:
            request['sessionid'] = self.session
        if self.cwd and 'cwd' not in request:
            request['cwd'] = self.cwd
        if self.cdd and 'cdd' not in request:
            request['cdd'] = self.cdd
        if self.cube and 'cube' not in request:
            request['cube'] = self.cube
        if self.exec_mode and 'exec_mode' not in request:
            request['exec_mode'] = self.exec_mode
        if self.ncores and 'ncores' not in request:
            request['ncores'] = str(self.ncores)
        self.last_request = json.dumps(request)
        try:
            if not self.wisvalid(self.last_request):
                print("The workflow is not valid")
                return None
            self.last_response, self.last_jobid, newsession, self.last_return_value, self.last_error = _ophsubmit.submit(
                self.username, self.password, self.server, self.port,
                self.last_request)
            if self.last_return_value:
                raise RuntimeError(self.last_error)
            if self.api_mode and not self.last_return_value and self.last_error is not None:
                raise RuntimeError(self.last_error)

            if newsession is not None:
                if len(newsession) == 0:
                    self.session = None
                else:
                    self.session = newsession
                    self.cwd = '/'
            response = self.deserialize_response()
            if response is not None:
                for response_i in response['response']:
                    if response_i['objclass'] == 'text' and response_i[
                            'objcontent'][0]['title'] == 'Output Cube':
                        self.cube = response_i['objcontent'][0]['message']
                        break

                for response_i in response['response']:
                    if response_i['objclass'] == 'text' and response_i[
                            'objcontent'][0][
                                'title'] == 'Current Working Directory':
                        self.cwd = response_i['objcontent'][0]['message']
                        break

                for response_i in response['response']:
                    if response_i['objclass'] == 'text' and response_i[
                            'objcontent'][0][
                                'title'] == 'Current Data Directory':
                        self.cdd = response_i['objcontent'][0]['message']
                        break

                self.pretty_print(response_i, response)

        except Exception as e:
            print(get_linenumber(),
                  "Something went wrong in submitting the request:", e)
            return None
        return self
Exemplo n.º 3
0
    def get_base_path(self, display=False):
        """get_base_path(display=False) -> self : Get base path for data from the Ophidia instance.
        :param display: option for displaying the response in a "pretty way" using the pretty_print function (default is False)
        :type display: bool
        :returns: self or None
        :rtype: Client or None
        :raises: RuntimeError
        """

        if self.username is None or self.password is None or self.server is None or self.port is None:
            raise RuntimeError('one or more login parameters are None')
        query = 'operator=oph_get_config;key=OPH_BASE_SRC_PATH;'
        self.last_request = query
        try:
            self.last_response, self.last_jobid, newsession, self.last_return_value, self.last_error = _ophsubmit.submit(
                self.username, self.password, self.server, self.port, query)
            if self.last_return_value:
                raise RuntimeError(self.last_error)
            if self.api_mode and not self.last_return_value and self.last_error is not None:
                raise RuntimeError(self.last_error)
            response = self.deserialize_response()
            if response is not None:
                for response_i in response['response']:
                    if response_i['objkey'] == 'get_config':
                        self.base_src_path = response_i['objcontent'][0][
                            'rowvalues'][0][1]

                    if self.api_mode and display is True:
                        self.pretty_print(response_i, response)

                    break
        except Exception as e:
            print(get_linenumber(),
                  "Something went wrong in retrieving base data path:", e)
            return None
        return self
Exemplo n.º 4
0
    def submit(self, query, display=False):
        """submit(query,display=False) -> self : Submit a query like 'operator=myoperator;param1=value1;' or 'myoperator param1=value1;' to the Ophidia server
               according to all login parameters of the Client and its state.
        :param query: query like 'operator=myoperator;param1=value1;' or 'myoperator param1=value1;'
        :type query: str
        :param display: option for displaying the response in a "pretty way" using the pretty_print function (default is False)
        :type display: bool
        :returns: self or None
        :rtype: Client or None
        :raises: RuntimeError
        """

        if query is None:
            raise RuntimeError('query is not present')
        if self.username is None or self.password is None or self.server is None or self.port is None:
            raise RuntimeError('one or more login parameters are None')
        # Check if the query contains only the oph operator
        r = query.split()
        if len(r) != 1:
            if not query.endswith(';'):
                query = query.rstrip()
                query += ';'
        else:
            query += ' '
        if self.session and 'sessionid' not in query:
            query += 'sessionid=' + self.session + ';'
        if self.cwd and 'cwd' not in query:
            query += 'cwd=' + self.cwd + ';'
        if self.cdd and 'cdd' not in query:
            query += 'cdd=' + self.cdd + ';'
        if self.cube and 'cube' not in query:
            query += 'cube=' + self.cube + ';'
        if self.exec_mode and 'exec_mode' not in query:
            query += 'exec_mode=' + self.exec_mode + ';'
        if self.ncores and 'ncores' not in query:
            query += 'ncores=' + str(self.ncores) + ';'
        self.last_request = query
        try:
            self.last_response, self.last_jobid, newsession, self.last_return_value, self.last_error = _ophsubmit.submit(
                self.username, self.password, self.server, self.port, query)
            if self.last_return_value:
                raise RuntimeError(self.last_error)
            if self.api_mode and not self.last_return_value and self.last_error is not None:
                raise RuntimeError(self.last_error)
            if newsession is not None:
                if len(newsession) == 0:
                    self.session = None
                else:
                    if self.session != newsession:
                        self.cwd = '/'
                    self.session = newsession
            response = self.deserialize_response()
            if response is not None:
                for response_i in response['response']:
                    if response_i['objclass'] == 'text' and response_i[
                            'objcontent'][0]['title'] == 'Output Cube':
                        self.cube = response_i['objcontent'][0]['message']
                        break

                for response_i in response['response']:
                    if response_i['objclass'] == 'text' and response_i[
                            'objcontent'][0][
                                'title'] == 'Current Working Directory':
                        self.cwd = response_i['objcontent'][0]['message']
                        break

                for response_i in response['response']:
                    if response_i['objclass'] == 'text' and response_i[
                            'objcontent'][0][
                                'title'] == 'Current Data Directory':
                        self.cdd = response_i['objcontent'][0]['message']
                        break

                if self.api_mode and display is True:
                    self.pretty_print(response_i, response)

        except Exception as e:
            print(get_linenumber(),
                  "Something went wrong in submitting the request:", e)
            return None
        return self