Skip to content

Commit

Permalink
flake8: remove unused imports, fix bare excepts (#990)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Aug 4, 2024
1 parent 0b1197e commit 497c3ff
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 73 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys, os
import sys
from unittest.mock import MagicMock

# If extensions (or modules to document with autodoc) are in another directory,
Expand Down
15 changes: 10 additions & 5 deletions pycsw/core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ def _parse_csw(context, repos, record, identifier, pagesize=10):
md.getrecords2(typenames=csw_typenames, resulttype='hits',
outputschema=csw_outputschema)
matches = md.results['matches']
except: # this is a CSW, but server rejects query
except Exception: # this is a CSW, but server rejects query
LOGGER.debug('CSW query failed')
raise RuntimeError(md.response)

if pagesize > matches:
Expand Down Expand Up @@ -1241,7 +1242,8 @@ def _parse_fgdc(context, repos, exml):
try:
tmp = '%s,%s,%s,%s' % (bbox.minx, bbox.miny, bbox.maxx, bbox.maxy)
_set(context, recobj, 'pycsw:BoundingBox', util.bbox2wktpolygon(tmp))
except: # coordinates are corrupted, do not include
except Exception:
LOGGER.debug('Coordinates are corrupt')
_set(context, recobj, 'pycsw:BoundingBox', None)
else:
_set(context, recobj, 'pycsw:BoundingBox', None)
Expand Down Expand Up @@ -1321,7 +1323,8 @@ def get_value_by_language(pt_group, language, pt_type='text'):
data.geographic_bounding_box.east_bound_longitude,
data.geographic_bounding_box.north_bound_latitude)
_set(context, recobj, 'pycsw:BoundingBox', util.bbox2wktpolygon(tmp))
except: # coordinates are corrupted, do not include
except Exception:
LOGGER.debug('Coordinates are corrupt')
_set(context, recobj, 'pycsw:BoundingBox', None)
else:
_set(context, recobj, 'pycsw:BoundingBox', None)
Expand Down Expand Up @@ -1608,7 +1611,8 @@ def _parse_iso(context, repos, exml):
try:
tmp = '%s,%s,%s,%s' % (bbox.minx, bbox.miny, bbox.maxx, bbox.maxy)
_set(context, recobj, 'pycsw:BoundingBox', util.bbox2wktpolygon(tmp))
except: # coordinates are corrupted, do not include
except Exception:
LOGGER.debug('Coordinates are corrupt')
_set(context, recobj, 'pycsw:BoundingBox', None)
else:
_set(context, recobj, 'pycsw:BoundingBox', None)
Expand Down Expand Up @@ -1686,7 +1690,8 @@ def _parse_dc(context, repos, exml):
try:
tmp = '%s,%s,%s,%s' % (bbox.minx, bbox.miny, bbox.maxx, bbox.maxy)
_set(context, recobj, 'pycsw:BoundingBox', util.bbox2wktpolygon(tmp))
except: # coordinates are corrupted, do not include
except Exception:
LOGGER.debug('Coordinates are corrupt')
_set(context, recobj, 'pycsw:BoundingBox', None)
else:
_set(context, recobj, 'pycsw:BoundingBox', None)
Expand Down
24 changes: 11 additions & 13 deletions pycsw/ogc/csw/csw2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@
# =================================================================

