Skip to content

Commit 9350261

Browse files
committed
Introduce generic CapabilitiesReader.fetch_cached and remove redundant code
1 parent 5ef5354 commit 9350261

File tree

1 file changed

+66
-130
lines changed

1 file changed

+66
-130
lines changed

src/config_generator/capabilities_reader.py

Lines changed: 66 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,47 @@ def __init__(self, generator_config, logger, use_cached_project_metadata, cache_
4646
"project_settings_read_timeout", 60
4747
)
4848

49-
# WMS GetProjectSettings
49+
50+
def fetch_cached(self, request_url, params, cache_name, description, silent=False):
51+
document = None
52+
cache_file = os.path.join(self.cache_dir, request_url.replace('/', '_').replace(':', '_') + cache_name)
53+
if self.use_cached_project_metadata:
54+
try:
55+
with open(cache_file) as fh:
56+
document = fh.read()
57+
if not silent:
58+
self.logger.info("Using cached %s for %s" % (description, request_url))
59+
except:
60+
pass
61+
62+
if not document:
63+
response = requests.get(
64+
request_url,
65+
params=params,
66+
timeout=self.project_settings_read_timeout
67+
)
68+
69+
if response.status_code != requests.codes.ok:
70+
self.logger.error(
71+
"Could not get %s from %s:\n%s" %
72+
(description, request_url, response.content)
73+
)
74+
return None
75+
76+
if not silent:
77+
self.logger.info(
78+
"Downloaded %s from %s" % (description, request_url)
79+
)
80+
81+
document = response.content
82+
try:
83+
os.makedirs(os.path.dirname(cache_file), exist_ok=True)
84+
with open(cache_file, "w") as fh:
85+
fh.write(document.decode('utf-8'))
86+
except Exception as e:
87+
self.logger.debug("Failed to store %s for %s in cache: %s" % (description, request_url, str(e)))
88+
89+
return document
5090

5191
def read_wms_service_capabilities(self, service_name, item, themes_config):
5292
"""Load and parse WMS GetProjectSettings for a theme item.
@@ -62,51 +102,15 @@ def read_wms_service_capabilities(self, service_name, item, themes_config):
62102
self.default_qgis_server_url,
63103
posixpath.join(self.qgis_server_url_tenant_suffix, service_name)
64104
)
65-
66-
if len(full_url) > 2000:
67-
self.logger.warning(
68-
"WMS URL is longer than 2000 characters!")
69-
70-
document = None
71-
cache_file = os.path.join(self.cache_dir, full_url.replace('/', '_').replace(':', '_') + "WMS_GetProjectSettings")
72-
if self.use_cached_project_metadata:
73-
try:
74-
with open(cache_file) as fh:
75-
document = fh.read()
76-
self.logger.info("Using cached WMS GetProjectSettings for %s" % full_url)
77-
except:
78-
pass
79-
105+
params = {
106+
'SERVICE': 'WMS',
107+
'VERSION': '1.3.0',
108+
'REQUEST': 'GetProjectSettings',
109+
'CLEARCACHE': '1'
110+
}
111+
document = self.fetch_cached(full_url, params, "WMS_GetProjectSettings", "WMS GetProjectSettings")
80112
if not document:
81-
response = requests.get(
82-
full_url,
83-
params={
84-
'SERVICE': 'WMS',
85-
'VERSION': '1.3.0',
86-
'REQUEST': 'GetProjectSettings',
87-
'CLEARCACHE': '1'
88-
},
89-
timeout=self.project_settings_read_timeout
90-
)
91-
92-
if response.status_code != requests.codes.ok:
93-
self.logger.error(
94-
"Could not get WMS GetProjectSettings from %s:\n%s" %
95-
(full_url, response.content)
96-
)
97-
return {}
98-
99-
self.logger.info(
100-
"Downloaded WMS GetProjectSettings from %s" % full_url
101-
)
102-
103-
document = response.content
104-
try:
105-
os.makedirs(os.path.dirname(cache_file), exist_ok=True)
106-
with open(cache_file, "w") as fh:
107-
fh.write(document.decode('utf-8'))
108-
except Exception as e:
109-
self.logger.debug("Failed to store WMS GetProjectSettings for %s in cache: %s" % (full_url, str(e)))
113+
return {}
110114

111115
# parse WMS GetProjectSettings XML
112116
ElementTree.register_namespace('', 'http://www.opengis.net/wms')
@@ -431,8 +435,6 @@ def collect_wms_layers(self, layer, layer_names, internal_print_layers, ns, np,
431435

432436
return wms_layer
433437

434-
# WFS Capabilities
435-
436438
def read_wfs_service_capabilities(self, service_name, item):
437439
"""Load and parse WFS GetCapabilities for a theme item.
438440
@@ -445,53 +447,17 @@ def read_wfs_service_capabilities(self, service_name, item):
445447
# get GetProjectSettings
446448
full_url = urljoin(
447449
self.default_qgis_server_url,
448-
posixpath.join(self.qgis_server_url_tenant_suffix, service_name )
450+
posixpath.join(self.qgis_server_url_tenant_suffix, service_name)
449451
)
450-
451-
if len(full_url) > 2000:
452-
self.logger.warning(
453-
"WFS URL is longer than 2000 characters!")
454-
455-
document = None
456-
cache_file = os.path.join(self.cache_dir, full_url.replace('/', '_').replace(':', '_') + "_WFS_GetCapabilities")
457-
if self.use_cached_project_metadata:
458-
try:
459-
with open(cache_file) as fh:
460-
document = fh.read()
461-
self.logger.info("Using cached WFS GetCapabilities for %s" % full_url)
462-
except:
463-
pass
464-
452+
params = {
453+
'SERVICE': 'WFS',
454+
'VERSION': '1.1.0',
455+
'REQUEST': 'GetCapabilities',
456+
'CLEARCACHE': '1'
457+
}
458+
document = self.fetch_cached(full_url, params, "WFS_GetCapabilities", "WFS GetCapabilities")
465459
if not document:
466-
response = requests.get(
467-
full_url,
468-
params={
469-
'SERVICE': 'WFS',
470-
'VERSION': '1.1.0',
471-
'REQUEST': 'GetCapabilities',
472-
'CLEARCACHE': '1'
473-
},
474-
timeout=self.project_settings_read_timeout
475-
)
476-
477-
if response.status_code != requests.codes.ok:
478-
self.logger.error(
479-
"Could not get WFS GetCapabilities from %s:\n%s" %
480-
(full_url, response.content)
481-
)
482-
return {}
483-
484-
self.logger.info(
485-
"Downloaded WFS GetCapabilities from %s" % full_url
486-
)
487-
488-
document = response.content
489-
try:
490-
os.makedirs(os.path.dirname(cache_file), exist_ok=True)
491-
with open(cache_file, "w") as fh:
492-
fh.write(document.decode('utf-8'))
493-
except Exception as e:
494-
self.logger.debug("Failed to store WFS capabilities for %s in cache: %s" % (full_url, str(e)))
460+
return {}
495461

