def get_axes_labels(self): """ Return axes labels as a list :rtype list[str] """ response = decode_res(self.__describe_coverage()) return response.split("axisLabels=\"")[1].split('"')[0].split(" ")
def execute(self, request, output=False, mock=False, input_base_url=None): """ Executes a WCST request and returns the response to it :param WCSTRequest request: the request to be executed :rtype str """ request = self.prepare_request(request) base_url_tmp = self.base_url if input_base_url is not None: base_url_tmp = input_base_url service_call = base_url_tmp + "?" + request.get_query_string() if output: log.info(service_call) if mock: log.info( make_bold( "This is just a mocked request, no data will be changed.")) log.info(service_call) return try: response = decode_res( validate_and_read_url(base_url_tmp, request.get_query_string())) except Exception as ex: raise WCSTException( 404, "Failed reading response from WCS service. " "Detailed error: {}.".format(str(ex)), service_call) namespaces = "" # check if a WMS error occurred and parse accordingly if response.find("ServiceExceptionReport") != -1: namespaces = None # if WCST error then using ows:ExceptionReport elif response.find("ExceptionReport") != -1: namespaces = {"ows": "http://www.opengis.net/ows/2.0"} # Check error from response self.__check_request_for_errors(response, namespaces, service_call) try: if str(response) != "" and str(response) != "None": return encode_res(response) return "" except Exception as ex: raise WCSTException( 0, "General exception while executing the request: " + str(ex), service_call)
def exists(self): """ Returns true if the coverage exist, false otherwise :rtype bool """ try: # Check if coverage exists via the Non-standard REST endpoint service_call = self.wcs_service + "/objectExists?coverageId=" + self.coverage_id response = decode_res(validate_and_read_url(service_call)) return response == "true" except Exception as ex: # Something is wrong, try with the standard WCS DescribeCoverage request pass try: # Check if coverage exists in WCS DescribeCoverage result service_call = self.wcs_service + "?service=WCS&request=DescribeCoverage&version=" + \ Session.get_WCS_VERSION_SUPPORTED() \ + "&coverageId=" + self.coverage_id response = validate_and_read_url(service_call) if decode_res(response).strip() != "": return True return False except Exception as ex: exception_text = str(ex) if "Missing basic authentication header" in exception_text: raise RuntimeException("Endpoint '{}' requires valid rasdaman credentials with format username:password in a text file. \n" "Hint: Create this identify file first with read permission for user running wcst_import, \n" "then rerun wcst_import.sh ingredients.json -i path_to_the_identity_file.".format(self.wcs_service)) if not "NoSuchCoverage" in exception_text: raise RuntimeException("Could not check if the coverage exists. " "Reason: {}".format(exception_text)) else: # coverage doesn't exist return False
def description(self): """ Gets the description from coverage_id in wcs_url :rtype: String """ xmlstr = validate_and_read_url(self._get_description_url()) # Check if coverage id does not exist in wcs_endpoint by returning an Exception if decode_res(xmlstr).find("ExceptionReport") != -1: raise RuntimeException( "Could not read the coverage description for coverage id: {} with url: {} " .format(self.coverage_id, self.wcs_url)) # If coverage id does exist then return its description return xmlstr
def validate_and_read_url(url, data=None): """ Open url and validate the status code before returning the response in string :param str url: the url to open :param dict data: POST parameters :rtype: str """ url = __encode_quote(url) try: request = Request(url) if data is not None: if sys.version_info[0] < 3: request.add_data(data) else: request.data = bytes(data, encoding = "ISO-8859-1") from config_manager import ConfigManager if ConfigManager.user is not None: base64string = base64.b64encode(ConfigManager.user + ":" + ConfigManager.passwd) request.add_header("Authorization", "Basic %s" % base64string) ret = urlopen(request, context = ssl._create_unverified_context()) except Exception as e: raise RuntimeException("Failed opening connection to '{}'. " "Check that the service is up and running." "Detail error: {}.".format(url, decode_res(e.read()))) response = ret.read() if ret.getcode() != 200: raise RuntimeException("Server responded failed for request '{}'." "Detail error: {}.".format(url, response)) if "<html" in decode_res(response): raise RuntimeException("Server requests credentials for authentication," "please provide them (check wcst_import.sh -h for details)") return response
def _get_data_type(self, slice): """ Returns the data type of the slice by downloading the slice and trying to guess it with GDAL :param Slice slice: slice :rtype: str """ if not ConfigManager.mock and isinstance(slice.data_provider, UrlDataProvider): # Do this only for coverages that have more than one axis if len(slice.axis_subsets) > 1: contents = decode_res( validate_and_read_url(slice.data_provider.get_url())) file_path = TmpFile().write_to_tmp_file(contents, "tif") return GDALGmlUtil(file_path).get_band_gdal_type(), file_path return None
def __run_shell_command(self, command, abort_on_error=False): """ Run a shell command and exit wcst_import if needed :param str command: shell command to run """ try: log.info("Executing shell command '{}'...".format(command)) output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True) output = decode_res(output) if output != "": log.info("Output result '{}'".format(output)) except subprocess.CalledProcessError as exc: log.warn("Failed, status code '{}', error message '{}'.".format(exc.returncode, str(exc.output).strip())) if abort_on_error: log.error("wcst_import terminated on running hook command.") exit(1)
def validate_and_read_url(url, data=None): """ Open url and validate the status code before returning the response in string :param str url: the url to open :param dict data: POST parameters :rtype: str """ url = __encode_quote(url) try: request = Request(url) if data is not None: if sys.version_info[0] < 3: request.add_data(data) else: request.data = bytes(data, encoding="ISO-8859-1") from config_manager import ConfigManager if ConfigManager.user is not None: tmp = (ConfigManager.user + ":" + ConfigManager.passwd).encode("utf-8") base64string = base64.b64encode(tmp).decode("utf-8") request.add_header("Authorization", "Basic %s" % base64string) ret = urlopen(request, context=ssl._create_unverified_context()) except Exception as e: if hasattr(e.reason, "strerror"): # URLError exception_text = e.reason.strerror else: # HTTPError exception_text = decode_res(e.read()) error_message = parse_error_message(exception_text) raise RuntimeException("Failed opening connection to '{}'. \n" "Reason: {}".format(url, error_message)) response = ret.read() if ret.getcode() != 200: raise RuntimeException("Server failed to respond for request '{}'. " "Reason: {}".format(url, response)) return response
def exists(self): """ Returns true if the coverage exist, false otherwise :rtype bool """ try: # Check if coverage exists in WCS DescribeCoverage result service_call = self.wcs_service + "?service=WCS&request=DescribeCoverage&version=" + \ Session.get_WCS_VERSION_SUPPORTED() \ + "&coverageId=" + self.coverage_id response = validate_and_read_url(service_call) if decode_res(response).strip() != "": return True return False except Exception as ex: if not "NoSuchCoverage" in str(ex): raise RuntimeException( "Could not check if the coverage exists. " "Detail error: {}".format(str(ex))) else: # coverage doesn't exist return False