import os
import sys
import cgi
from urllib.parse import quote, unquote
from io import StringIO
from pycsw.core.etree import etree
from pycsw import oaipmh, opensearch, sru
from pycsw import opensearch
from pycsw.ogc.csw.cql import cql2fes
from pycsw.plugins.profiles import profile as pprofile
import pycsw.plugins.outputschemas
from pycsw.core import config, log, metadata, util
from pycsw.core import metadata, util
from pycsw.core.formats.fmt_json import xml2dict
from pycsw.ogc.fes import fes1
import logging
Expand Down Expand Up @@ -517,7 +511,8 @@ def getdomain(self):
self.parent.context.namespaces)).text = pname
try:
operation, parameter = pname.split('.')
except:
except Exception as err:
LOGGER.debug(f'Cannot split pname: {err}')
return node
if (operation in self.parent.context.model['operations'].keys() and
parameter in
Expand All @@ -540,7 +535,8 @@ def getdomain(self):
else: # it's a core queryable, map to internal typename model
try:
pname2 = self.parent.repository.queryables['_all'][pname]['dbcol']
except:
except Exception as err:
LOGGER.debug(f'pname2 not found: {err}')
pname2 = pname

# decipher typename
Expand Down Expand Up @@ -780,7 +776,7 @@ def getrecords(self):

try:
name, order = tmp.rsplit(':', 1)
except:
except Exception:
return self.exceptionreport('InvalidParameterValue',
'sortby', 'Invalid SortBy value: must be in the format\
propertyname:A or propertyname:D')
Expand Down Expand Up @@ -1989,7 +1985,8 @@ def exceptionreport(self, code, locator, text):
try:
language = self.parent.config['server'].get('language')
ogc_schemas_base = self.parent.config['server'].get('ogc_schemas_base')
except:
except Exception:
LOGGER.debug('Dropping to default language and OGC schemas base')
language = 'en-US'
ogc_schemas_base = self.parent.context.ogc_schemas_base

Expand Down Expand Up @@ -2023,7 +2020,8 @@ def write_boundingbox(bbox, nsmap):
if bbox is not None:
try:
bbox2 = util.wkt2geom(bbox)
except:
except Exception as err:
LOGGER.debug(f'Geometry parsing error: {err}')
return None

if len(bbox2) == 4:
Expand Down
29 changes: 13 additions & 16 deletions pycsw/ogc/csw/csw3.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,12 @@
#
# =================================================================

import json
import os
import sys
import cgi
from time import time
from urllib.parse import quote, unquote
from io import StringIO
from pycsw.core.etree import etree
from pycsw.ogc.csw.cql import cql2fes
from pycsw import oaipmh, opensearch, sru
from pycsw.plugins.profiles import profile as pprofile
import pycsw.plugins.outputschemas
from pycsw.core import config, log, metadata, util
from pycsw import opensearch
from pycsw.core import metadata, util
from pycsw.core.formats.fmt_json import xml2dict
from pycsw.ogc.fes import fes1, fes2
import logging
Expand Down Expand Up @@ -113,7 +106,8 @@ def getcapabilities(self):
try:
updatesequence = \
util.get_time_iso2unix(self.parent.repository.query_insert())
except:
except Exception as err:
LOGGER.debug(f'Cannot set updatesequence: {err}')
updatesequence = None

node = etree.Element(util.nspath_eval('csw30:Capabilities',
Expand Down Expand Up @@ -512,7 +506,8 @@ def getdomain(self):
self.parent.context.namespaces)).text = pname
try:
operation, parameter = pname.split('.')
except:
except Exception as err:
LOGGER.debug(f'pname2 not found: {err}')
return node
if (operation in self.parent.context.model['operations'] and
parameter in self.parent.context.model['operations'][operation]['parameters']):
Expand All @@ -534,8 +529,8 @@ def getdomain(self):
else: # it's a core queryable, map to internal typename model
try:
pname2 = self.parent.repository.queryables['_all'][pname]['dbcol']
except:
pname2 = pname
except Exception as err:
LOGGER.debug(f'pname2 not found: {err}')

# decipher typename
dvtype = None
Expand Down Expand Up @@ -810,7 +805,7 @@ def getrecords(self):

try:
name, order = tmp.rsplit(':', 1)
except:
except Exception:
return self.exceptionreport('InvalidParameterValue',
'sortby', 'Invalid SortBy value: must be in the format\
propertyname:A or propertyname:D')
Expand Down Expand Up @@ -2079,7 +2074,8 @@ def exceptionreport(self, code, locator, text):
try:
language = self.parent.config['server'].get('language')
ogc_schemas_base = self.parent.config['server'].get('ogc_schemas_base')
except:
except Exception:
LOGGER.debug('Dropping to default language and OGC schemas base')
language = 'en-US'
ogc_schemas_base = self.parent.context.ogc_schemas_base

Expand Down Expand Up @@ -2157,7 +2153,8 @@ def write_boundingbox(bbox, nsmap):
if bbox is not None:
try:
bbox2 = util.wkt2geom(bbox)
except:
except Exception as err:
LOGGER.debug(f'Geometry parsing error: {err}')
return None

if len(bbox2) == 4:
Expand Down
3 changes: 1 addition & 2 deletions pycsw/ogc/fes/fes1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Authors: Tom Kralidis <tomkralidis@gmail.com>
# Angelos Tzotsos <tzotsos@gmail.com>
#
# Copyright (c) 2015 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
# Copyright (c) 2015 Angelos Tzotsos
#
# Permission is hereby granted, free of charge, to any person
Expand Down Expand Up @@ -416,7 +416,6 @@ def set_spatial_ranking(geometry):
util.ranking_pass = True
util.ranking_query_geometry = geometry.wkt
elif geometry.type in ['LineString', 'Point']:
from shapely.geometry.base import BaseGeometry
from shapely.geometry import box
from shapely.wkt import loads,dumps
ls = loads(geometry.wkt)
Expand Down
3 changes: 1 addition & 2 deletions pycsw/ogc/fes/fes2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Authors: Tom Kralidis <tomkralidis@gmail.com>
# Angelos Tzotsos <tzotsos@gmail.com>
#
# Copyright (c) 2015 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
# Copyright (c) 2015 Angelos Tzotsos
#
# Permission is hereby granted, free of charge, to any person
Expand Down Expand Up @@ -434,7 +434,6 @@ def set_spatial_ranking(geometry):
util.ranking_pass = True
util.ranking_query_geometry = geometry.wkt
elif geometry.type in ['LineString', 'Point']:
from shapely.geometry.base import BaseGeometry
from shapely.geometry import box
from shapely.wkt import loads,dumps
ls = loads(geometry.wkt)
Expand Down
5 changes: 3 additions & 2 deletions pycsw/ogc/gml/gml3.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ def transform(self, src, dest):
proj_src = 'epsg:%s' % src
proj_dst = 'epsg:%s' % dest
transformer = Transformer.from_crs(proj_src, proj_dst, always_xy=True)
except:
raise RuntimeError('Invalid projection transformation')
except Exception as err:
msg = f'Invalid projection transformation: {err}'
raise RuntimeError(msg)

geom = loads(self.wkt)

Expand Down
8 changes: 4 additions & 4 deletions pycsw/ogc/gml/gml32.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Authors: Tom Kralidis <tomkralidis@gmail.com>
#
# Copyright (c) 2015 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand All @@ -28,7 +28,6 @@
#
# =================================================================

from copy import deepcopy
import logging
from owslib import crs

Expand Down Expand Up @@ -213,8 +212,9 @@ def transform(self, src, dest):
proj_src = 'epsg:%s' % src
proj_dst = 'epsg:%s' % dest
transformer = Transformer.from_crs(proj_src, proj_dst, always_xy=True)
except:
raise RuntimeError('Invalid projection transformation')
except Exception as err:
msg = f'Invalid projection transformation: {err}'
raise RuntimeError(msg)

geom = loads(self.wkt)

Expand Down
1 change: 0 additions & 1 deletion pycsw/opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
# =================================================================

import logging
from urllib.parse import urlencode

from pycsw.core import util
from pycsw.core.etree import etree
Expand Down
6 changes: 3 additions & 3 deletions pycsw/plugins/outputschemas/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Authors: Tom Kralidis <tomkralidis@gmail.com>
#
# Copyright (c) 2015 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand All @@ -28,7 +28,6 @@
#
# =================================================================

import os
from pycsw.core import util
from pycsw.core.etree import etree

Expand Down Expand Up @@ -132,7 +131,8 @@ def write_extent(bbox, nsmap):
if bbox is not None:
try:
bbox2 = util.wkt2geom(bbox)
except:
except Exception as err:
LOGGER.debug(f'Geometry parsing error: {err}')
return None
where = etree.Element(util.nspath_eval('georss:where', NAMESPACES))
envelope = etree.SubElement(where, util.nspath_eval('gml:Envelope', nsmap), srsName='http://www.opengis.net/def/crs/EPSG/0/4326')
Expand Down
5 changes: 3 additions & 2 deletions pycsw/plugins/outputschemas/datacite.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# This module intends to follow DataCite 4.3
#
# PyCSW Copyright (C) 2015 Tom Kralidis
# PyCSW Copyright (C) 2024 Tom Kralidis
# Schema Copyright (C) 2016 CERN
# Schema Copyright (C) 2019 Caltech
#
Expand Down Expand Up @@ -341,7 +341,8 @@ def write_record(result, esn, context, url=None):
if bbox not in [None, '']:
try:
bbox2 = util.wkt2geom(bbox)
except:
except Exception as err:
LOGGER.debug(f'Geometry parsing error: {err}')
return None
bounds = etree.SubElement(node, util.nspath_eval('geoLocations', NAMESPACES))
bound = etree.SubElement(bounds, util.nspath_eval('geoLocation', NAMESPACES))
Expand Down
7 changes: 3 additions & 4 deletions pycsw/plugins/outputschemas/dif.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Authors: Tom Kralidis <tomkralidis@gmail.com>
#
# Copyright (c) 2015 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -198,12 +198,11 @@ def write_record(result, esn, context, url=None):
def write_extent(bbox, nsmap):
''' Generate BBOX extent '''

from shapely.wkt import loads

if bbox is not None:
try:
bbox2 = util.wkt2geom(bbox)
except:
except Exception as err:
LOGGER.debug(f'Geometry parsing error: {err}')
return None
extent = etree.Element(util.nspath_eval('dif:Spatial_Coverage', nsmap))
etree.SubElement(extent, util.nspath_eval('dif:Southernmost_Latitude', nsmap)).text = str(bbox2[1])
Expand Down
5 changes: 3 additions & 2 deletions pycsw/plugins/outputschemas/fgdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Authors: Tom Kralidis <tomkralidis@gmail.com>
#
# Copyright (c) 2015 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -166,7 +166,8 @@ def write_extent(bbox):
if bbox is not None:
try:
bbox2 = util.wkt2geom(bbox)
except:
except Exception as err:
LOGGER.debug(f'Geometry parsing error: {err}')
return None

spdom = etree.Element('spdom')
Expand Down
5 changes: 3 additions & 2 deletions pycsw/plugins/outputschemas/gm03.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Authors: Tom Kralidis <tomkralidis@gmail.com>
#
# Copyright (c) 2015 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -228,7 +228,8 @@ def write_extent(bbox, nsmap):
if bbox is not None:
try:
bbox2 = util.wkt2geom(bbox)
except:
except Exception as err:
LOGGER.debug(f'Geometry parsing error: {err}')
return None
bounding_box = etree.Element(util.nspath_eval('gm03:GM03_2_1Core.Core.EX_GeographicBoundingBox', NAMESPACES))
etree.SubElement(bounding_box, util.nspath_eval('gm03:northBoundLatitude', nsmap)).text = str(bbox2[3])
Expand Down
Loading

0 comments on commit 497c3ff

Please sign in to comment.