496462
# parse WFS Capabilities XML
497463
ElementTree.register_namespace('', 'http://www.opengis.net/wfs')
@@ -604,45 +570,15 @@ def collect_wfs_layers_attributes(self, full_url):
604570
:param str full_url: WFS URL
605571
"""
606572
try:
607-
document = None
608-
cache_file = os.path.join(self.cache_dir, full_url.replace('/', '_').replace(':', '_') + "_WFS_DescribeFeatureType")
609-
if self.use_cached_project_metadata:
610-
try:
611-
with open(cache_file) as fh:
612-
document = fh.read()
613-
self.logger.info("Using cached WFS DescribeFeatureType for %s" % full_url)
614-
except:
615-
pass
616-
573+
params = {
574+
'SERVICE': 'WFS',
575+
'VERSION': '1.1.0',
576+
'REQUEST': 'DescribeFeatureType',
577+
'CLEARCACHE': '1'
578+
}
579+
document = self.fetch_cached(full_url, params, "WFS_DescribeFeatureType", "WFS DescribeFeatureType")
617580
if not document:
618-
response = requests.get(
619-
full_url,
620-
params={
621-
'SERVICE': 'WFS',
622-
'VERSION': '1.1.0',
623-
'REQUEST': 'DescribeFeatureType'
624-
},
625-
timeout=self.project_settings_read_timeout
626-
)
627-
628-
if response.status_code != requests.codes.ok:
629-
self.logger.error(
630-
"Could not get WFS DescribeFeatureType from %s:\n%s" %
631-
(full_url, response.content)
632-
)
633-
return {}
634-
635-
self.logger.info(
636-
"Downloaded WFS DescribeFeatureType from %s" % full_url
637-
)
638-
639-
document = response.content
640-
try:
641-
os.makedirs(os.path.dirname(cache_file), exist_ok=True)
642-
with open(cache_file, "w") as fh:
643-
fh.write(document.decode('utf-8'))
644-
except Exception as e:
645-
self.logger.debug("Failed to store WFS DescribeFeatureType for %s in cache: %s" % (full_url, str(e)))
581+
return {}
646582

647583
# parse WFS Capabilities XML
648584
ElementTree.register_namespace('', 'http://www.w3.org/2001/XMLSchema')

0 commit comments

Comments
 (0)