From e8dd55fb1d349dde0e2c7881fa9103681708201c Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Fri, 24 Feb 2023 11:50:17 -0500 Subject: [PATCH] move owslib.wps print statements to logging (#864) * move owslib.wps print statements to logging * fix --- examples/wps-birdhouse.py | 8 +-- examples/wps-ceda-script.py | 3 +- examples/wps-client.py | 14 ++-- examples/wps-pml-script-1.py | 3 +- examples/wps-pml-script-2.py | 3 +- examples/wps-usgs-script.py | 4 +- owslib/coverage/wcs100.py | 8 +-- owslib/util.py | 11 +-- owslib/wps.py | 69 ++++++++----------- .../doctests_sphinx/wps_example_usgs.txt | 2 +- tests/test_wps_execute_language.py | 6 +- tests/test_wps_request7.py | 1 - 12 files changed, 48 insertions(+), 84 deletions(-) diff --git a/examples/wps-birdhouse.py b/examples/wps-birdhouse.py index d4a025a6a..e6b2296d6 100644 --- a/examples/wps-birdhouse.py +++ b/examples/wps-birdhouse.py @@ -4,13 +4,11 @@ from owslib.wps import WebProcessingService, ComplexDataInput, monitorExecution -verbose = False - def multiple_outputs(): print("\nmultiple outputs ...") # get multiple outputs - wps = WebProcessingService('http://localhost:8094/wps', verbose=verbose) + wps = WebProcessingService('http://localhost:8094/wps') processid = 'dummyprocess' inputs = [("input1", '1'), ("input2", '2')] @@ -41,7 +39,7 @@ def complex_input_with_reference(): print("\ncomplex_input_with_reference ...") - wps = WebProcessingService('http://localhost:8094/wps', verbose=verbose) + wps = WebProcessingService('http://localhost:8094/wps') processid = 'wordcount' textdoc = ComplexDataInput("http://www.gutenberg.org/files/28885/28885-h/28885-h.htm") # alice in wonderland @@ -67,7 +65,7 @@ def complex_input_with_content(): print("\ncomplex_input_with_content ...") - wps = WebProcessingService('http://localhost:8094/wps', verbose=verbose) + wps = WebProcessingService('http://localhost:8094/wps') processid = 'wordcount' textdoc = ComplexDataInput("ALICE was beginning to get very tired ...") # alice in wonderland diff --git a/examples/wps-ceda-script.py b/examples/wps-ceda-script.py index 84c218214..7d2e129db 100644 --- a/examples/wps-ceda-script.py +++ b/examples/wps-ceda-script.py @@ -3,8 +3,7 @@ from owslib.wps import WebProcessingService, WPSExecution, WFSFeatureCollection, WFSQuery, GMLMultiPolygonFeatureCollection, monitorExecution, ComplexData, printInputOutput from owslib.util import dump -verbose = True -wps = WebProcessingService('http://ceda-wps2.badc.rl.ac.uk/wps', verbose=verbose) +wps = WebProcessingService('http://ceda-wps2.badc.rl.ac.uk/wps') # 1) GetCapabilities # GET request: http://ceda-wps2.badc.rl.ac.uk/wps?Service=WPS&Request=GetCapabilities&Format=text/xml diff --git a/examples/wps-client.py b/examples/wps-client.py index 60996a42e..740a55977 100755 --- a/examples/wps-client.py +++ b/examples/wps-client.py @@ -21,7 +21,6 @@ def usage(): -u, --url=[URL] the base URL of the WPS - required -r, --request=[REQUEST] the request type (GetCapabilities, DescribeProcess, Execute) - required - -v, --verbose set flag for verbose output - optional (defaults to False) Request Specific Parameters --------------------------- @@ -34,19 +33,19 @@ def usage(): Examples -------- python wps-client.py -u http://cida.usgs.gov/climate/gdp/process/WebProcessingService -r GetCapabilities -python wps-client.py --verbose --url=http://cida.usgs.gov/climate/gdp/process/WebProcessingService --request=GetCapabilities +python wps-client.py --url=http://cida.usgs.gov/climate/gdp/process/WebProcessingService --request=GetCapabilities python wps-client.py -u http://ceda-wps2.badc.rl.ac.uk/wps -r GetCapabilities python wps-client.py -u http://rsg.pml.ac.uk/wps/generic.cgi -r GetCapabilities python wps-client.py -u http://rsg.pml.ac.uk/wps/vector.cgi -r GetCapabilities python wps-client.py -u http://cida.usgs.gov/climate/gdp/process/WebProcessingService -r DescribeProcess -i gov.usgs.cida.gdp.wps.algorithm.FeatureWeightedGridStatisticsAlgorithm -python wps-client.py --verbose --url http://cida.usgs.gov/climate/gdp/process/WebProcessingService --request DescribeProcess --identifier gov.usgs.cida.gdp.wps.algorithm.FeatureWeightedGridStatisticsAlgorithm +python wps-client.py --url http://cida.usgs.gov/climate/gdp/process/WebProcessingService --request DescribeProcess --identifier gov.usgs.cida.gdp.wps.algorithm.FeatureWeightedGridStatisticsAlgorithm python wps-client.py -u http://ceda-wps2.badc.rl.ac.uk/wps -r DescribeProcess -i DoubleIt python wps-client.py -u http://rsg.pml.ac.uk/wps/generic.cgi -r DescribeProcess -i reprojectCoords python wps-client.py -u http://rsg.pml.ac.uk/wps/vector.cgi -r DescribeProcess -i v.mkgrid python wps-client.py -u http://cida.usgs.gov/climate/gdp/process/WebProcessingService -r Execute -x ../tests/wps_USGSExecuteRequest1.xml -python wps-client.py --verbose --url http://cida.usgs.gov/climate/gdp/process/WebProcessingService --request Execute --xml ../tests/wps_USGSExecuteRequest1.xml +python wps-client.py --url http://cida.usgs.gov/climate/gdp/process/WebProcessingService --request Execute --xml ../tests/wps_USGSExecuteRequest1.xml python wps-client.py -u http://rsg.pml.ac.uk/wps/generic.cgi -r Execute -x ../tests/wps_PMLExecuteRequest4.xml python wps-client.py -u http://rsg.pml.ac.uk/wps/generic.cgi -r Execute -x ../tests/wps_PMLExecuteRequest5.xml python wps-client.py -u http://rsg.pml.ac.uk/wps/vector.cgi -r Execute -x ../tests/wps_PMLExecuteRequest6.xml @@ -61,7 +60,7 @@ def usage(): print('ARGV :', sys.argv[1:]) try: - options, remainder = getopt.getopt(sys.argv[1:], 'u:r:x:i:v', ['url=', 'request=', 'xml=', 'identifier=', 'verbose']) + options, remainder = getopt.getopt(sys.argv[1:], 'u:r:x:i:v', ['url=', 'request=', 'xml=', 'identifier=']) except getopt.GetoptError as err: print(str(err)) usage() @@ -73,7 +72,6 @@ def usage(): request = None identifier = None xml = None -verbose = False for opt, arg in options: if opt in ('-u', '--url'): @@ -84,8 +82,6 @@ def usage(): xml = open(arg, 'rb').read() elif opt in ('-i', '--identifier'): identifier = arg - elif opt in ('-v', '--verbose'): - verbose = True else: assert False, 'Unhandled option' @@ -95,7 +91,7 @@ def usage(): sys.exit(3) # instantiate client -wps = WebProcessingService(url, verbose=verbose, skip_caps=True) +wps = WebProcessingService(url, skip_caps=True) if request == 'GetCapabilities': wps.getcapabilities() diff --git a/examples/wps-pml-script-1.py b/examples/wps-pml-script-1.py index 5961e8afb..4d31adea9 100644 --- a/examples/wps-pml-script-1.py +++ b/examples/wps-pml-script-1.py @@ -3,8 +3,7 @@ from owslib.wps import WebProcessingService, monitorExecution # instantiate WPS client -verbose = False -wps = WebProcessingService('http://rsg.pml.ac.uk/wps/generic.cgi', verbose=verbose, skip_caps=True) +wps = WebProcessingService('http://rsg.pml.ac.uk/wps/generic.cgi', skip_caps=True) # 1) GetCapabilities wps.getcapabilities() diff --git a/examples/wps-pml-script-2.py b/examples/wps-pml-script-2.py index 3d2804bd9..02580a1fd 100644 --- a/examples/wps-pml-script-2.py +++ b/examples/wps-pml-script-2.py @@ -3,8 +3,7 @@ from owslib.wps import WebProcessingService, monitorExecution # instantiate WPS client -verbose = False -wps = WebProcessingService('http://rsg.pml.ac.uk/wps/vector.cgi', verbose=verbose, skip_caps=True) +wps = WebProcessingService('http://rsg.pml.ac.uk/wps/vector.cgi', skip_caps=True) # 1) GetCapabilities wps.getcapabilities() diff --git a/examples/wps-usgs-script.py b/examples/wps-usgs-script.py index 5b5035a5a..30fb79efd 100644 --- a/examples/wps-usgs-script.py +++ b/examples/wps-usgs-script.py @@ -4,9 +4,7 @@ from owslib.util import dump # instantiate WPS client -# setting verbose=True will print out all HTTP request and responses to standard output -verbose = False -wps = WebProcessingService('http://cida.usgs.gov/climate/gdp/process/WebProcessingService', verbose=verbose, skip_caps=True) +wps = WebProcessingService('http://cida.usgs.gov/climate/gdp/process/WebProcessingService', skip_caps=True) # 1) GetCapabilities # Submits an HTTP GET "GetCapabilities" request to the WPS service and parses the HTTP response. diff --git a/owslib/coverage/wcs100.py b/owslib/coverage/wcs100.py index 64c6d8a4e..cfdc66276 100644 --- a/owslib/coverage/wcs100.py +++ b/owslib/coverage/wcs100.py @@ -17,7 +17,6 @@ import os import errno -import logging from owslib.util import log, makeString @@ -108,10 +107,9 @@ def getCoverage(self, identifier=None, bbox=None, time=None, format=None, crs=No http://myhost/mywcs?SERVICE=WCS&REQUEST=GetCoverage&IDENTIFIER=TuMYrRQ4&VERSION=1.1.0&BOUNDINGBOX=-180,-90,180,90&TIME=2792-06-01T00:00:00.0&FORMAT=cf-netcdf """ - if log.isEnabledFor(logging.DEBUG): - msg = 'WCS 1.0.0 DEBUG: Parameters passed to GetCoverage: identifier={}, bbox={}, time={}, format={}, crs={}, width={}, height={}, resx={}, resy={}, resz={}, parameter={}, method={}, other_arguments={}' # noqa - log.debug(msg.format( - identifier, bbox, time, format, crs, width, height, resx, resy, resz, parameter, method, str(kwargs))) + msg = 'WCS 1.0.0 DEBUG: Parameters passed to GetCoverage: identifier={}, bbox={}, time={}, format={}, crs={}, width={}, height={}, resx={}, resy={}, resz={}, parameter={}, method={}, other_arguments={}' # noqa + log.debug(msg.format( + identifier, bbox, time, format, crs, width, height, resx, resy, resz, parameter, method, str(kwargs))) try: base_url = next((m.get('url') for m in self.getOperationByName('GetCoverage').methods diff --git a/owslib/util.py b/owslib/util.py index 68551f3e3..74c257771 100644 --- a/owslib/util.py +++ b/owslib/util.py @@ -7,6 +7,7 @@ # Contact email: tomkralidis@gmail.com # ============================================================================= +import logging as log import os import sys from collections import OrderedDict @@ -618,7 +619,7 @@ def build_get_url(base_url, params, overwrite=False): def dump(obj, prefix=''): '''Utility function to print to standard output a generic object with all its attributes.''' - print(("{} {}.{} : {}".format(prefix, obj.__module__, obj.__class__.__name__, obj.__dict__))) + return "{} {}.{} : {}".format(prefix, obj.__module__, obj.__class__.__name__, obj.__dict__) def getTypedValue(data_type, value): @@ -758,14 +759,6 @@ def bind_url(url): return '%s%s' % (url, binder) -import logging -# Null logging handler -NullHandler = logging.NullHandler - -log = logging.getLogger('owslib') -log.addHandler(NullHandler()) - - def findall(root, xpath, attribute_name=None, attribute_value=None): """Find elements recursively from given root element based on xpath and possibly given attribute diff --git a/owslib/wps.py b/owslib/wps.py index bf8671116..06719d2ee 100644 --- a/owslib/wps.py +++ b/owslib/wps.py @@ -106,6 +106,8 @@ * The files PMLExecuteRequest*.xml contain requests that can be submitted to the live PML WPS service. """ +import logging + from owslib.etree import etree from owslib.ows import DEFAULT_OWS_NAMESPACE, XLINK_NAMESPACE from owslib.ows import ServiceIdentification, ServiceProvider, OperationsMetadata, BoundingBox @@ -139,6 +141,8 @@ SYNC = 'sync' ASYNC = 'async' +LOGGER = logging.getLogger(__name__) + def get_namespaces(): ns = n.get_namespaces(["ogc", "wfs", "wps", "gml", "xsi", "xlink"]) @@ -223,7 +227,7 @@ class WebProcessingService(object): Implements IWebProcessingService. """ - def __init__(self, url, version=WPS_DEFAULT_VERSION, username=None, password=None, verbose=False, skip_caps=False, + def __init__(self, url, version=WPS_DEFAULT_VERSION, username=None, password=None, skip_caps=False, headers=None, verify=None, cert=None, timeout=None, auth=None, language=None): """ Initialization method resets the object status. @@ -238,7 +242,6 @@ def __init__(self, url, version=WPS_DEFAULT_VERSION, username=None, password=Non # fields passed in from object initializer self.url = clean_ows_url(url) self.version = version - self.verbose = verbose self.headers = headers self.timeout = timeout self.language = language @@ -263,7 +266,6 @@ def getcapabilities(self, xml=None): # read capabilities document reader = WPSCapabilitiesReader( version=self.version, - verbose=self.verbose, auth=self.auth, language=self.language, timeout=self.timeout, @@ -291,7 +293,6 @@ def describeprocess(self, identifier, xml=None): # read capabilities document reader = WPSDescribeProcessReader( version=self.version, - verbose=self.verbose, auth=self.auth, language=self.language, timeout=self.timeout, @@ -342,7 +343,6 @@ def execute(self, identifier, inputs, output=None, mode=ASYNC, lineage=False, re execution = WPSExecution( version=self.version, url=self.url, - verbose=self.verbose, headers=self.headers, timeout=self.timeout, auth=self.auth, @@ -382,7 +382,7 @@ def _parseProcessMetadata(self, rootElement): processDescriptionElements = rootElement.findall('ProcessDescription') processes = [] for processDescriptionElement in processDescriptionElements: - process = Process(processDescriptionElement, verbose=self.verbose) + process = Process(processDescriptionElement) # override existing processes in object metadata, if existing already found = False @@ -421,14 +421,12 @@ def _parseCapabilitiesMetadata(self, root): if element.tag.endswith('ServiceIdentification'): self.identification = ServiceIdentification( element, namespace=ns) - if self.verbose is True: - dump(self.identification) + LOGGER.debug(self.identification) # metadata elif element.tag.endswith('ServiceProvider'): self.provider = ServiceProvider(element, namespace=ns) - if self.verbose is True: - dump(self.provider) + LOGGER.debug(self.provider) # # @@ -444,8 +442,7 @@ def _parseCapabilitiesMetadata(self, root): for child in element.findall(nspath('Operation', ns=ns)): self.operations.append( OperationsMetadata(child, namespace=ns)) - if self.verbose is True: - dump(self.operations[-1]) + LOGGER.debug(self.operations[-1]) # # @@ -456,10 +453,9 @@ def _parseCapabilitiesMetadata(self, root): # elif element.tag.endswith('ProcessOfferings'): for child in element.findall(nspath('Process', ns=ns)): - p = Process(child, verbose=self.verbose) + p = Process(child) self.processes.append(p) - if self.verbose is True: - dump(self.processes[-1]) + LOGGER.debug(self.processes[-1]) # # @@ -473,8 +469,7 @@ def _parseCapabilitiesMetadata(self, root): # elif element.tag.endswith('Languages'): self.languages = Languages(element) - if self.verbose: - dump(self.languages) + LOGGER.debug(self.languages) class WPSReader(object): @@ -482,9 +477,8 @@ class WPSReader(object): Superclass for reading a WPS document into a lxml.etree infoset. """ - def __init__(self, version=WPS_DEFAULT_VERSION, verbose=False, timeout=30, auth=None, language=None): + def __init__(self, version=WPS_DEFAULT_VERSION, timeout=30, auth=None, language=None): self.version = version - self.verbose = verbose self.timeout = timeout self.auth = auth or Authentication() self.language = language @@ -537,10 +531,10 @@ class WPSCapabilitiesReader(WPSReader): Utility class that reads and parses a WPS GetCapabilities document into a lxml.etree infoset. """ - def __init__(self, version=WPS_DEFAULT_VERSION, verbose=False, timeout=None, auth=None, language=None): + def __init__(self, version=WPS_DEFAULT_VERSION, timeout=None, auth=None, language=None): # superclass initializer super(WPSCapabilitiesReader, self).__init__( - version=version, verbose=verbose, timeout=timeout, auth=auth, language=language) + version=version, timeout=timeout, auth=auth, language=language) def readFromUrl(self, url, username=None, password=None, headers=None, verify=None, cert=None): @@ -563,10 +557,10 @@ class WPSDescribeProcessReader(WPSReader): Class that reads and parses a WPS DescribeProcess document into a etree infoset """ - def __init__(self, version=WPS_DEFAULT_VERSION, verbose=False, timeout=None, auth=None, language=None): + def __init__(self, version=WPS_DEFAULT_VERSION, timeout=None, auth=None, language=None): # superclass initializer super(WPSDescribeProcessReader, self).__init__( - version=version, verbose=verbose, timeout=timeout, auth=auth, language=language) + version=version, timeout=timeout, auth=auth, language=language) def readFromUrl(self, url, identifier, username=None, password=None, headers=None, verify=None, cert=None): @@ -590,9 +584,9 @@ class WPSExecuteReader(WPSReader): Class that reads and parses a WPS Execute response document into a etree infoset """ - def __init__(self, verbose=False, timeout=None, auth=None, language=None): + def __init__(self, timeout=None, auth=None, language=None): # superclass initializer - super(WPSExecuteReader, self).__init__(verbose=verbose, timeout=timeout, auth=auth, language=language) + super(WPSExecuteReader, self).__init__(timeout=timeout, auth=auth, language=language) def readFromUrl(self, url, data={}, method='Get', username=None, password=None, headers=None, verify=None, cert=None): @@ -611,13 +605,12 @@ class WPSExecution(object): Class that represents a single WPS process executed on a remote WPS service. """ - def __init__(self, version=WPS_DEFAULT_VERSION, url=None, username=None, password=None, verbose=False, + def __init__(self, version=WPS_DEFAULT_VERSION, url=None, username=None, password=None, headers=None, verify=None, cert=None, timeout=None, auth=None, language=None): # initialize fields self.url = url self.version = version - self.verbose = verbose self.headers = headers self.auth = auth or Authentication() _fix_auth(self.auth, username, password, verify, cert) @@ -800,7 +793,6 @@ def checkStatus(self, url=None, response=None, sleepSecs=60): """ reader = WPSExecuteReader( - verbose=self.verbose, auth=self.auth, language=self.language, timeout=self.timeout @@ -925,7 +917,6 @@ def submitRequest(self, request): self.request = request reader = WPSExecuteReader( - verbose=self.verbose, auth=self.auth, language=self.language, timeout=self.timeout, @@ -1028,7 +1019,7 @@ def _parseExecuteResponse(self, root): self._parseExceptionReport(element) self.process = Process( - root.find(nspath('Process', ns=wpsns)), verbose=self.verbose) + root.find(nspath('Process', ns=wpsns))) # # xmlns:ns="http://www.opengis.net/wps/1.0.0" @@ -1048,8 +1038,7 @@ def _parseExecuteResponse(self, root): self.processOutputs[:] = [] for outputElement in root.findall(nspath('ProcessOutputs/Output', ns=wpsns)): self.processOutputs.append(Output(outputElement)) - if self.verbose is True: - dump(self.processOutputs[-1]) + LOGGER.debug(self.processOutputs[-1]) class ComplexData(object): @@ -1543,7 +1532,7 @@ class Process(object): """ Class that represents a WPS process. """ - def __init__(self, elem, verbose=False): + def __init__(self, elem): """ Initialization method extracts all available metadata from an XML document (passed in as etree object) """ # self._root = elem - self.verbose = verbose # when process is instantiated from GetCapabilities, elem is 'wps:Process' => wpsns='wps' # when process is instantiated from DescribeProcess, elem is 'ProcessDescription' => wpsns='' @@ -1617,22 +1605,19 @@ def get_bool_attribute(elem, attribute): elif child.tag.endswith('Metadata'): self.metadata.append(Metadata(child)) - if self.verbose is True: - dump(self) + LOGGER.debug(self) # self.dataInputs = [] for inputElement in elem.findall('DataInputs/Input'): self.dataInputs.append(Input(inputElement)) - if self.verbose is True: - dump(self.dataInputs[-1], prefix='\tInput: ') + LOGGER.debug(self.dataInputs[-1], prefix='\tInput: ') # self.processOutputs = [] for outputElement in elem.findall('ProcessOutputs/Output'): self.processOutputs.append(Output(outputElement)) - if self.verbose is True: - dump(self.processOutputs[-1], prefix='\tOutput: ') + LOGGER.debug(self.processOutputs[-1], prefix='\tOutput: ') def __str__(self): return "WPS Process: {}, title={}".format(self.identifier or '', self.title or '') diff --git a/tests/_broken/doctests_sphinx/wps_example_usgs.txt b/tests/_broken/doctests_sphinx/wps_example_usgs.txt index 9e9572637..9ca6b5f8f 100644 --- a/tests/_broken/doctests_sphinx/wps_example_usgs.txt +++ b/tests/_broken/doctests_sphinx/wps_example_usgs.txt @@ -1,7 +1,7 @@ Inspect a remote WPS and retrieve the supported processes: >>> from owslib.wps import WebProcessingService - >>> wps = WebProcessingService('http://cida.usgs.gov/climate/gdp/process/WebProcessingService', verbose=False, skip_caps=True) + >>> wps = WebProcessingService('http://cida.usgs.gov/climate/gdp/process/WebProcessingService', skip_caps=True) >>> wps.getcapabilities() >>> wps.identification.type 'WPS' diff --git a/tests/test_wps_execute_language.py b/tests/test_wps_execute_language.py index bc17095c9..092401f8b 100644 --- a/tests/test_wps_execute_language.py +++ b/tests/test_wps_execute_language.py @@ -1,5 +1,5 @@ -from owslib.wps import WebProcessingService import owslib.wps +from owslib.util import log def test_wps_execute_language(monkeypatch): @@ -8,10 +8,10 @@ def raise_on_log_error(*a): """Make sure the errors are raised, not only caught and logged""" raise AssertionError - monkeypatch.setattr(owslib.wps.log, "error", raise_on_log_error) + monkeypatch.setattr(log, "error", raise_on_log_error) monkeypatch.setattr(owslib.wps.WPSExecution, "parseResponse", lambda *a: None) - wps = WebProcessingService('http://www.example.com', language='fr-CA', skip_caps=True) + wps = owslib.wps.WebProcessingService('http://www.example.com', language='fr-CA', skip_caps=True) execution = wps.execute('test', [], response=b'') assert b'language="fr-CA"' in execution.request diff --git a/tests/test_wps_request7.py b/tests/test_wps_request7.py index 3851cc145..c1388074d 100644 --- a/tests/test_wps_request7.py +++ b/tests/test_wps_request7.py @@ -22,5 +22,4 @@ def test_wps_request7(): # Compare to cached XML request _request = open(resource_file('wps_EmuExecuteRequest7.xml'), 'rb').read() - print(request) assert compare_xml(request, _request) is True